"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;
var _cluster = _interopRequireDefault(require("cluster"));
var _config = require("./config");
var _logger = require("./logger");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// NOTE: Impure function, mutate config by adding gridUrl prop
async function startWebdriverServer(config, options) {
  if (config.useDocker) {
    return (await Promise.resolve().then(() => _interopRequireWildcard(require('./docker')))).default(config, options.browser, async () => (await Promise.resolve().then(() => _interopRequireWildcard(require('./selenium/selenoid')))).startSelenoidContainer(config, options.debug));
  } else {
    return (await Promise.resolve().then(() => _interopRequireWildcard(require('./selenium/selenoid')))).startSelenoidStandalone(config, options.debug);
  }
}
async function _default(options) {
  const config = await (0, _config.readConfig)(options);
  const {
    browser = _config.defaultBrowser,
    tests,
    update,
    ui,
    port
  } = options;
  if (!config) return;

  // NOTE: We don't need docker nor selenoid for webpack or update options
  if (!(config.gridUrl || Object.values(config.browsers).every(({
    gridUrl
  }) => gridUrl)) && !tests && !update) {
    await startWebdriverServer(config, options);
  }
  switch (true) {
    case Boolean(update):
      {
        return (await Promise.resolve().then(() => _interopRequireWildcard(require('./update')))).default(config, typeof update == 'string' ? update : undefined);
      }
    case _cluster.default.isPrimary:
      {
        _logger.logger.info('Starting Master Process');
        const resolveApi = (await Promise.resolve().then(() => _interopRequireWildcard(require('./master/server')))).default(config.reportDir, port, ui);
        return (await Promise.resolve().then(() => _interopRequireWildcard(require('./master')))).default(config, options, resolveApi);
      }
    default:
      {
        _logger.logger.info(`Starting Worker for ${browser}`);
        return (await Promise.resolve().then(() => _interopRequireWildcard(require('./worker')))).default(config, {
          ...options,
          browser
        });
      }
  }
}