"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var normalize_initial_state_exports = {};
__export(normalize_initial_state_exports, {
  normalizeInitialState: () => normalizeInitialState
});
module.exports = __toCommonJS(normalize_initial_state_exports);
var import_lodash = __toESM(require("lodash"));
var import_wix_rich_content_common = require("./../../wix-rich-content-common/index.js");
var import_consts = require("../consts.js");
var import_dataNormalizers = require("./dataNormalizers/index.js");
var import_process_content_state = require("./process-content-state.js");
const dataNormalizers = {
  [import_consts.LINK_TYPE]: import_dataNormalizers.linkDataNormalizer,
  [import_consts.IMAGE_TYPE]: import_dataNormalizers.imageDataNormalizer,
  [import_consts.GALLERY_TYPE]: import_dataNormalizers.galleryDataNormalizer,
  [import_consts.VIDEO_TYPE]: import_dataNormalizers.videoDataNormalizer,
  [import_consts.VERTICAL_EMBED_TYPE]: import_dataNormalizers.verticalEmbedDataNormalizer,
  [import_consts.POLL_TYPE]: import_dataNormalizers.pollsDataNormalizer
};
const normalizeComponentData = (type, componentData, config, version) => dataNormalizers[type](componentData, config, version);
const normalizeComponentConfig = (componentData) => {
  if (componentData.config) {
    return componentData;
  }
  const config = {};
  const { alignment, size, src, oembed } = componentData;
  if (alignment) {
    delete componentData.alignment;
    config.alignment = alignment;
    config.size = "small";
  } else if (size) {
    delete componentData.size;
    if (size === "smallCenter") {
      config.size = "small";
      config.alignment = "center";
    } else if (size === "fullWidth") {
      config.size = "fullWidth";
      config.alignment = "center";
    }
  } else {
    config.size = src && src.width && src.width <= 740 ? "original" : "content";
    config.alignment = "center";
  }
  const patch = { config };
  if (oembed) {
    delete componentData.url;
    delete componentData.oembed;
    patch.src = oembed.video_url;
    patch.metadata = { oembed };
  }
  return { ...componentData, ...patch };
};
const entityTypeMap = {
  configNormalization: {
    [import_consts.IMAGE_TYPE_LEGACY]: import_consts.IMAGE_TYPE,
    [import_consts.VIDEO_TYPE_LEGACY]: import_consts.VIDEO_TYPE,
    [import_consts.SOUND_CLOUD_TYPE]: import_consts.VIDEO_TYPE
  },
  dataNormalization: {
    [import_consts.LINK_TYPE]: import_consts.LINK_TYPE,
    [import_consts.IMAGE_TYPE]: import_consts.IMAGE_TYPE,
    [import_consts.GALLERY_TYPE]: import_consts.GALLERY_TYPE,
    [import_consts.VIDEO_TYPE]: import_consts.VIDEO_TYPE,
    [import_consts.VERTICAL_EMBED_TYPE]: import_consts.VERTICAL_EMBED_TYPE,
    [import_consts.POLL_TYPE]: import_consts.POLL_TYPE
  }
};
const shouldNormalizeEntity = (entity, normalizationMap) => Object.keys(normalizationMap).includes(entity.type) && entity.data;
const shouldNormalizeEntityConfig = (entity) => shouldNormalizeEntity(entity, entityTypeMap.configNormalization);
const shouldNormalizeEntityData = (entity) => shouldNormalizeEntity(entity, entityTypeMap.dataNormalization);
const normalizeEntityMap = (entityMap, config, stateVersion) => {
  const normalizeType = (key, obj) => obj[key] || key;
  return import_lodash.default.mapValues(entityMap, (entity) => {
    let newEntity = entity;
    if (shouldNormalizeEntityConfig(entity)) {
      newEntity = {
        ...entity,
        type: normalizeType(entity.type, entityTypeMap.configNormalization),
        data: normalizeComponentConfig(entity.data)
      };
    } else if (shouldNormalizeEntityData(entity)) {
      newEntity = {
        ...entity,
        type: normalizeType(entity.type, entityTypeMap.dataNormalization),
        data: normalizeComponentData(entity.type, entity.data, config, stateVersion)
      };
    }
    if (newEntity?.data?.config && !newEntity?.data?.config?.textWrap) {
      newEntity.data.config.textWrap = import_consts.WRAP;
    }
    convertImageAnchorToLink(newEntity);
    return newEntity;
  });
};
const isImageAnchor = (entity) => entity.type === "wix-draft-plugin-image" && !!entity.data?.config?.anchor && !entity.data?.config?.link;
const convertImageAnchorToLink = (newEntity) => {
  if (isImageAnchor(newEntity)) {
    const { anchor, ...rest } = newEntity.data.config;
    newEntity.data = {
      ...newEntity.data,
      config: {
        ...rest,
        link: { anchor }
      }
    };
  }
};
const normalizeInitialState = (content, config = {}) => {
  const { blocks, entityMap, documentStyle, VERSION } = (0, import_process_content_state.processContentState)(
    import_lodash.default.cloneDeep(content),
    config
  );
  const contentState = {
    blocks,
    entityMap: normalizeEntityMap(entityMap, config, content.VERSION || "0.0.0"),
    VERSION,
    ID: content.ID || (0, import_wix_rich_content_common.uuid)()
  };
  if (!import_lodash.default.isEmpty(documentStyle)) {
    contentState.documentStyle = documentStyle;
  }
  return contentState;
};
//# sourceMappingURL=normalize-initial-state.js.map
