"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const HorizontalRuleElement = require("./HorizontalRuleElement.cjs");
const withHorizontalRules = (editor) => {
  const { insertText } = editor;
  editor.registerElement(HorizontalRuleElement.horizontalRuleElementPlugin);
  editor.insertText = (text) => {
    insertText(text);
    if (text !== "-") {
      return;
    }
    const selection = editor.selection;
    if (!selection || !Slate.Range.isCollapsed(selection)) {
      return;
    }
    const focusPoint = selection.focus;
    if (focusPoint.offset < 3) {
      return;
    }
    const [targetTextNode, targetPath] = Slate.Editor.node(editor, focusPoint);
    if (!Slate.Text.isText(targetTextNode)) {
      return;
    }
    const slice = targetTextNode.text.slice(focusPoint.offset - 3, focusPoint.offset);
    if (slice !== "---") {
      return;
    }
    Slate.Editor.withoutNormalizing(editor, () => {
      let closestBlockEntry = Slate.Editor.above(editor, {
        at: focusPoint,
        mode: "lowest",
        match: (matchedNode) => Slate.Element.isElement(matchedNode) && !editor.isInline(matchedNode) && editor.isDefaultElement(matchedNode)
      });
      if (!closestBlockEntry) {
        return;
      }
      const [closestBlock, closestBlockPath] = closestBlockEntry;
      if (closestBlockPath.length !== 1 || Slate.Node.string(closestBlock) !== "---") {
        return;
      }
      const horizontalRule = {
        type: HorizontalRuleElement.horizontalRuleElementType,
        children: [{ text: "" }]
      };
      const newHrPath = [...closestBlockPath.slice(0, -1), closestBlockPath[closestBlockPath.length - 1] + 1];
      Slate.Transforms.insertNodes(editor, horizontalRule, {
        at: newHrPath
      });
      editor.deleteBackward("character");
      editor.deleteBackward("character");
      editor.deleteBackward("character");
      const pointAfter = Slate.Editor.after(editor, newHrPath);
      if (pointAfter) {
        Slate.Transforms.select(editor, pointAfter);
      } else {
        const targetPath2 = [...newHrPath.slice(0, -1), newHrPath[newHrPath.length - 1] + 1];
        Slate.Transforms.insertNodes(editor, editor.createDefaultElement([{ text: "" }]), {
          at: targetPath2,
          select: true
        });
      }
      closestBlockEntry = Slate.Editor.node(editor, closestBlockPath);
      if (!closestBlockEntry) {
        return;
      }
      Slate.Transforms.removeNodes(editor, {
        at: closestBlockEntry[1]
      });
    });
  };
  return editor;
};
exports.withHorizontalRules = withHorizontalRules;
//# sourceMappingURL=withHorizontalRules.cjs.map
