"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 lift_list_item_nodes_exports = {};
__export(lift_list_item_nodes_exports, {
  lifListItemNodes: () => lifListItemNodes
});
module.exports = __toCommonJS(lift_list_item_nodes_exports);
var import_model = require("./../../../tiptap/pm/model.js");
var import_state = require("./../../../tiptap/pm/state.js");
var import_transform = require("./../../../tiptap/pm/transform.js");
var import_commons = require("./commons.js");
const lifListItemNodes = (tr, siblingListItems, parentList) => {
  const parentListOffset = parentList.node.attrs.offset;
  const parentListStart = parentList.pos;
  const firstSibling = siblingListItems[0];
  const lastSibling = siblingListItems[siblingListItems.length - 1];
  const siblingsRangeStart = firstSibling.pos;
  const siblingsRangeEnd = lastSibling.pos + lastSibling.node.nodeSize;
  const shouldFirstListItemBeLifted = firstSibling.node.attrs.id === parentList.node.firstChild?.attrs.id;
  if (parentListOffset === 0) {
    semanticallyLiftListItems(firstSibling.node.type, tr, siblingsRangeStart, siblingsRangeEnd);
    return true;
  }
  if (shouldFirstListItemBeLifted) {
    tr.setNodeAttribute(parentListStart, "offset", parentListOffset - 1);
    return false;
  }
  decreaseOffsetWithNewParent(tr, siblingListItems, parentList);
  return false;
};
const decreaseOffsetWithNewParent = (tr, siblingListItems, parentList) => {
  const parentListOffset = parentList.node.attrs.offset;
  const parentListStart = parentList.pos;
  const parentListEnd = parentListStart + parentList.node.nodeSize;
  const siblingsRangeStart = siblingListItems[0].pos;
  const newListNode = parentList.node.cut(siblingsRangeStart - parentList.pos);
  const sizeOfContent = newListNode.nodeSize - 2;
  const parentEndWithoutContent = parentListEnd - sizeOfContent;
  const insertFragment = import_model.Fragment.from(
    newListNode.type.create(
      {
        ...newListNode.attrs,
        offset: parentListOffset - 1
      },
      newListNode.content
    )
  );
  const preChangeSelection = tr.selection;
  tr.delete(siblingsRangeStart, parentListEnd - 1).insert(parentEndWithoutContent, insertFragment);
  (0, import_commons.fixTextSelection)(tr, preChangeSelection);
  return false;
};
const semanticallyLiftListItems = (itemType, tr, selectionStart, selectionEnd) => {
  const newSelection = import_state.TextSelection.between(
    tr.doc.resolve(selectionStart),
    tr.doc.resolve(selectionEnd)
  );
  const { $from } = newSelection;
  const range = getBlockRange(newSelection, itemType);
  if (!range) {
    return false;
  }
  if ($from.node(range.depth - 1).type === itemType) {
    return liftToOuterList(tr, itemType, range);
  }
};
const liftToOuterList = (tr, itemType, range) => {
  const end = range.end;
  const endOfList = range.$to.end(range.depth);
  if (end < endOfList) {
    tr.step(
      new import_transform.ReplaceAroundStep(
        end - 1,
        endOfList,
        end,
        endOfList,
        new import_model.Slice(import_model.Fragment.from(itemType.create(null, range.parent.copy())), 1, 0),
        1,
        true
      )
    );
    range = new import_model.NodeRange(tr.doc.resolve(range.$from.pos), tr.doc.resolve(endOfList), range.depth);
  }
  const target = (0, import_transform.liftTarget)(range);
  if (target === null) {
    return false;
  }
  tr.lift(range, target);
  tr.scrollIntoView();
  return true;
};
const getBlockRange = (selection, itemType) => {
  const { $from, $to } = selection;
  const range = $from.blockRange(
    $to,
    (node) => node.childCount > 0 && node.firstChild?.type === itemType
  );
  return range;
};
//# sourceMappingURL=lift-list-item-nodes.js.map
