import { Element, Range, Editor, Point, Transforms } from "slate";
import { ContemberEditor } from "../../../ContemberEditor/index.js";
import { paragraphElementPlugin, paragraphElementType, isParagraphElement } from "./ParagraphElement.js";
import { paragraphHtmlDeserializer } from "./ParagraphHtmlDeserializer.js";
const withParagraphs = (editor) => {
  const {
    canToggleElement,
    deleteBackward
  } = editor;
  editor.registerElement(paragraphElementPlugin);
  editor.canToggleElement = (elementType, suchThat) => {
    if (elementType !== 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 = ContemberEditor.closestBlockEntry(editor);
    if (closestBlockEntry === void 0) {
      return true;
    }
    const [closestBlockElement, closestBlockPath] = closestBlockEntry;
    return closestBlockPath.length === 1 && Element.isElement(closestBlockElement) && closestBlockElement.type === paragraphElementType;
  };
  editor.deleteBackward = (unit) => {
    const selection = editor.selection;
    if (unit !== "character" || !selection || !Range.isCollapsed(selection) || selection.focus.offset !== 0) {
      return deleteBackward(unit);
    }
    const closestNumberedEntry = Editor.above(editor, {
      match: (node) => Element.isElement(node) && isParagraphElement(node, { isNumbered: true })
    });
    if (closestNumberedEntry === void 0) {
      return deleteBackward(unit);
    }
    const [, paragraphPath] = closestNumberedEntry;
    const paragraphStartPoint = Editor.start(editor, paragraphPath);
    if (!Point.equals(paragraphStartPoint, selection.focus)) {
      return deleteBackward(unit);
    }
    Transforms.setNodes(editor, { isNumbered: null }, { at: paragraphPath });
  };
  editor.htmlDeserializer.registerPlugin(paragraphHtmlDeserializer);
  return editor;
};
export { withParagraphs };
//# sourceMappingURL=withParagraphs.js.map
