"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = update;
var _path = _interopRequireDefault(require("path"));
var _fs = _interopRequireWildcard(require("fs"));
var _micromatch = _interopRequireDefault(require("micromatch"));
var _types = require("../types");
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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function tryToLoadTestsData(filename) {
  try {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    return require(filename);
  } catch (_) {
    /* noop */
  }
}
const actualRegex = /^(.*)-actual-(\d+)\.png$/i;
function approve(dirents, srcPath, dstPath, testPaths, isMatch) {
  dirents.filter(dirent => dirent.isFile()).map(dirent => actualRegex.exec(dirent.name)).filter(_types.isDefined).filter(([fileName, imageName]) => !testPaths || testPaths.find(([token]) => token == imageName) && isMatch(_path.default.join(srcPath, fileName))).reduce((images, [, imageName, retry]) => Number(retry) > (images.get(imageName) ?? -1) ? images.set(imageName, Number(retry)) : images, new Map()).forEach((retry, imageName) => {
    (0, _fs.mkdirSync)(dstPath, {
      recursive: true
    });
    _fs.default.copyFileSync(_path.default.join(srcPath, `${imageName}-actual-${retry}.png`), _path.default.join(dstPath, `${imageName}.png`));
  });
}
function traverse(srcPath, dstPath, testPaths, isMatch) {
  const dirents = _fs.default.readdirSync(srcPath, {
    withFileTypes: true
  });
  approve(dirents, srcPath, dstPath, testPaths, isMatch);
  dirents.filter(dirent => dirent.isDirectory()).map(dirent => [dirent.name, testPaths === null || testPaths === void 0 ? void 0 : testPaths.map(([token, ...restPath]) => token == dirent.name ? restPath : null).filter(_types.isDefined)]).filter(([, paths]) => !paths || paths.length > 0).forEach(([dirname, paths]) => traverse(_path.default.join(srcPath, dirname), _path.default.join(dstPath, dirname), paths, isMatch));
}
function update(config, grepPattern) {
  const {
    reportDir,
    screenDir
  } = config;
  const isMatch = grepPattern ? _micromatch.default.matcher(grepPattern, {
    contains: true
  }) : () => true;
  const testsMeta = tryToLoadTestsData(`${reportDir}/tests.json`);
  const testsReport = tryToLoadTestsData(`${reportDir}/data`);
  let testPaths = null;
  if (testsMeta && testsReport) {
    testPaths = Object.values(testsMeta).filter(_types.isDefined).filter(({
      id
    }) => {
      var _testsReport$id;
      return ((_testsReport$id = testsReport[id]) === null || _testsReport$id === void 0 ? void 0 : _testsReport$id.status) == 'failed';
    }).map(({
      storyPath,
      testName,
      browser
    }) => [...storyPath, ...(testName ? [testName] : []), browser]);
  }
  traverse(reportDir, screenDir, testPaths, value => isMatch(_path.default.relative(reportDir, value)));
}