"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 postprocess_exports = {};
__export(postprocess_exports, {
  postprocess: () => postprocess
});
module.exports = __toCommonJS(postprocess_exports);
var A = __toESM(require("fp-ts-esm/Array"));
var E = __toESM(require("fp-ts-esm/Either"));
var import_function = require("fp-ts-esm/function");
var import_v1 = require("./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_extract = require("../../../../RicosContentAPI/extract.js");
var import_modify = require("../../../../RicosContentAPI/modify.js");
var import_node_utils = require("../../../node-utils.js");
const decomposeListItem = (node) => (0, import_node_utils.partitionBy)(
  ({ type }) => ![
    import_v1.Node_Type.PARAGRAPH,
    import_v1.Node_Type.ORDERED_LIST,
    import_v1.Node_Type.BULLETED_LIST,
    import_v1.Node_Type.CHECKBOX_LIST
  ].includes(type),
  // anything but paragraph or list is a separator
  ({ type }) => type === import_v1.Node_Type.BULLETED_LIST,
  // partition is a bullet list
  import_function.identity,
  // separator added as is
  () => (0, import_node_utils.createNode)(import_v1.Node_Type.BULLETED_LIST, {
    nodes: [(0, import_node_utils.createNode)(import_v1.Node_Type.LIST_ITEM, { nodes: [], data: {} })],
    data: {}
  }),
  // partition initialization
  (list, paragraphOrList) => list.nodes?.[0]?.nodes?.push(paragraphOrList)
)(node.nodes?.[0]?.nodes || []);
const decomposeUnsupportedListItems = (content) => (0, import_modify.modify)(content).filter(({ type }) => type === import_v1.Node_Type.ORDERED_LIST).filter(
  ({ nodes }) => Number(
    (nodes?.length === 1 && nodes?.[0]?.nodes?.filter(
      ({ type }) => ![
        import_v1.Node_Type.PARAGRAPH,
        import_v1.Node_Type.ORDERED_LIST,
        import_v1.Node_Type.BULLETED_LIST,
        import_v1.Node_Type.CHECKBOX_LIST
      ].includes(type)
    )?.length) ?? 0
  ) > 0
).set(decomposeListItem);
const splitList = (list) => (0, import_node_utils.partitionBy)(
  (li) => (li.nodes?.filter(
    ({ type }) => ![
      import_v1.Node_Type.PARAGRAPH,
      import_v1.Node_Type.ORDERED_LIST,
      import_v1.Node_Type.BULLETED_LIST,
      import_v1.Node_Type.CHECKBOX_LIST
    ].includes(type)
  )?.length ?? 0) > 0,
  // separator is a list item with unsupported nodes
  (n) => n.type === import_v1.Node_Type.BULLETED_LIST,
  // partition is an empty bullet list
  (n) => (0, import_node_utils.createNode)(import_v1.Node_Type.ORDERED_LIST, { nodes: [n], data: {} }),
  // separator list item wrapped with ordered list
  () => (0, import_node_utils.createNode)(import_v1.Node_Type.BULLETED_LIST, { nodes: [], data: {} }),
  // new partition
  (list2, item) => list2.nodes?.push(item)
  // add valid list item to partition
)(list.nodes ?? []);
const getListToSplitIds = (content) => (0, import_extract.extract)(content?.nodes ?? []).filter(
  ({ type }) => type === import_v1.Node_Type.BULLETED_LIST || type === import_v1.Node_Type.CHECKBOX_LIST || type === import_v1.Node_Type.ORDERED_LIST
).map(({ id, nodes }) => ({ id, nodes: (nodes ?? []).flatMap((n) => n.nodes ?? []) })).filter(
  ({ nodes }) => nodes.filter(
    ({ type }) => ![
      import_v1.Node_Type.PARAGRAPH,
      import_v1.Node_Type.ORDERED_LIST,
      import_v1.Node_Type.BULLETED_LIST,
      import_v1.Node_Type.CHECKBOX_LIST
    ].includes(type)
  ).length > 0
).map(({ id }) => id).get();
const splitUnsupportedLists = (content) => {
  const listToSplitIds = getListToSplitIds(content);
  return listToSplitIds.length > 0 ? E.right(
    (0, import_modify.modify)(content).filter(({ id }) => listToSplitIds.includes(id)).set(splitList)
  ) : E.left(content);
};
const newLineP = {
  type: import_v1.Node_Type.TEXT,
  id: "",
  nodes: [],
  textData: { text: "\n", decorations: [] }
};
const mergeAdjacentParagraphs = (listItem) => ({
  ...listItem,
  nodes: (0, import_node_utils.partitionBy)(
    ({ type }) => type !== import_v1.Node_Type.PARAGRAPH,
    // any non-paragraph is separator
    ({ type }) => type === import_v1.Node_Type.PARAGRAPH,
    // paragraph is partition
    import_function.identity,
    // seprator added as is to result partition array
    (paragraph) => ({ ...paragraph, nodes: [] }),
    // empty paragraph is a new partition
    // paragraph nodes appended to partition nodes, separated by \n
    (containerP, mergedP) => containerP.nodes = (containerP.nodes || []).concat(containerP.nodes?.length ? [newLineP] : []).concat(mergedP.nodes ?? [])
  )(listItem.nodes ?? [])
});
const mergeListParagraphNodes = (content) => (0, import_modify.modify)(content).filter(({ type }) => type === import_v1.Node_Type.LIST_ITEM).filter(
  ({ nodes }) => (nodes?.filter(({ type }) => type === import_v1.Node_Type.PARAGRAPH).length ?? 0) > 1
).set(mergeAdjacentParagraphs);
const wrapTopLevelTextInParagraphs = (content) => {
  const nodes = (0, import_function.pipe)(
    content.nodes,
    A.map((node) => {
      if (node.type === import_v1.Node_Type.TEXT && node.textData?.text?.trim() !== "") {
        return (0, import_node_utils.createParagraphNode)([node]);
      }
      return node;
    })
  );
  return {
    ...content,
    nodes
  };
};
const postprocess = (0, import_function.flow)(
  mergeListParagraphNodes,
  splitUnsupportedLists,
  E.map(decomposeUnsupportedListItems),
  E.fold(import_function.identity, import_function.identity),
  wrapTopLevelTextInParagraphs
);
//# sourceMappingURL=postprocess.js.map
