import { unorderedListElementType } from "./UnorderedListElement.js";
import { orderedListElementType } from "./OrderedListElement.js";
import { listItemElementType } from "./ListItemElement.js";
import { EditorPasteUtils } from "../../../baseEditor/EditorPasteUtils.js";
const defaultGetListElementProperties = (text) => {
  const firstChar = text[0];
  return {
    ordered: firstChar === "o" ? false : firstChar.match(/^\w$/) !== null,
    properties: {}
  };
};
const listHtmlDeserializerFactory = ({ getListElementProperties = defaultGetListElementProperties } = {}) => ({
  processBlockPaste: ({ element, next, cumulativeTextAttrs }) => {
    if (element.tagName === "UL") {
      return [{ type: unorderedListElementType, children: next(element.childNodes, cumulativeTextAttrs) }];
    }
    if (element.tagName === "OL") {
      return [{ type: orderedListElementType, children: next(element.childNodes, cumulativeTextAttrs) }];
    }
    if (element.tagName === "LI") {
      return [{ type: listItemElementType, children: next(element.childNodes, cumulativeTextAttrs) }];
    }
    return null;
  },
  processNodeListPaste: ({ nodeList, cumulativeTextAttrs, deserializer }) => {
    const result = [];
    let group = [];
    let groupWasList = false;
    let isOrdered = false;
    let currentListProperties = {};
    let includesList = false;
    let lastListId = null;
    const processGroup = () => {
      if (groupWasList) {
        return {
          elements: [
            {
              type: isOrdered ? orderedListElementType : unorderedListElementType,
              ...currentListProperties,
              children: group.map((item) => {
                return {
                  type: listItemElementType,
                  children: deserializer.deserializeBlocks(EditorPasteUtils.wordPasteListItemContent(item.childNodes), cumulativeTextAttrs)
                };
              })
            }
          ]
        };
      } else {
        return deserializer.processNodeListPaste(group, cumulativeTextAttrs);
      }
    };
    for (let i = 0; i < nodeList.length; i++) {
      const curr = nodeList[i];
      const isWhiteSpace = curr.nodeType === Node.TEXT_NODE && curr.textContent?.match(/^\s*$/) !== null;
      if (groupWasList && isWhiteSpace) {
        continue;
      }
      let isList = false;
      let listId = null;
      if (curr instanceof HTMLElement && curr.nodeName === "P") {
        const match = curr.getAttribute("style")?.match(/mso-list:(\w+ level\d+ \w+)/) ?? null;
        if (match !== null) {
          isList = true;
          listId = match[1];
          if (!groupWasList || lastListId === listId) {
            const textContent = curr.textContent;
            const specifics = getListElementProperties(textContent);
            isOrdered = specifics.ordered;
            currentListProperties = specifics.properties;
          }
        }
      }
      if (isList !== groupWasList || listId !== lastListId) {
        includesList = true;
        if (group.length > 0) {
          result.push(processGroup());
          group = [];
        }
      }
      groupWasList = isList;
      lastListId = listId;
      group.push(curr);
    }
    if (includesList) {
      result.push(processGroup());
      return EditorPasteUtils.flattenNodesWithType(result, deserializer.createDefaultElement);
    }
    return null;
  }
});
export { listHtmlDeserializerFactory };
//# sourceMappingURL=ListHtmlDeserializer.js.map
