"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.calcStatus = calcStatus;
exports.checkSuite = checkSuite;
exports.countTestsStatus = countTestsStatus;
exports.filterTests = filterTests;
exports.flattenSuite = flattenSuite;
exports.getBorderSize = getBorderSize;
exports.getCheckedTests = getCheckedTests;
exports.getConnectionUrl = getConnectionUrl;
exports.getImageUrl = getImageUrl;
exports.getSuiteByPath = getSuiteByPath;
exports.getTestByPath = getTestByPath;
exports.getTestPath = getTestPath;
exports.getTestPathFromSearch = getTestPathFromSearch;
exports.getTestsByStoryId = getTestsByStoryId;
exports.openSuite = openSuite;
exports.removeTests = removeTests;
exports.setSearchParams = setSearchParams;
exports.treeifyTests = treeifyTests;
exports.updateTestStatus = updateTestStatus;
exports.useApplyScale = useApplyScale;
exports.useCalcScale = useCalcScale;
exports.useForceUpdate = useForceUpdate;
exports.useLoadImages = useLoadImages;
exports.useResizeObserver = useResizeObserver;
exports.useTheme = useTheme;
var _theming = require("@storybook/theming");
var _qs = require("qs");
var _react = require("react");
var _types = require("../../types");
const statusUpdatesMap = new Map([[undefined, /(unknown|success|failed|pending|running)/], ['unknown', /(success|failed|pending|running)/], ['success', /(failed|pending|running)/], ['failed', /(pending|running)/], ['pending', /running/]]);
function makeEmptySuiteNode() {
  let path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return {
    path,
    skip: true,
    opened: false,
    checked: true,
    indeterminate: false,
    children: {}
  };
}
function calcStatus(oldStatus, newStatus) {
  return newStatus && statusUpdatesMap.get(oldStatus)?.test(newStatus) ? newStatus : oldStatus;
}
function getTestPath(test) {
  const {
    browser,
    testName,
    storyPath
  } = test;
  return [...storyPath, testName, browser].filter(_types.isDefined);
}
function getSuiteByPath(suite, path) {
  return path.reduce((suiteOrTest, pathToken) => (0, _types.isTest)(suiteOrTest) ? suiteOrTest : suiteOrTest?.children[pathToken], suite);
}
function getTestByPath(suite, path) {
  const test = getSuiteByPath(suite, path) ?? suite;
  return (0, _types.isTest)(test) ? test : null;
}
function getTestsByStoryId(suite, storyId) {
  return Object.values(suite.children).filter(_types.isDefined).flatMap(suiteOrTest => {
    if ((0, _types.isTest)(suiteOrTest)) return suiteOrTest.storyId === storyId ? suiteOrTest : [];
    return getTestsByStoryId(suiteOrTest, storyId);
  }).filter(_types.isDefined);
}
function checkTests(suiteOrTest, checked) {
  suiteOrTest.checked = checked;
  if (!(0, _types.isTest)(suiteOrTest)) {
    suiteOrTest.indeterminate = false;
    Object.values(suiteOrTest.children).filter(_types.isDefined).forEach(child => checkTests(child, checked));
  }
}
function updateChecked(suite) {
  const children = Object.values(suite.children).filter(_types.isDefined).filter(child => !child.skip);
  const checkedEvery = children.every(test => test.checked);
  const checkedSome = children.some(test => test.checked);
  const indeterminate = children.some(test => (0, _types.isTest)(test) ? false : test.indeterminate) || !checkedEvery && checkedSome;
  const checked = indeterminate || suite.checked == checkedEvery ? suite.checked : checkedEvery;
  suite.checked = checked;
  suite.indeterminate = indeterminate;
}
function checkSuite(suite, path, checked) {
  const subSuite = getSuiteByPath(suite, path);
  if (subSuite) checkTests(subSuite, checked);
  path.slice(0, -1).map((_, index, tokens) => tokens.slice(0, tokens.length - index)).forEach(parentPath => {
    const parentSuite = getSuiteByPath(suite, parentPath);
    if ((0, _types.isTest)(parentSuite)) return;
    if (parentSuite) updateChecked(parentSuite);
  });
  updateChecked(suite);
}
function treeifyTests(testsById) {
  const rootSuite = makeEmptySuiteNode();
  rootSuite.opened = true;
  Object.values(testsById).forEach(test => {
    if (!test) return;
    const [browser, ...testPath] = getTestPath(test).reverse();
    const lastSuite = testPath.reverse().reduce((suite, token) => {
      const subSuite = suite.children[token] || makeEmptySuiteNode([...suite.path, token]);
      subSuite.status = calcStatus(subSuite.status, test.status);
      if (!test.skip) subSuite.skip = false;
      if (!subSuite.skip) suite.skip = false;
      suite.children[token] = subSuite;
      suite.status = calcStatus(suite.status, subSuite.status);
      if ((0, _types.isTest)(subSuite)) {
        throw new Error(`Suite and Test should not have same path '${JSON.stringify(getTestPath(subSuite))}'`);
      }
      return subSuite;
    }, rootSuite);
    lastSuite.children[browser] = {
      ...test,
      checked: true
    };
  });
  return rootSuite;
}
function getCheckedTests(suite) {
  return Object.values(suite.children).filter(_types.isDefined).flatMap(suiteOrTest => {
    if ((0, _types.isTest)(suiteOrTest)) return suiteOrTest.checked ? suiteOrTest : [];
    if (!suiteOrTest.checked && !suiteOrTest.indeterminate) return [];
    return getCheckedTests(suiteOrTest);
  });
}
function updateTestStatus(suite, path, update) {
  const title = path.shift();
  if (!title) return;
  const suiteOrTest = suite.children[title] ?? (suite.children[title] = {
    ...(path.length == 0 ? update : makeEmptySuiteNode([...suite.path, title])),
    checked: suite.checked
  });
  if ((0, _types.isTest)(suiteOrTest)) {
    const test = suiteOrTest;
    const {
      skip,
      status,
      results,
      approved
    } = update;
    if ((0, _types.isDefined)(skip)) test.skip = skip;
    if ((0, _types.isDefined)(status)) test.status = status;
    if ((0, _types.isDefined)(results)) test.results ? test.results.push(...results) : test.results = results;
    if ((0, _types.isDefined)(approved)) Object.entries(approved).forEach(_ref => {
      let [image, retry] = _ref;
      return retry !== undefined && ((test.approved = test.approved || {})[image] = retry);
    });
  } else {
    const subSuite = suiteOrTest;
    updateTestStatus(subSuite, path, update);
  }
  suite.skip = Object.values(suite.children).filter(_types.isDefined).map(_ref2 => {
    let {
      skip
    } = _ref2;
    return skip;
  }).every(Boolean);
  suite.status = Object.values(suite.children).filter(_types.isDefined).map(_ref3 => {
    let {
      status
    } = _ref3;
    return status;
  }).reduce(calcStatus);
}
function removeTests(suite, path) {
  const title = path.shift();
  if (!title) return;
  const suiteOrTest = suite.children[title];
  if (suiteOrTest && !(0, _types.isTest)(suiteOrTest)) removeTests(suiteOrTest, path);
  if ((0, _types.isTest)(suiteOrTest) || Object.keys(suiteOrTest?.children ?? {}).length == 0) delete suite.children[title];
  if (Object.keys(suite.children).length == 0) return;
  updateChecked(suite);
  suite.skip = Object.values(suite.children).filter(_types.isDefined).map(_ref4 => {
    let {
      skip
    } = _ref4;
    return skip;
  }).every(Boolean);
  suite.status = Object.values(suite.children).filter(_types.isDefined).map(_ref5 => {
    let {
      status
    } = _ref5;
    return status;
  }).reduce(calcStatus);
}
function filterTests(suite, filter) {
  const {
    status,
    subStrings
  } = filter;
  if (!status && !subStrings.length) return suite;
  const filteredSuite = {
    ...suite,
    children: {}
  };
  Object.entries(suite.children).forEach(_ref6 => {
    let [title, suiteOrTest] = _ref6;
    if (!suiteOrTest || suiteOrTest.skip) return;
    if (!status && subStrings.some(subString => title.toLowerCase().includes(subString))) {
      filteredSuite.children[title] = suiteOrTest;
    } else if ((0, _types.isTest)(suiteOrTest)) {
      if (status && suiteOrTest.status && ['pending', 'running', status].includes(suiteOrTest.status)) filteredSuite.children[title] = suiteOrTest;
    } else {
      const filteredSubSuite = filterTests(suiteOrTest, filter);
      if (Object.keys(filteredSubSuite.children).length == 0) return;
      filteredSuite.children[title] = filteredSubSuite;
    }
  });
  return filteredSuite;
}
function openSuite(suite, path, opened) {
  const subSuite = path.reduce((suiteOrTest, pathToken) => {
    if (suiteOrTest && !(0, _types.isTest)(suiteOrTest)) {
      if (opened) suiteOrTest.opened = opened;
      return suiteOrTest.children[pathToken];
    }
  }, suite);
  if (subSuite && !(0, _types.isTest)(subSuite)) subSuite.opened = opened;
}
function flattenSuite(suite) {
  if (!suite.opened) return [];
  return Object.entries(suite.children).flatMap(_ref7 => {
    let [title, subSuite] = _ref7;
    return subSuite ? [{
      title,
      suite: subSuite
    }, ...((0, _types.isTest)(subSuite) ? [] : flattenSuite(subSuite))] : [];
  });
}
function countTestsStatus(suite) {
  let successCount = 0;
  let failedCount = 0;
  let skippedCount = 0;
  let pendingCount = 0;
  const cases = Object.values(suite.children).filter(_types.isDefined);
  let suiteOrTest;
  while (suiteOrTest = cases.pop()) {
    if ((0, _types.isTest)(suiteOrTest)) {
      if (suiteOrTest.skip) skippedCount++;
      if (suiteOrTest.status === 'success') successCount++;
      if (suiteOrTest.status === 'failed') failedCount++;
      if (suiteOrTest.status === 'pending') pendingCount++;
    } else {
      cases.push(...Object.values(suiteOrTest.children).filter(_types.isDefined));
    }
  }
  return {
    successCount,
    failedCount,
    skippedCount,
    pendingCount
  };
}
function getConnectionUrl() {
  return [typeof __CREEVEY_SERVER_HOST__ == 'undefined' ? window.location.hostname : __CREEVEY_SERVER_HOST__, typeof __CREEVEY_SERVER_PORT__ == 'undefined' ? window.location.port : __CREEVEY_SERVER_PORT__].filter(Boolean).join(':');
}
function getImageUrl(path, imageName) {
  // path => [kind, story, test, browser]
  const browser = path.slice(-1)[0];
  const imagesUrl = window.location.host ? `${window.location.protocol}//${getConnectionUrl()}${window.location.pathname == '/' ? '/report' : window.location.pathname.split('/').slice(0, -1).join('/')}/${encodeURI(path.slice(0, -1).join('/'))}` : encodeURI(path.slice(0, -1).join('/'));
  return imageName == browser ? imagesUrl : `${imagesUrl}/${encodeURI(browser)}`;
}
function getBorderSize(element) {
  // NOTE Firefox returns empty string for `borderWidth` prop
  const borderSize = parseFloat(getComputedStyle(element).borderTopWidth);
  return Number.isNaN(borderSize) ? 0 : borderSize;
}
function useLoadImages(s1, s2, s3) {
  const [loaded, setLoaded] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    setLoaded(false);
    void Promise.all([s1, s2, s3].map(url => new Promise(resolve => {
      const image = document.createElement('img');
      image.src = url;
      image.onload = resolve;
      image.onerror = resolve;
    }))).then(() => setLoaded(true));
  }, [s1, s2, s3]);
  return loaded;
}

