"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 indent_list_exports = {};
__export(indent_list_exports, {
  indentList: () => indentList,
  sinkListItemNodes: () => sinkListItemNodes
});
module.exports = __toCommonJS(indent_list_exports);
var import_model = require("./../../../tiptap/pm/model.js");
var import_wix_rich_content_common = require("./../../../wix-rich-content-common/index.js");
var import_commons = require("./commons.js");
var import_lift_list_item_nodes = require("./lift-list-item-nodes.js");
const indentList = () => ({ tr }) => {
  const { biggestIndentation } = (0, import_commons.getIndentationsInSelection)(tr);
  if (biggestIndentation && biggestIndentation >= import_wix_rich_content_common.MAX_LIST_VISIBLE_INDENTATION) {
    return false;
  }
  return sinkListItem(tr);
};
const sinkListItem = (tr) => {
  const parentList = (0, import_commons.findParentList)(tr.selection.$from);
  if (!parentList) {
    return false;
  }
  const multipleListsInSelection = tr.selection.to > parentList.pos + parentList.node.nodeSize;
  if (multipleListsInSelection) {
    (0, import_commons.changeIndentationOfEveryListSeparately)(tr, sinkChildrenOfList);
    return true;
  }
  sinkChildrenOfList(tr, parentList);
  return true;
};
const sinkChildrenOfList = (tr, parentList) => {
  const listItemsInSelection = (0, import_commons.getListItemsInSelection)(tr, parentList);
  if (!listItemsInSelection.length) {
    return listItemsInSelection;
  }
  sinkListItemNodes(tr, listItemsInSelection, parentList);
  const notSelectedDescendantListItems = (0, import_commons.getNotSelectedDescendantListItems)(
    tr,
    listItemsInSelection
  );
  if (!notSelectedDescendantListItems.length) {
    return listItemsInSelection;
  }
  (0, import_commons.changeIndentationOfNotSelectedNodes)(tr, notSelectedDescendantListItems, import_lift_list_item_nodes.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 = import_model.Fragment.from(
    parentList.node.type.create(
      { ...parentList.node.attrs, offset: parentList.node.attrs.offset + 1 },
      import_model.Fragment.from(listItemsToSink.map(({ node }) => node))
    )
  );
  tr.delete(start, end).insert(parentList.pos, fragment);
  (0, import_commons.fixTextSelection)(tr, preChangeSelection);
};
const semanticSink = (tr, parentList, listItemsToSink) => {
  const preChangeSelection = tr.selection;
  const slice = new import_model.Slice(
    import_model.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);
  (0, import_commons.fixTextSelection)(tr, preChangeSelection);
};
//# sourceMappingURL=indent-list.js.map
