"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defaultConfig = exports.defaultBrowser = void 0;
exports.readConfig = readConfig;
var _fs = _interopRequireDefault(require("fs"));
var _path = _interopRequireDefault(require("path"));
var _browser = require("./storybook/providers/browser");
var _types = require("../types");
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; }
const defaultBrowser = exports.defaultBrowser = 'chrome';
const defaultConfig = exports.defaultConfig = {
  disableTelemetry: false,
  useDocker: true,
  dockerImage: 'aerokube/selenoid:latest-release',
  dockerImagePlatform: '',
  pullImages: true,
  failFast: false,
  storybookUrl: 'http://localhost:6006',
  screenDir: _path.default.resolve('images'),
  reportDir: _path.default.resolve('report'),
  maxRetries: 0,
  diffOptions: {
    threshold: 0,
    includeAA: true
  },
  browsers: {
    [defaultBrowser]: true
  },
  hooks: {},
  babelOptions: _ => _,
  testsRegex: /\.creevey\.(t|j)s$/
};
function normalizeBrowserConfig(name, config) {
  if (typeof config == 'boolean') return {
    browserName: name
  };
  if (typeof config == 'string') return {
    browserName: config
  };
  return config;
}
function resolveConfigPath(configPath) {
  const rootDir = process.cwd();
  const configDir = _path.default.resolve('.creevey');
  if ((0, _types.isDefined)(configPath)) {
    configPath = _path.default.resolve(configPath);
  } else if (_fs.default.existsSync(configDir)) {
    configPath = _path.default.join(configDir, 'config');
    // TODO We already find file with extension, why not use it?
  } else if (_fs.default.readdirSync(rootDir).find(filename => filename.startsWith('creevey.config'))) {
    configPath = _path.default.join(rootDir, 'creevey.config');
  }
  return configPath;
}
async function readConfig(options) {
  const configPath = resolveConfigPath(options.config);
  const userConfig = {
    ...defaultConfig
  };
  if ((0, _types.isDefined)(configPath)) Object.assign(userConfig, (await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(configPath)).default);
  if (!userConfig.storiesProvider) userConfig.storiesProvider = _browser.loadStories;
  if (options.failFast != undefined) userConfig.failFast = Boolean(options.failFast);
  if (options.reportDir) userConfig.reportDir = _path.default.resolve(options.reportDir);
  if (options.screenDir) userConfig.screenDir = _path.default.resolve(options.screenDir);
  if (options.storybookUrl) userConfig.storybookUrl = options.storybookUrl;

  // NOTE: Hack to pass typescript checking
  const config = userConfig;
  Object.entries(config.browsers).forEach(([browser, browserConfig]) => config.browsers[browser] = normalizeBrowserConfig(browser, browserConfig));
  return config;
}