"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var commons_exports = {};
__export(commons_exports, {
  changeIndentationOfEveryListSeparately: () => changeIndentationOfEveryListSeparately,
  changeIndentationOfNotSelectedNodes: () => changeIndentationOfNotSelectedNodes,
  findParentList: () => findParentList,
  fixTextSelection: () => fixTextSelection,
  getIndentationsInSelection: () => getIndentationsInSelection,
  getListItemsInSelection: () => getListItemsInSelection,
  getListItemsWithVisibleIndentation: () => getListItemsWithVisibleIndentation,
  getNotSelectedDescendantListItems: () => getNotSelectedDescendantListItems
});
module.exports = __toCommonJS(commons_exports);
var import_v1 = require("./../../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_tiptap = require("./../../../tiptap/index.js");
var import_state = require("./../../../tiptap/pm/state.js");
const changeIndentationOfEveryListSeparately = (tr, changeListChildrenIndent) => {
  const validNodeTypes = [import_v1.Node_Type.BULLETED_LIST, import_v1.Node_Type.CHECKBOX_LIST, import_v1.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 = (0, import_tiptap.findParentNodeClosestToPos)(
      tr.doc.resolve(newPos),
      (node2) => node2.type?.name === import_v1.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 !== import_v1.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 = (0, import_tiptap.findParentNodeClosestToPos)(
    tr.selection.$from,
    (node) => node.type?.name === import_v1.Node_Type.LIST_ITEM
  );
  if (!firstListItemInSelection) {
    return listItemsInSelection;
  }
  parentList.node.descendants((node, pos) => {
    if (node.type.name !== import_v1.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 === import_v1.Node_Type.BULLETED_LIST || grandChildNode.type.name === import_v1.Node_Type.CHECKBOX_LIST || grandChildNode.type.name === import_v1.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 = [
    import_v1.Node_Type.BULLETED_LIST,
    import_v1.Node_Type.CHECKBOX_LIST,
    import_v1.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 !== import_v1.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 = [import_v1.Node_Type.BULLETED_LIST, import_v1.Node_Type.CHECKBOX_LIST, import_v1.Node_Type.ORDERED_LIST];
  return (0, import_tiptap.findParentNodeClosestToPos)(
    $pos,
    (node) => validNodeTypes.includes(node.type.name)
  );
};
const fixTextSelection = (tr, selection) => {
  tr.setSelection(
    import_state.TextSelection.between(tr.doc.resolve(selection.from), tr.doc.resolve(selection.to))
  );
};
//# sourceMappingURL=commons.js.map
