import { Buffer } from 'buffer'
import { request } from 'http'

import rawBody from 'raw-body'

const { byteLength } = Buffer

// Report log with a HTTP request
// TODO: use a proper HTTP request library
export const report = function ({ log, opts: { method = 'POST', ...opts } }) {
  const methodA = method.toUpperCase()

  const body = JSON.stringify(log)

  const req = getRequest({ method: methodA, body, ...opts })

  // eslint-disable-next-line promise/avoid-new
  const promise = new Promise((resolve, reject) =>
    reqToPromise({ req, resolve, reject }),
  )

  req.end(body)

  return promise
}

const getRequest = function ({ method, body, hostname, port, auth, path }) {
  const headers = getHeaders({ body })

  const req = request({
    method,
    hostname,
    port,
    auth,
    path,
    headers,
    timeout: TIMEOUT,
  })
  return req
}

const TIMEOUT = 5e3

const getHeaders = function ({ body }) {
  return {
    'Content-Type': 'application/json',
    'Content-Length': byteLength(body),
  }
}

const reqToPromise = function ({ req, resolve, reject }) {
  req.on('response', (res) => responseHandler({ res, resolve, reject }))

  req.on('error', reject)
}

const responseHandler = async function ({ res, resolve, reject }) {
  const isSuccess = String(res.statusCode).startsWith('2')

  if (isSuccess) {
    resolve()
  }

  const response = await rawBody(res, 'utf-8')
  const responseA = JSON.stringify(response)
  reject(responseA)
}

@@ -512,16 +512,18 @@
 ject) =%3E
+ %7B
 %0A    req
@@ -557,20 +557,20 @@
 eject %7D)
-,
 %0A  
+%7D
 )%0A%0A  req

Fix linting