import cluster from 'cluster';
import { readConfig, defaultBrowser } from './config';
import { logger } from './logger';

// NOTE: Impure function, mutate config by adding gridUrl prop
async function startWebdriverServer(config, options) {
  if (config.useDocker) {
    return (await import('./docker')).default(config, options.browser, async () => (await import('./selenium/selenoid')).startSelenoidContainer(config, options.debug));
  } else {
    return (await import('./selenium/selenoid')).startSelenoidStandalone(config, options.debug);
  }
}
export default async function (options) {
  const config = await readConfig(options);
  const {
    browser = 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 import('./update')).default(config, typeof update == 'string' ? update : undefined);
      }
    case cluster.isPrimary:
      {
        logger.info('Starting Master Process');
        const resolveApi = (await import('./master/server')).default(config.reportDir, port, ui);
        return (await import('./master')).default(config, options, resolveApi);
      }
    default:
      {
        logger.info(`Starting Worker for ${browser}`);
        return (await import('./worker')).default(config, {
          ...options,
          browser
        });
      }
  }
}