"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = master;
var _path = _interopRequireDefault(require("path"));
var _types = require("../../types");
var _stories = require("../stories");
var _runner = _interopRequireDefault(require("./runner"));
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; }
function mergeTests(testsWithReports, testsFromStories) {
  Object.values(testsFromStories).filter(_types.isDefined).forEach(test => {
    const testWithReport = testsWithReports[test.id];
    if (!testWithReport) return;
    test.retries = testWithReport.retries;
    if (testWithReport.status == 'success' || testWithReport.status == 'failed') test.status = testWithReport.status;
    test.results = testWithReport.results;
    test.approved = testWithReport.approved;
  });
  return testsFromStories;
}
async function master(config, options) {
  const runner = new _runner.default(config);
  const reportDataPath = _path.default.join(config.reportDir, 'data.js');
  let testsFromReport = {};
  try {
    testsFromReport = await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(reportDataPath);
  } catch (error) {
    // Ignore error
  }
  await runner.init();
  const tests = await (0, _stories.loadTestsFromStories)(Object.keys(config.browsers), listener => config.storiesProvider(config, options, listener), testsDiff => {
    runner.updateTests(testsDiff);
    (0, _stories.saveTestsJson)(runner.tests, config.reportDir);
  });
  runner.tests = mergeTests(testsFromReport, tests);
  (0, _stories.saveTestsJson)(runner.tests, config.reportDir);
  return runner;
}