"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const index = require("../../../ContemberEditor/index.cjs");
const ParagraphElement = require("./ParagraphElement.cjs");
const ParagraphHtmlDeserializer = require("./ParagraphHtmlDeserializer.cjs");
const withParagraphs = (editor) => {
  const {
    canToggleElement,
    deleteBackward
  } = editor;
  editor.registerElement(ParagraphElement.paragraphElementPlugin);
  editor.canToggleElement = (elementType, suchThat) => {
    if (elementType !== ParagraphElement.paragraphElementType || suchThat === void 0) {
      return canToggleElement(elementType, suchThat);
    }
    const isNumbered = suchThat.isNumbered;
    if (isNumbered !== true) {
      return canToggleElement(elementType, suchThat);
    }
    if (!editor.selection) {
      return false;
    }
    const closestBlockEntry = index.ContemberEditor.closestBlockEntry(editor);
    if (closestBlockEntry === void 0) {
      return true;
    }
    const [closestBlockElement, closestBlockPath] = closestBlockEntry;
    return closestBlockPath.length === 1 && Slate.Element.isElement(closestBlockElement) && closestBlockElement.type === ParagraphElement.paragraphElementType;
  };
  editor.deleteBackward = (unit) => {
    const selection = editor.selection;
    if (unit !== "character" || !selection || !Slate.Range.isCollapsed(selection) || selection.focus.offset !== 0) {
      return deleteBackward(unit);
    }
    const closestNumberedEntry = Slate.Editor.above(editor, {
      match: (node) => Slate.Element.isElement(node) && ParagraphElement.isParagraphElement(node, { isNumbered: true })
    });
    if (closestNumberedEntry === void 0) {
      return deleteBackward(unit);
    }
    const [, paragraphPath] = closestNumberedEntry;
    const paragraphStartPoint = Slate.Editor.start(editor, paragraphPath);
    if (!Slate.Point.equals(paragraphStartPoint, selection.focus)) {
      return deleteBackward(unit);
    }
    Slate.Transforms.setNodes(
      editor,
      { isNumbered: null },
      // null removes the key altogether
      { at: paragraphPath }
    );
  };
  editor.htmlDeserializer.registerPlugin(ParagraphHtmlDeserializer.paragraphHtmlDeserializer);
  return editor;
};
exports.withParagraphs = withParagraphs;
//# sourceMappingURL=withParagraphs.cjs.map