/**
 * Uses the ResizeObserver API to observe changes within the given HTML Element DOM Rect.
 *
 * @returns dimensions of element's content box (which means without paddings and border width)
 */
function useResizeObserver(elementRef, onResize) {
  let debounceTimeout = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 16;
  const observerRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (!elementRef.current) return;
    observerRef.current = new ResizeObserver(onResize);
    observerRef.current.observe(elementRef.current);
    return () => observerRef.current?.disconnect();
  }, [debounceTimeout, elementRef, onResize]);
}
function useApplyScale(imageRef, scale, dependency) {
  (0, _react.useLayoutEffect)(() => {
    if (!imageRef.current) return;
    const image = imageRef.current;
    const borderSize = getBorderSize(image);
    image.style.height = `${image.naturalHeight * scale + borderSize * 2}px`;
  }, [imageRef, scale, dependency]);
}
function useCalcScale(diffImageRef, loaded) {
  const [scale, setScale] = (0, _react.useState)(1);
  const calcScale = (0, _react.useCallback)(() => {
    const diffImage = diffImageRef.current;
    if (!diffImage || !loaded) return setScale(1);
    const borderSize = getBorderSize(diffImage);
    const ratio = (diffImage.getBoundingClientRect().width - borderSize * 2) / diffImage.naturalWidth;
    setScale(Math.min(1, ratio));
  }, [diffImageRef, loaded]);
  useResizeObserver(diffImageRef, calcScale);
  (0, _react.useLayoutEffect)(calcScale, [calcScale]);
  return scale;
}
const CREEVEY_THEME = 'Creevey_theme';
function isTheme(theme) {
  return (0, _types.isDefined)(theme) && Object.prototype.hasOwnProperty.call(_theming.themes, theme);
}
function initialTheme() {
  const theme = localStorage.getItem(CREEVEY_THEME);
  return isTheme(theme) ? theme : 'light';
}
function useTheme() {
  const [theme, setTheme] = (0, _react.useState)(initialTheme());
  (0, _react.useEffect)(() => {
    localStorage.setItem(CREEVEY_THEME, theme);
  }, [theme]);
  return [theme, setTheme];
}
function setSearchParams(testPath) {
  const pageUrl = `?${(0, _qs.stringify)({
    testPath
  })}`;
  window.history.pushState({
    testPath
  }, '', pageUrl);
}
function getTestPathFromSearch() {
  const {
    testPath
  } = (0, _qs.parse)(window.location.search.slice(1));
  //@ts-expect-error: This expression is not callable.
  if (Array.isArray(testPath) && testPath.every(token => typeof token == 'string')) {
    return testPath;
  }
  return [];
}
function useForceUpdate() {
  const [, update] = (0, _react.useState)({});
  return (0, _react.useCallback)(() => update({}), []);
}