"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactBinding = require("@contember/react-binding");
const Slate = require("slate");
const overrideCreateElementReference = require("./overrideCreateElementReference.cjs");
const overrideInsertBreak = require("./overrideInsertBreak.cjs");
const overrideInsertData = require("./overrideInsertData.cjs");
const overrideInsertElementWithReference = require("./overrideInsertElementWithReference.cjs");
const overrideInsertNode = require("./overrideInsertNode.cjs");
const overrideRenderElement = require("./overrideRenderElement.cjs");
const ParagraphElement = require("../../plugins/element/paragraphs/ParagraphElement.cjs");
const createEditor = require("../../editorFactory/createEditor.cjs");
const ReferenceElement = require("../elements/ReferenceElement.cjs");
function _interopNamespaceDefault(e) {
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
  if (e) {
    for (const k in e) {
      if (k !== "default") {
        const d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: () => e[k]
        });
      }
    }
  }
  n.default = e;
  return Object.freeze(n);
}
const Slate__namespace = /* @__PURE__ */ _interopNamespaceDefault(Slate);
const initBlockEditor = ({ editor, ...options }) => {
  if (options.plugins && options.plugins.indexOf(ParagraphElement.paragraphElementType) === -1) {
    throw new reactBinding.BindingError(`The block editor plugin set must include the paragraph plugin!`);
  }
  return createEditor.initializeEditor({
    editor,
    plugins: options.plugins,
    augmentEditor: options.augmentEditor,
    augmentEditorBuiltins: options.augmentEditorBuiltins,
    addEditorBuiltins: (editor2) => {
      const e = editor2;
      e.registerElement(ReferenceElement.createReferenceElementPlugin(options));
      e.insertElementWithReference = () => {
        throw new reactBinding.BindingError(
          `BlockEditor: trying to insert a referenced element but referencing has not been correctly set up. Check the BlockEditor props.`
        );
      };
      e.getReferencedEntity = () => {
        throw new reactBinding.BindingError(
          `BlockEditor: trying to access a referenced entity but referencing has not been correctly set up. Check the BlockEditor props.`
        );
      };
      const { upgradeFormatBySingleVersion } = e;
      e.upgradeFormatBySingleVersion = (node, oldVersion) => {
        if (oldVersion !== 0 || !Slate.Element.isElement(node)) {
          return upgradeFormatBySingleVersion(node, oldVersion);
        }
        if (node.type === "embed" || node.type === "blockReference" || node.type === "blockVoidReference") {
          return {
            ...node,
            type: "reference",
            children: node.children.map((child) => editor2.upgradeFormatBySingleVersion(child, oldVersion))
          };
        }
        return upgradeFormatBySingleVersion(node, oldVersion);
      };
      e.slate = Slate__namespace;
      overrideCreateElementReference.overrideCreateElementReference(e, options);
      overrideInsertBreak.overrideInsertBreak(e);
      overrideInsertData.overrideInsertData(e, options);
      overrideInsertElementWithReference.overrideInsertElementWithReference(e, options);
      overrideInsertNode.overrideInsertNode(e);
      overrideRenderElement.overrideRenderElement(e);
      return e;
    }
  });
};
exports.initBlockEditor = initBlockEditor;
//# sourceMappingURL=initBlockEditor.cjs.map
