import { Fragment, Slice } from "./../../../tiptap/pm/model.js";
import { MAX_LIST_VISIBLE_INDENTATION } from "./../../../wix-rich-content-common/index.js";
import {
  changeIndentationOfEveryListSeparately,
  changeIndentationOfNotSelectedNodes,
  findParentList,
  fixTextSelection,
  getIndentationsInSelection,
  getListItemsInSelection,
  getNotSelectedDescendantListItems
} from "./commons.js";
import { lifListItemNodes } from "./lift-list-item-nodes.js";
const indentList = () => ({ tr }) => {
  const { biggestIndentation } = getIndentationsInSelection(tr);
  if (biggestIndentation && biggestIndentation >= MAX_LIST_VISIBLE_INDENTATION) {
    return false;
  }
  return sinkListItem(tr);
};
const sinkListItem = (tr) => {
  const parentList = findParentList(tr.selection.$from);
  if (!parentList) {
    return false;
  }
  const multipleListsInSelection = tr.selection.to > parentList.pos + parentList.node.nodeSize;
  if (multipleListsInSelection) {
    changeIndentationOfEveryListSeparately(tr, sinkChildrenOfList);
    return true;
  }
  sinkChildrenOfList(tr, parentList);
  return true;
};
const sinkChildrenOfList = (tr, parentList) => {
  const listItemsInSelection = getListItemsInSelection(tr, parentList);
  if (!listItemsInSelection.length) {
    return listItemsInSelection;
  }
  sinkListItemNodes(tr, listItemsInSelection, parentList);
  const notSelectedDescendantListItems = getNotSelectedDescendantListItems(
    tr,
    listItemsInSelection
  );
  if (!notSelectedDescendantListItems.length) {
    return listItemsInSelection;
  }
  changeIndentationOfNotSelectedNodes(tr, notSelectedDescendantListItems, lifListItemNodes);
  return listItemsInSelection;
};
const sinkListItemNodes = (tr, listItemsToSink, parentList) => {
  const shouldAllListItemsOfListBeSunk = listItemsToSink.length === parentList.node.childCount;
  const shouldFirstListItemBeSunk = listItemsToSink[0].node.attrs.id === parentList.node.firstChild?.attrs.id;
  if (shouldAllListItemsOfListBeSunk) {
    increaseListOffset(tr, parentList);
    return;
  }
  if (shouldFirstListItemBeSunk) {
    increaseOffsetWithNewParent(tr, listItemsToSink, parentList);
    return;
  }
  semanticSink(tr, parentList, listItemsToSink);
};
const increaseListOffset = (tr, parentList) => {
  const newListOffset = parentList.node.attrs.offset + 1;
  tr.setNodeAttribute(parentList.pos, "offset", newListOffset);
};
const increaseOffsetWithNewParent = (tr, listItemsToSink, parentList) => {
  const preChangeSelection = tr.selection;
  const start = listItemsToSink[0].pos;
  const end = listItemsToSink[listItemsToSink.length - 1].pos + listItemsToSink[listItemsToSink.length - 1].node.nodeSize;
  const fragment = Fragment.from(
    parentList.node.type.create(
      { ...parentList.node.attrs, offset: parentList.node.attrs.offset + 1 },
      Fragment.from(listItemsToSink.map(({ node }) => node))
    )
  );
  tr.delete(start, end).insert(parentList.pos, fragment);
  fixTextSelection(tr, preChangeSelection);
};
const semanticSink = (tr, parentList, listItemsToSink) => {
  const preChangeSelection = tr.selection;
  const slice = new Slice(
    Fragment.from(
      parentList.node.type.create(
        null,
        listItemsToSink.map((child) => child.node)
      )
    ),
    1,
    0
  );
  const start = listItemsToSink[0].pos - 1;
  const end = listItemsToSink[listItemsToSink.length - 1].pos + listItemsToSink[listItemsToSink.length - 1].node.nodeSize;
  tr.replace(start, end, slice);
  fixTextSelection(tr, preChangeSelection);
};
export {
  indentList,
  sinkListItemNodes
};
//# sourceMappingURL=indent-list.js.map
