import WebSocketRequestContext from './WebSocketRequestContext.js'
import { parseHeaders, parseMultiValueHeaders } from '../../utils/index.js'

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

  create() {
    // TODO FIXME not sure where the headers come from
    const rawHeaders = ['Host', 'localhost', 'x-api-key', '', 'x-restapi', '']

    const headers = parseHeaders(rawHeaders)
    const multiValueHeaders = parseMultiValueHeaders(rawHeaders)

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

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

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,
    }
  }
}

--- 
+++ 
@@ -1,5 +1,14 @@
 import WebSocketRequestContext from './WebSocketRequestContext.js'
-import { parseHeaders, parseMultiValueHeaders } from '../../utils/index.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) {
@@ -7,11 +16,13 @@
   }
 
   create() {
-    // TODO FIXME not sure where the headers come from
-    const rawHeaders = ['Host', 'localhost', 'x-api-key', '', 'x-restapi', '']
+    const headers = {
+      Host: 'localhost',
+      'x-api-key': '',
+      'x-restapi': '',
+    }
 
-    const headers = parseHeaders(rawHeaders)
-    const multiValueHeaders = parseMultiValueHeaders(rawHeaders)
+    const multiValueHeaders = createMultiValueHeaders(headers)
 
     const requestContext = new WebSocketRequestContext(
       'DISCONNECT',
Rewrite header handling in websocket disconnect event
