/* eslint object-curly-newline: 0, max-len: 0 */
const fs = require('fs');
const path = require('path');
const CDP = require('chrome-remote-interface');

const injectMediator = `
(function (win) {
  win.sessionStorage.setItem('aw', '');

  win.awMediator = {
    emit(name, data = '') {
      const json = JSON.stringify({ name, data });
      win.sessionStorage.setItem('aw', json);
    },
  };
}(window));
`;

function getContent(filePath) {
  return fs.readFileSync(filePath, 'utf-8');
}

module.exports = async function connect(argv, files, debugging) {
  const client = await CDP(argv.client);
  const { DOM, DOMStorage, Console, Network, Page, Runtime } = client;

  const { bail, useColors, reporter, ui, timeout } = argv.mocha;
  const injectMochaOptions = `window.mochaOptions = ${JSON.stringify({
    bail,
    useColors,
    reporter,
    ui,
    timeout,
  })}`;
  console.log(argv.mocha);
  const injectAwFiles = `window.awFiles = ${JSON.stringify(files)}`;
  const injectAwDevtools = `window.awDevtools = ${JSON.stringify(
    argv.chrome.devtools,
  )}`;
  const injectAwDebugging = `window.awDebugging = ${JSON.stringify(debugging)}`;

  await Promise.all([
    DOM.enable(),
    DOMStorage.enable(),
    Network.enable(),
    Page.enable(),
    Runtime.enable(),
    Console.enable(),
  ]);
  const sourceMapSupport = `${path.dirname(
    require.resolve('source-map-support'),
  )}/browser-source-map-support.js`;
  await Page.addScriptToEvaluateOnLoad({
    scriptSource: `${getContent(sourceMapSupport)};`,
  });
  await Page.addScriptToEvaluateOnLoad({
    scriptSource: 'sourceMapSupport.install();',
  });
  await Page.addScriptToEvaluateOnLoad({ scriptSource: injectMediator });
  await Page.addScriptToEvaluateOnLoad({ scriptSource: injectMochaOptions });
  await Page.addScriptToEvaluateOnLoad({ scriptSource: injectAwFiles });
  await Page.addScriptToEvaluateOnLoad({ scriptSource: injectAwDevtools });
  await Page.addScriptToEvaluateOnLoad({ scriptSource: injectAwDebugging });
  await Page.addScriptToEvaluateOnLoad({
    scriptSource: getContent(path.join(__dirname, 'browser-shim.js')),
  });
  return client;
};

@@ -872,34 +872,8 @@
 %7D%60;%0A
-  console.log(argv.mocha);
 %0A  c

remove console log