"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const ui = require("@contember/ui");
const react = require("react");
const Slate = require("slate");
const slateReact = require("slate-react");
const getDiscriminatedDatum = require("../../../discrimination/getDiscriminatedDatum.cjs");
const getDiscriminatedBlock = require("../../../blocks/getDiscriminatedBlock.cjs");
const BlockElement = require("../../baseEditor/BlockElement.cjs");
const ReferenceElementRenderer = react.memo((props) => {
  const editor = slateReact.useSlateStatic();
  const selected = slateReact.useSelected();
  const referencedEntity = reactBinding.useEntity();
  const discriminationField = referencedEntity.getRelativeSingleField(props.referenceDiscriminationField);
  const selectedReference = getDiscriminatedDatum.getDiscriminatedDatum(props.editorReferenceBlocks, discriminationField)?.datum;
  if (!selectedReference) {
    throw new reactBinding.BindingError(`BlockEditor: Trying to render a reference with an undefined reference type.`);
  }
  const elementTemplate = selectedReference.template;
  const isEditable = elementTemplate !== void 0;
  const onContainerClick = react.useCallback(
    (e) => {
      if (e.target === e.currentTarget) {
        const path = slateReact.ReactEditor.findPath(editor, props.element);
        Slate.Transforms.select(editor, path);
      }
    },
    [editor, props.element]
  );
  const onRemove = react.useCallback(() => {
    const path = slateReact.ReactEditor.findPath(editor, props.element);
    Promise.resolve().then(() => {
      Slate.Transforms.removeNodes(editor, {
        at: path
      });
    });
  }, [editor, props.element]);
  let blockBody;
  let renderedBlock = selectedReference;
  let isFullWidth = true;
  if (elementTemplate !== void 0) {
    const contentTemplate = elementTemplate.blockContent;
    if (contentTemplate === void 0) {
      throw new reactBinding.BindingError(
        `BlockEditor: All block reference elements must at least use the BlockEditor.ContentOutlet component!

It appears that other BlockEditor._____ components have been used but the BlockEditor.ContentOutlet is missing or isn't at the very top level.`
      );
    }
    const placeholder = contentTemplate.value.placeholder;
    const showPlaceholder = placeholder !== void 0 && props.element.children.length === 1 && props.element.children[0]?.text === "";
    blockBody = /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
      !!contentTemplate.nodeBefore && /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { contentEditable: false, children: contentTemplate.nodeBefore }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
        lineNumber: 72,
        columnNumber: 38
      }, void 0),
      showPlaceholder ? /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { style: { position: "relative" }, children: [
        /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { style: { position: "absolute", pointerEvents: "none" }, contentEditable: false, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.EditorPlaceholder, { children: placeholder }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
          lineNumber: 77,
          columnNumber: 9
        }, void 0) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
          lineNumber: 76,
          columnNumber: 8
        }, void 0),
        /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { children: props.children }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
          lineNumber: 79,
          columnNumber: 8
        }, void 0)
      ] }, void 0, true, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
        lineNumber: 75,
        columnNumber: 7
      }, void 0) : /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { children: props.children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
        lineNumber: 82,
        columnNumber: 7
      }, void 0),
      !!contentTemplate.nodeAfter && /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { contentEditable: false, children: contentTemplate.nodeAfter }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
        lineNumber: 85,
        columnNumber: 37
      }, void 0)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
      lineNumber: 71,
      columnNumber: 4
    }, void 0);
  } else if (props.embedReferenceDiscriminateBy !== void 0 && discriminationField.hasValue(props.embedReferenceDiscriminateBy)) {
    if (props.embedContentDiscriminationField === void 0 || props.embedHandlers === void 0 || props.embedSubBlocks === void 0) {
      throw new reactBinding.BindingError(
        `BlockEditor: Trying to render an embed element without all the correct settings. Check the related BlockEditor props.`
      );
    }
    const embedTypeDiscriminant = referencedEntity.getRelativeSingleField(props.embedContentDiscriminationField);
    const embedHandler = getDiscriminatedDatum.getDiscriminatedDatum(props.embedHandlers, embedTypeDiscriminant);
    const subBlock = getDiscriminatedBlock.getDiscriminatedBlock(props.embedSubBlocks, embedTypeDiscriminant);
    if (embedHandler === void 0) {
      throw new reactBinding.BindingError(`BlockEditor: Missing handler for embed of type '${embedTypeDiscriminant.value}'.`);
    }
    if (subBlock === void 0) {
      throw new reactBinding.BindingError(`BlockEditor: Missing Block for embed of type '${embedTypeDiscriminant.value}'.`);
    }
    isFullWidth = false;
    renderedBlock = subBlock.datum;
    blockBody = /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      "div",
      {
        style: { display: "flex" },
        children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(EmbedElementRendererInner, { handler: embedHandler.datum }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
          lineNumber: 120,
          columnNumber: 5
        }, void 0)
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
        lineNumber: 115,
        columnNumber: 4
      },
      void 0
    );
  } else {
    blockBody = renderedBlock.children;
  }
  const alternate = renderedBlock.alternate ? /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Box, { children: renderedBlock.alternate }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
    lineNumber: 127,
    columnNumber: 46
  }, void 0) : void 0;
  const wrappedReference = /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Entity, { accessor: referencedEntity, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.ActionableBox, { editContents: alternate, onRemove, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    ui.FieldSet,
    {
      heading: renderedBlock.label,
      isActive: selected && !isEditable,
      onClick: isEditable ? onContainerClick : void 0,
      children: blockBody
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
      lineNumber: 131,
      columnNumber: 5
    },
    void 0
  ) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
    lineNumber: 130,
    columnNumber: 4
  }, void 0) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
    lineNumber: 129,
    columnNumber: 3
  }, void 0);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(BlockElement.BlockElement, { element: props.element, attributes: props.attributes, withBoundaries: true, children: isEditable ? wrappedReference : (
    // https://github.com/ianstormtaylor/slate/issues/3426#issuecomment-573939245
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        "div",
        {
          contentEditable: false,
          "data-slate-editor": false,
          style: isFullWidth ? void 0 : { display: "inline-block" },
          children: wrappedReference
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
          lineNumber: 149,
          columnNumber: 6
        },
        void 0
      ),
      props.children
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
      lineNumber: 148,
      columnNumber: 5
    }, void 0)
  ) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
    lineNumber: 143,
    columnNumber: 3
  }, void 0);
});
ReferenceElementRenderer.displayName = "ReferenceElementRenderer";
function EmbedElementRendererInner({ handler }) {
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: handler.renderEmbed() }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/renderers/ReferenceElementRenderer.tsx",
    lineNumber: 168,
    columnNumber: 9
  }, this);
}
exports.ReferenceElementRenderer = ReferenceElementRenderer;
//# sourceMappingURL=ReferenceElementRenderer.cjs.map
