import path from 'path';
import { isDefined } from '../../types';
import { loadTestsFromStories, saveTestsJson } from '../stories';
import Runner from './runner';
function mergeTests(testsWithReports, testsFromStories) {
  Object.values(testsFromStories).filter(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;
}
export default async function master(config, options) {
  const runner = new Runner(config);
  const reportDataPath = path.join(config.reportDir, 'data.js');
  let testsFromReport = {};
  try {
    testsFromReport = await import(reportDataPath);
  } catch (error) {
    // Ignore error
  }
  await runner.init();
  const tests = await loadTestsFromStories(Object.keys(config.browsers), listener => config.storiesProvider(config, options, listener), testsDiff => {
    runner.updateTests(testsDiff);
    saveTestsJson(runner.tests, config.reportDir);
  });
  runner.tests = mergeTests(testsFromReport, tests);
  saveTestsJson(runner.tests, config.reportDir);
  return runner;
}