"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const Slate = require("slate");
const reactBinding = require("@contember/react-binding");
const slateReact = require("slate-react");
const ReferenceElementRenderer = require("../renderers/ReferenceElementRenderer.cjs");
const getDiscriminatedDatum = require("../../../discrimination/getDiscriminatedDatum.cjs");
const referenceElementType = "reference";
const isReferenceElement = (node) => Slate.Element.isElement(node) && node.type === referenceElementType;
const findElementPathFallback = (parent, element) => {
  for (const i in parent.children) {
    const child = parent.children[i];
    if (element === child) {
      return [Number(i)];
    }
    if (Slate.Element.isAncestor(child)) {
      const result = findElementPathFallback(child, element);
      if (result !== void 0) {
        return [Number(i), ...result];
      }
    }
  }
  return void 0;
};
const createReferenceElementPlugin = (args) => {
  return {
    type: referenceElementType,
    render: (props) => {
      if (!args.referenceDiscriminationField) {
        throw new Error();
      }
      return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ReferenceElementRenderer.ReferenceElementRenderer, { ...args, ...props, referenceDiscriminationField: args.referenceDiscriminationField }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/elements/ReferenceElement.tsx",
        lineNumber: 55,
        columnNumber: 11
      }, void 0);
    },
    canContainAnyBlocks: true,
    isVoid: ({ element, editor }) => {
      if (args.referenceDiscriminationField === void 0) {
        throw new reactBinding.BindingError();
      }
      const path = (() => {
        try {
          return slateReact.ReactEditor.findPath(editor, element);
        } catch (e) {
          return findElementPathFallback(editor, element);
        }
      })();
      if (!path) {
        return false;
      }
      let referencedEntity;
      try {
        referencedEntity = args.getReferencedEntity(path, element.referenceId);
      } catch (e) {
        return false;
      }
      const discriminationField = referencedEntity.getRelativeSingleField(args.referenceDiscriminationField);
      const selectedReference = getDiscriminatedDatum.getDiscriminatedDatum(args.editorReferenceBlocks, discriminationField)?.datum;
      if (selectedReference === void 0) {
        throw new reactBinding.BindingError();
      }
      return selectedReference.template === void 0;
    }
    // normalizeNode: ({ editor, element, path }) => {
    // 	const referenceId = element.referenceId
    // 	try {
    // 		args.getReferencedEntity(path, referenceId)
    // 	} catch {
    // 		console.warn(`Removing a node linking a non-existent reference id '${referenceId}'.`)
    // 		Transforms.delete(editor, { at: path })
    // 	}
    // },
  };
};
exports.createReferenceElementPlugin = createReferenceElementPlugin;
exports.isReferenceElement = isReferenceElement;
exports.referenceElementType = referenceElementType;
//# sourceMappingURL=ReferenceElement.cjs.map
