import WebSocketRequestContext from './WebSocketRequestContext.js'

// TODO this should be probably moved to utils, and combined with other header
// functions and utilities
function createMultiValueHeaders(headers) {
  return Object.entries(headers).reduce((acc, [key, value]) => {
    acc[key] = [value]

    return acc
  }, {})
}

export default class WebSocketDisconnectEvent {
  constructor(connectionId) {
    this._connectionId = connectionId
  }

  create() {
    const headers = {
      Host: 'localhost',
      'x-api-key': '',
      'x-restapi': '',
    }

    const multiValueHeaders = createMultiValueHeaders(headers)

    const requestContext = new WebSocketRequestContext(
      'DISCONNECT',
      '$disconnect',
      this._connectionId,
    ).create()

    return {
      headers,
      isBase64Encoded: false,
      multiValueHeaders,
      requestContext,
    }
  }
}

@@ -64,273 +64,83 @@
 js'%0A
-%0A// TODO this should be probably moved to utils, and combined with other header%0A// functions and utilities%0Afunction createMultiValueHeaders(headers) %7B%0A  return Object.entries(headers).reduce((acc, %5Bkey, value%5D) =%3E %7B%0A    acc%5Bkey%5D = %5Bvalue%5D%0A%0A    return acc%0A  %7D, %7B%7D)%0A%7D
+import %7B parseHeaders, parseMultiValueHeaders %7D from '../../utils/index.js'
 %0A%0Aex
@@ -279,37 +279,90 @@
     
-const headers = %7B%0A      
+// TODO FIXME not sure where the headers come from%0A    const rawHeaders = %5B'
 Host
-:
+',
  'lo
@@ -370,22 +370,16 @@
 alhost',
-%0A     
  'x-api-
@@ -386,19 +386,13 @@
 key'
-:
+,
  '',
-%0A     
  'x-
@@ -403,20 +403,59 @@
 api'
-: '',
+, ''%5D%0A
 %0A    
-%7D%0A
+const headers = parseHeaders(rawHeaders)
 %0A   
@@ -481,21 +481,20 @@
 aders = 
-creat
+pars
 eMultiVa
@@ -504,17 +504,20 @@
 Headers(
-h
+rawH
 eaders)%0A

Rewrite header handling in websocket disconnect event