"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.serializeRawStories = exports.deserializeStory = exports.deserializeRawStories = exports.denormalizeStoryParameters = exports.combineParameters = void 0;
var _lodash = require("lodash");
var _serializeRegExp = require("./serializeRegExp");
// NOTE: Copy-paste from storybook/api
const combineParameters = function () {
  for (var _len = arguments.length, parameterSets = new Array(_len), _key = 0; _key < _len; _key++) {
    parameterSets[_key] = arguments[_key];
  }
  return (
    // eslint-disable-next-line @typescript-eslint/no-unsafe-return
    (0, _lodash.mergeWith)({}, ...parameterSets, (_, srcValue) => {
      // Treat arrays as scalars:
      if (Array.isArray(srcValue)) return srcValue;
      return undefined;
    })
  );
};

// NOTE: Copy-paste from storybook/api
exports.combineParameters = combineParameters;
const denormalizeStoryParameters = _ref => {
  let {
    globalParameters,
    kindParameters,
    stories
  } = _ref;
  return (0, _lodash.mapValues)(stories, storyData => ({
    ...storyData,
    parameters: combineParameters(globalParameters, kindParameters[storyData.kind] ?? {}, storyData.parameters)
  }));
};
exports.denormalizeStoryParameters = denormalizeStoryParameters;
const serializeRawStories = stories => {
  return (0, _lodash.mapValues)(stories, storyData => {
    const creevey = storyData.parameters.creevey;
    if (creevey?.skip) {
      return {
        ...storyData,
        parameters: {
          ...storyData.parameters,
          creevey: {
            ...creevey,
            skip: (0, _lodash.cloneDeepWith)(creevey.skip, value => {
              if ((0, _serializeRegExp.isRegExp)(value)) {
                return (0, _serializeRegExp.serializeRegExp)(value);
              }
              return undefined;
            })
          }
        }
      };
    }
    return storyData;
  });
};
exports.serializeRawStories = serializeRawStories;
const deserializeRawStories = stories => {
  return (0, _lodash.mapValues)(stories, deserializeStory);
};
exports.deserializeRawStories = deserializeRawStories;
const deserializeStory = story => {
  const creevey = story.parameters.creevey;
  if (creevey?.skip) {
    return {
      ...story,
      parameters: {
        ...story.parameters,
        creevey: {
          ...creevey,
          skip: (0, _lodash.cloneDeepWith)(creevey.skip, value => {
            if ((0, _serializeRegExp.isSerializedRegExp)(value)) {
              return (0, _serializeRegExp.deserializeRegExp)(value);
            }
            return undefined;
          })
        }
      }
    };
  }
  return story;
};
exports.deserializeStory = deserializeStory;