import forEachObjIndexed from 'ramda/src/forEachObjIndexed';
import isStream from './isStream';
import isString from './isString';
import sendString from './sendString';
import sendStream from './sendStream';
import isPlainObject from 'lodash.isplainobject';

const sendResponse = (nativeResponse, response) => {
  // eslint-disable-next-line no-param-reassign
  nativeResponse.statusCode = response.statusCode || 500;

  forEachObjIndexed((value, key) => {
    nativeResponse.setHeader(key, value);
  }, response.headers);

  if (response.body) {
    if (isStream(response.body)) {
      sendStream(response.body, nativeResponse);
      return;
    }

    if (isString(response.body)) {
      sendString(response.body, nativeResponse);
      return;
    }

    if (isPlainObject(response.body)) {
      sendString(JSON.stringify(response.body), nativeResponse);
      return;
    }
  }

  throw new Error('Invalid/Unset response body');
};

export default sendResponse;

@@ -309,17 +309,17 @@
 =%3E %7B%0A  /
-/
+*
  eslint-
@@ -329,18 +329,8 @@
 able
--next-line
  no-
@@ -343,16 +343,19 @@
 reassign
+ */
 %0A  nativ
@@ -404,16 +404,111 @@
  %7C%7C 500;
+%0A  if (response.statusMessage) %7B%0A    nativeResponse.statusMessage = response.statusMessage;%0A  %7D
 %0A%0A  forE
@@ -964,16 +964,58 @@
 ;%0A    %7D%0A
+    /* eslint-enable no-param-reassign */%0A
   %7D%0A%0A  t

add response.statusMessage