import * as A from "fp-ts-esm/Array";
import * as E from "fp-ts-esm/Either";
import { flow, identity, pipe } from "fp-ts-esm/function";
import { Node_Type } from "./../../../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { extract } from "../../../../RicosContentAPI/extract.js";
import { modify } from "../../../../RicosContentAPI/modify.js";
import { createNode, createParagraphNode, partitionBy } from "../../../node-utils.js";
const decomposeListItem = (node) => partitionBy(
  ({ type }) => ![
    Node_Type.PARAGRAPH,
    Node_Type.ORDERED_LIST,
    Node_Type.BULLETED_LIST,
    Node_Type.CHECKBOX_LIST
  ].includes(type),
  // anything but paragraph or list is a separator
  ({ type }) => type === Node_Type.BULLETED_LIST,
  // partition is a bullet list
  identity,
  // separator added as is
  () => createNode(Node_Type.BULLETED_LIST, {
    nodes: [createNode(Node_Type.LIST_ITEM, { nodes: [], data: {} })],
    data: {}
  }),
  // partition initialization
  (list, paragraphOrList) => list.nodes?.[0]?.nodes?.push(paragraphOrList)
)(node.nodes?.[0]?.nodes || []);
const decomposeUnsupportedListItems = (content) => modify(content).filter(({ type }) => type === Node_Type.ORDERED_LIST).filter(
  ({ nodes }) => Number(
    (nodes?.length === 1 && nodes?.[0]?.nodes?.filter(
      ({ type }) => ![
        Node_Type.PARAGRAPH,
        Node_Type.ORDERED_LIST,
        Node_Type.BULLETED_LIST,
        Node_Type.CHECKBOX_LIST
      ].includes(type)
    )?.length) ?? 0
  ) > 0
).set(decomposeListItem);
const splitList = (list) => partitionBy(
  (li) => (li.nodes?.filter(
    ({ type }) => ![
      Node_Type.PARAGRAPH,
      Node_Type.ORDERED_LIST,
      Node_Type.BULLETED_LIST,
      Node_Type.CHECKBOX_LIST
    ].includes(type)
  )?.length ?? 0) > 0,
  // separator is a list item with unsupported nodes
  (n) => n.type === Node_Type.BULLETED_LIST,
  // partition is an empty bullet list
  (n) => createNode(Node_Type.ORDERED_LIST, { nodes: [n], data: {} }),
  // separator list item wrapped with ordered list
  () => createNode(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) => extract(content?.nodes ?? []).filter(
  ({ type }) => type === Node_Type.BULLETED_LIST || type === Node_Type.CHECKBOX_LIST || type === Node_Type.ORDERED_LIST
).map(({ id, nodes }) => ({ id, nodes: (nodes ?? []).flatMap((n) => n.nodes ?? []) })).filter(
  ({ nodes }) => nodes.filter(
    ({ type }) => ![
      Node_Type.PARAGRAPH,
      Node_Type.ORDERED_LIST,
      Node_Type.BULLETED_LIST,
      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(
    modify(content).filter(({ id }) => listToSplitIds.includes(id)).set(splitList)
  ) : E.left(content);
};
const newLineP = {
  type: Node_Type.TEXT,
  id: "",
  nodes: [],
  textData: { text: "\n", decorations: [] }
};
const mergeAdjacentParagraphs = (listItem) => ({
  ...listItem,
  nodes: partitionBy(
    ({ type }) => type !== Node_Type.PARAGRAPH,
    // any non-paragraph is separator
    ({ type }) => type === Node_Type.PARAGRAPH,
    // paragraph is partition
    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) => modify(content).filter(({ type }) => type === Node_Type.LIST_ITEM).filter(
  ({ nodes }) => (nodes?.filter(({ type }) => type === Node_Type.PARAGRAPH).length ?? 0) > 1
).set(mergeAdjacentParagraphs);
const wrapTopLevelTextInParagraphs = (content) => {
  const nodes = pipe(
    content.nodes,
    A.map((node) => {
      if (node.type === Node_Type.TEXT && node.textData?.text?.trim() !== "") {
        return createParagraphNode([node]);
      }
      return node;
    })
  );
  return {
    ...content,
    nodes
  };
};
const postprocess = flow(
  mergeListParagraphNodes,
  splitUnsupportedLists,
  E.map(decomposeUnsupportedListItems),
  E.fold(identity, identity),
  wrapTopLevelTextInParagraphs
);
export {
  postprocess
};
//# sourceMappingURL=postprocess.js.map
