"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.loadStories = void 0;
var _chokidar = _interopRequireDefault(require("chokidar"));
var _browser = require("./browser");
var _logger = require("../../logger");
var _parser = _interopRequireDefault(require("../../testsFiles/parser"));
var _utils = require("../../../server/utils");
var _shared = require("../../../shared");
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 loadStories = async (_config, _options, storiesListener) => {
  let creeveyParamsByStoryId = {};
  const mergeParamsFromTestsToStory = (story, creeveyParams) => {
    if (story.parameters) {
      story.parameters.creevey = (0, _shared.combineParameters)(story.parameters.creevey || {}, creeveyParams);
    }
  };
  const stories = await (0, _browser.loadStories)(_config, {}, updatedStoriesByFiles => {
    Array.from(updatedStoriesByFiles.entries()).forEach(([, storiesArray]) => {
      storiesArray.forEach(story => {
        const creeveyParams = creeveyParamsByStoryId[story.id];
        if (creeveyParams) {
          mergeParamsFromTestsToStory(story, creeveyParams);
        }
      });
    });
    storiesListener(updatedStoriesByFiles);
  });

  // TODO fix test files hot reloading
  creeveyParamsByStoryId = await parseParams(_config /*, (data) => console.log(data) */);
  Object.entries(stories).forEach(([storyId, story]) => {
    mergeParamsFromTestsToStory(story, creeveyParamsByStoryId[storyId]);
  });
  return stories;
};
exports.loadStories = loadStories;
async function parseParams(config, listener) {
  if (!config.testsDir) {
    return Promise.resolve({});
  }
  const testFiles = (0, _utils.readDirRecursive)(config.testsDir).filter(file => {
    var _config$testsRegex;
    return (_config$testsRegex = config.testsRegex) === null || _config$testsRegex === void 0 ? void 0 : _config$testsRegex.test(file);
  });
  await (await Promise.resolve().then(() => _interopRequireWildcard(require('../../testsFiles/register')))).default(config);
  if (listener) {
    _chokidar.default.watch(testFiles).on('change', filePath => {
      _logger.logger.debug(`changed: ${filePath}`);

      // doesn't work, always returns {} due modules caching
      // see https://github.com/nodejs/modules/issues/307
      void (0, _parser.default)(testFiles).then(data => listener(data));
    });
  }
  return (0, _parser.default)(testFiles);
}