import { ContemberEditor } from "../../ContemberEditor/index.js";
import { Editor, Transforms, Text, Element, Path } from "slate";
import { isInReferenceElement } from "../utils/isInReferenceElement.js";
import { isElementWithReference } from "../elements/ElementWithReference.js";
const overrideInsertBreak = (editor, options) => {
  const { insertBreak } = editor;
  editor.insertBreak = () => {
    const closestBlockEntry = ContemberEditor.closestBlockEntry(editor);
    if (closestBlockEntry && isElementWithReference(closestBlockEntry[0])) {
      const selection = editor.selection;
      const [, closestBlockPath] = closestBlockEntry;
      const [referenceStart, referenceEnd] = Editor.edges(editor, closestBlockPath);
      if (!selection) {
        return;
      }
      return Editor.withoutNormalizing(editor, () => {
        Transforms.wrapNodes(editor, editor.createDefaultElement([]), {
          at: {
            anchor: referenceStart,
            focus: referenceEnd
          },
          match: (node) => Text.isText(node) || Element.isElement(node) && editor.isInline(node)
        });
        const relative = Path.relative(selection.focus.path, closestBlockPath);
        Transforms.splitNodes(editor, {
          at: {
            path: [...closestBlockPath, 0, ...relative],
            offset: selection.focus.offset
          },
          always: true
        });
      });
    }
    if (isInReferenceElement(editor)) {
      return;
    }
    return insertBreak();
  };
};
export { overrideInsertBreak };
//# sourceMappingURL=overrideInsertBreak.js.map
