"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 keyboard_shortcuts_exports = {};
__export(keyboard_shortcuts_exports, {
  handleCollapsibleBackspace: () => handleCollapsibleBackspace
});
module.exports = __toCommonJS(keyboard_shortcuts_exports);
var import_ricos_content = require("./../../ricos-content/index.js");
var import_tiptap = require("./../../tiptap/index.js");
var import_state = require("./../../tiptap/pm/state.js");
const jumpToCollapsibleTitle = (editor, collapsibleItemBody) => {
  if (!collapsibleItemBody) {
    return false;
  }
  editor.chain().setTextSelection(collapsibleItemBody.pos - 2).focus().run();
  return true;
};
const getCollapsibleItemBody = (editor) => (0, import_tiptap.findParentNodeClosestToPos)(
  editor.state.selection.$from,
  (node) => node.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_ITEM_BODY_TYPE
);
const getCollapsibleItemTitle = (editor) => (0, import_tiptap.findParentNodeClosestToPos)(
  editor.state.selection.$from,
  (node) => node.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_ITEM_TITLE_TYPE
);
const getCollapsibleItem = (editor) => (0, import_tiptap.findParentNodeClosestToPos)(
  editor.state.selection.$from,
  (node) => node.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_ITEM_TYPE
);
const handleCollapsibleBackspace = (editor, event) => {
  const { selection } = editor.state;
  if (selection instanceof import_state.NodeSelection && selection.node.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_ITEM_TYPE) {
    const $pos = editor.state.doc.resolve(selection.from);
    const parentList = (0, import_tiptap.findParentNodeClosestToPos)(
      $pos,
      (node) => node.type.name === import_ricos_content.TIPTAP_COLLAPSIBLE_LIST_TYPE
    );
    if (parentList && parentList.node.childCount <= 1) {
      editor.chain().deleteRange({
        from: parentList.pos,
        to: parentList.pos + parentList.node.nodeSize
      }).focus().run();
      event?.preventDefault();
      event?.stopImmediatePropagation();
      return "handled";
    }
    const { from, to } = selection;
    editor.chain().deleteRange({ from, to }).focus().run();
    event?.preventDefault();
    event?.stopImmediatePropagation();
    return "handled";
  }
  if (!selection.empty) {
    return "continue";
  }
  const collapsibleItemBody = getCollapsibleItemBody(editor);
  const isAtBodyStart = selection.from - 1 === collapsibleItemBody?.start;
  if (isAtBodyStart) {
    jumpToCollapsibleTitle(editor, collapsibleItemBody);
    event?.preventDefault();
    event?.stopImmediatePropagation();
    return "handled";
  }
  const titleNode = getCollapsibleItemTitle(editor);
  const isAtTitleStart = selection.from - 1 === titleNode?.start;
  if (isAtTitleStart) {
    const collapsibleItem = getCollapsibleItem(editor);
    if (!collapsibleItem) {
      return "continue";
    }
    editor.commands.setNodeSelection(collapsibleItem.pos);
    event?.preventDefault();
    event?.stopImmediatePropagation();
    return "handled";
  }
  return "continue";
};
//# sourceMappingURL=keyboard-shortcuts.js.map
