import { Node_Type } from "./../../../ricos-schema/legacy/wix/rich_content/v1/index.js";
import { findParentNodeClosestToPos } from "./../../../tiptap/index.js";
import { TextSelection } from "./../../../tiptap/pm/state.js";
const changeIndentationOfEveryListSeparately = (tr, changeListChildrenIndent) => {
  const validNodeTypes = [Node_Type.BULLETED_LIST, Node_Type.CHECKBOX_LIST, Node_Type.ORDERED_LIST];
  const alreadyChangedIndent = [];
  tr.doc.nodesBetween(tr.selection.from, tr.selection.to, (node, pos) => {
    if (!validNodeTypes.includes(node.type.name)) {
      return;
    }
    let needsToBeChangedIndent = false;
    node.forEach((child, childPos) => {
      if (needsToBeChangedIndent) {
        return;
      }
      const paragraphNode = child.firstChild;
      if (!paragraphNode) {
        return;
      }
      const paragraphNodeStart = pos + childPos + 2;
      const paragraphNodeEnd = paragraphNodeStart + paragraphNode.nodeSize;
      needsToBeChangedIndent = paragraphNodeEnd >= tr.selection.from && paragraphNodeStart < tr.selection.to;
    });
    if (!needsToBeChangedIndent) {
      return;
    }
    const newPos = getNewPos(tr, node.attrs.id);
    const parentListItem = findParentNodeClosestToPos(
      tr.doc.resolve(newPos),
      (node2) => node2.type?.name === Node_Type.LIST_ITEM
    );
    if (parentListItem && alreadyChangedIndent.includes(parentListItem.node.attrs.id)) {
      return false;
    }
    const alreadyChangedIndentListItems = changeListChildrenIndent(tr, { node, pos: newPos });
    alreadyChangedIndentListItems.forEach(({ node: node2 }) => {
      alreadyChangedIndent.push(node2.attrs.id);
    });
  });
  return true;
};
const changeIndentationOfNotSelectedNodes = (tr, notSelectedListItems, changeListItemIndent) => {
  let currParent = null;
  let siblings = [];
  notSelectedListItems.forEach(({ node }, i) => {
    const newPos = getNewPos(tr, node.attrs.id);
    const parentList = findParentList(tr.doc.resolve(newPos));
    if (!parentList) {
      return;
    }
    if (!currParent) {
      currParent = parentList;
    }
    if (currParent?.pos === parentList.pos) {
      siblings.push({ node, pos: newPos });
    } else {
      changeListItemIndent(tr, siblings, currParent);
      siblings = [];
      siblings.push({ node, pos: newPos });
      currParent = parentList;
    }
    if (i === notSelectedListItems.length - 1) {
      changeListItemIndent(tr, siblings, currParent);
    }
  });
};
const getNotSelectedDescendantListItems = (tr, listItemsInSelection) => {
  const notSelectedDescendantListItems = [];
  const alreadyFoundParentPos = [];
  let currentParentRange = null;
  listItemsInSelection.forEach((listItem) => {
    listItem.node.descendants((node, pos) => {
      const startInDoc = pos + getNewPos(tr, listItem.node.attrs.id) + 1;
      if (node.type.name !== Node_Type.LIST_ITEM || startInDoc < tr.selection.to) {
        return;
      }
      const isNodeInFoundParents = alreadyFoundParentPos.some(
        ({ from, to }) => startInDoc > from && startInDoc < to
      );
      if (isNodeInFoundParents) {
        return false;
      }
      const shouldAddToList = startInDoc > tr.selection.to;
      if (shouldAddToList) {
        currentParentRange = { from: startInDoc, to: startInDoc + node.nodeSize };
        notSelectedDescendantListItems.push({ node, pos: startInDoc });
      }
      if (shouldAddToList && currentParentRange) {
        alreadyFoundParentPos.push(currentParentRange);
      }
    });
  });
  return notSelectedDescendantListItems;
};
const getListItemsInSelection = (tr, parentList) => {
  const listItemsInSelection = [];
  const firstListItemInSelection = findParentNodeClosestToPos(
    tr.selection.$from,
    (node) => node.type?.name === Node_Type.LIST_ITEM
  );
  if (!firstListItemInSelection) {
    return listItemsInSelection;
  }
  parentList.node.descendants((node, pos) => {
    if (node.type.name !== Node_Type.LIST_ITEM) {
      return false;
    }
    const startInDoc = pos + parentList.pos + 1;
    if (startInDoc >= firstListItemInSelection.pos && startInDoc < tr.selection.to) {
      listItemsInSelection.push({ node, pos: startInDoc });
    }
  });
  return listItemsInSelection;
};
const getIndentationsInSelection = (tr) => {
  let smallestIndentation = Infinity;
  let biggestIndentation = -Infinity;
  const listNodes = getListItemsWithVisibleIndentation(tr.doc);
  listNodes.forEach(({ node, visibleIndentation, pos }) => {
    node.forEach((childNode, childPos) => {
      childNode.forEach((grandChildNode, grandChildPos) => {
        if (grandChildNode.type.name === Node_Type.BULLETED_LIST || grandChildNode.type.name === Node_Type.CHECKBOX_LIST || grandChildNode.type.name === Node_Type.ORDERED_LIST) {
          return;
        }
        const grandChildNodeStart = pos + childPos + grandChildPos + 2;
        const grandChildNodeEnd = grandChildNodeStart + grandChildNode.nodeSize;
        if (grandChildNodeStart <= tr.selection.to && grandChildNodeEnd >= tr.selection.from) {
          biggestIndentation = Math.max(biggestIndentation, visibleIndentation);
          smallestIndentation = Math.min(smallestIndentation, visibleIndentation);
        }
      });
    });
  });
  return { smallestIndentation, biggestIndentation };
};
const getListItemsWithVisibleIndentation = (doc) => {
  const listItemsWithVisibleIndent = [];
  const listTypes = [
    Node_Type.BULLETED_LIST,
    Node_Type.CHECKBOX_LIST,
    Node_Type.ORDERED_LIST
  ];
  let listNodes = [];
  doc.descendants((node, pos) => {
    if (!listTypes.includes(node.type.name)) {
      return;
    }
    const $pos = doc.resolve(pos);
    if ($pos.parent.type.name !== Node_Type.LIST_ITEM) {
      listNodes = [];
    }
    const listDepth = $pos.depth === 0 ? 0 : $pos.depth / 2;
    listNodes = listNodes.filter((prevNode) => prevNode.depth < listDepth);
    listNodes.push({
      offset: node.attrs.offset,
      depth: listDepth,
      node
    });
    const offsetSum = listNodes.map((listNode) => listNode.offset).reduce((acc, sum) => acc + sum, 0);
    const level = listNodes.length - 1;
    const visibleIndentation = offsetSum + level;
    listItemsWithVisibleIndent.push({ node, visibleIndentation, pos });
  });
  return listItemsWithVisibleIndent;
};
const getNewPos = (tr, id) => {
  let newPos = 0;
  tr.doc.descendants((node, pos) => {
    if (node.attrs.id === id) {
      newPos = pos;
    }
  });
  return newPos;
};
const findParentList = ($pos) => {
  const validNodeTypes = [Node_Type.BULLETED_LIST, Node_Type.CHECKBOX_LIST, Node_Type.ORDERED_LIST];
  return findParentNodeClosestToPos(
    $pos,
    (node) => validNodeTypes.includes(node.type.name)
  );
};
const fixTextSelection = (tr, selection) => {
  tr.setSelection(
    TextSelection.between(tr.doc.resolve(selection.from), tr.doc.resolve(selection.to))
  );
};
export {
  changeIndentationOfEveryListSeparately,
  changeIndentationOfNotSelectedNodes,
  findParentList,
  fixTextSelection,
  getIndentationsInSelection,
  getListItemsInSelection,
  getListItemsWithVisibleIndentation,
  getNotSelectedDescendantListItems
};
//# sourceMappingURL=commons.js.map
