import lodash from "lodash";
import { TextStyle_TextAlignment } from "./../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import {
  COLLAPSIBLE_LIST_TYPE,
  DIVIDER_TYPE,
  EMBED_TYPE,
  LINK_PREVIEW_TYPE,
  NO_WRAP,
  TABLE_TYPE
} from "../../../consts.js";
import { generateId } from "../../generate-random-id.js";
import { TO_RICOS_DATA_FIELD, TO_RICOS_PLUGIN_TYPE } from "../consts.js";
import { convertBlockDataToRicos } from "./convert-ricos-plugin-data.js";
const supportedTextAlignments = [
  TextStyle_TextAlignment.AUTO,
  TextStyle_TextAlignment.LEFT,
  TextStyle_TextAlignment.RIGHT,
  TextStyle_TextAlignment.CENTER,
  TextStyle_TextAlignment.JUSTIFY
];
const normalizeBlockData = (type, data) => {
  switch (type) {
    case DIVIDER_TYPE: {
      data.config = data.config || { textWrap: NO_WRAP };
      return;
    }
    case TABLE_TYPE: {
      data.config.rows = Object.keys(data.config.rows).reduce((acc, key) => {
        const row = data.config.rows[key];
        acc[key] = {
          ...row,
          columns: row.columns || {
            0: {
              content: {
                blocks: [
                  {
                    key: generateId(),
                    text: "",
                    type: "unstyled",
                    depth: 0,
                    inlineStyleRanges: [],
                    entityRanges: [],
                    data: {}
                  }
                ]
              }
            }
          }
        };
        return acc;
      }, {});
      return;
    }
    default: {
    }
  }
};
const getEntity = (key, entityMap) => {
  if (!entityMap[key]) {
    console.error(`Invalid entity key '${key}' -- entry does not exist.`);
    return null;
  }
  const { type, data } = entityMap[key];
  const blockType = type === LINK_PREVIEW_TYPE && data.html ? EMBED_TYPE : type;
  const dataFieldName = TO_RICOS_DATA_FIELD[blockType];
  if (dataFieldName === void 0) {
    console.error(`Unknown entity type "${blockType}"!`);
    return null;
  }
  if (!data) {
    console.error(`no data for "${blockType}"!`);
    return null;
  }
  normalizeBlockData(type, data);
  const advancedPluginsAdditionalData = {
    [COLLAPSIBLE_LIST_TYPE]: { pairs: data?.pairs },
    [TABLE_TYPE]: { rows: data?.config?.rows }
  };
  return {
    type: TO_RICOS_PLUGIN_TYPE[blockType],
    [dataFieldName]: convertBlockDataToRicos(blockType, data),
    ...advancedPluginsAdditionalData[blockType] || {}
  };
};
const getTextStyle = (blockData) => {
  const { textAlignment, dynamicStyles } = blockData || {};
  const { "line-height": lineHeight } = dynamicStyles || {};
  const normalizedTextAlignment = textAlignment?.toUpperCase();
  return {
    textAlignment: normalizedTextAlignment && supportedTextAlignments.includes(normalizedTextAlignment) ? normalizedTextAlignment : null,
    lineHeight
  };
};
const getNodeStyle = (blockData) => {
  const { dynamicStyles } = blockData || {};
  const {
    "padding-top": paddingTop,
    "padding-bottom": paddingBottom,
    backgroundColor
  } = dynamicStyles || {};
  const style = lodash.pickBy({ paddingTop, paddingBottom, backgroundColor }, lodash.identity);
  const hasStyle = Object.values(style).length > 0;
  return hasStyle ? style : void 0;
};
export {
  getEntity,
  getNodeStyle,
  getTextStyle
};
//# sourceMappingURL=get-ricos-entity-data.js.map
