"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 split_block_exports = {};
__export(split_block_exports, {
  splitBlock: () => splitBlock
});
module.exports = __toCommonJS(split_block_exports);
var import_prosemirror_state = require("prosemirror-state");
var import_prosemirror_transform = require("prosemirror-transform");
var import_default_block_at = require("../helpers/default-block-at.js");
var import_get_splitted_attributes = require("../helpers/get-splitted-attributes.js");
function ensureMarks(state, splittableMarks) {
  const marks = state.storedMarks || state.selection.$to.parentOffset && state.selection.$from.marks();
  if (marks) {
    const filteredMarks = marks.filter((mark) => splittableMarks?.includes(mark.type.name));
    state.tr.ensureMarks(filteredMarks);
  }
}
const splitBlock = ({ keepMarks = true } = {}) => ({ tr, state, dispatch, editor }) => {
  const { selection, doc } = tr;
  const { $from, $to } = selection;
  const extensionAttributes = editor.extensionManager.attributes;
  const newAttributes = (0, import_get_splitted_attributes.getSplittedAttributes)(
    extensionAttributes,
    $from.node().type.name,
    $from.node().attrs
  );
  if (selection instanceof import_prosemirror_state.NodeSelection && selection.node.isBlock) {
    if (!$from.parentOffset || !(0, import_prosemirror_transform.canSplit)(doc, $from.pos)) {
      return false;
    }
    if (dispatch) {
      if (keepMarks) {
        ensureMarks(state, editor.extensionManager.splittableMarks);
      }
      tr.split($from.pos).scrollIntoView();
    }
    return true;
  }
  if (!$from.parent.isBlock) {
    return false;
  }
  if (dispatch) {
    const atEnd = $to.parentOffset === $to.parent.content.size;
    if (selection instanceof import_prosemirror_state.TextSelection) {
      tr.deleteSelection();
    }
    const deflt = $from.depth === 0 ? void 0 : (0, import_default_block_at.defaultBlockAt)($from.node(-1).contentMatchAt($from.indexAfter(-1)));
    const attrs = removeLevelAttribute(newAttributes);
    let types = atEnd && deflt ? [
      {
        type: deflt,
        attrs
      }
    ] : void 0;
    let can = (0, import_prosemirror_transform.canSplit)(tr.doc, tr.mapping.map($from.pos), 1, types);
    if (!types && !can && (0, import_prosemirror_transform.canSplit)(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : void 0)) {
      can = true;
      types = deflt ? [
        {
          type: deflt,
          attrs: newAttributes
        }
      ] : void 0;
    }
    if (can) {
      tr.split(tr.mapping.map($from.pos), 1, types);
      if (deflt && !atEnd && !$from.parentOffset && $from.parent.type !== deflt) {
        const first = tr.mapping.map($from.before());
        const $first = tr.doc.resolve(first);
        if ($from.node(-1).canReplaceWith($first.index(), $first.index() + 1, deflt)) {
          tr.setNodeMarkup(tr.mapping.map($from.before()), deflt);
        }
      }
    }
    if (keepMarks) {
      ensureMarks(state, editor.extensionManager.splittableMarks);
    }
    tr.scrollIntoView();
  }
  return true;
};
const removeLevelAttribute = (attributes) => {
  const { level, ...attrs } = attributes;
  return attrs;
};
//# sourceMappingURL=split-block.js.map
