"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-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__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
      !!contentTemplate.nodeBefore && /* @__PURE__ */ jsxRuntime.jsx("div", { contentEditable: false, children: contentTemplate.nodeBefore }),
      showPlaceholder ? /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { position: "relative" }, children: [
        /* @__PURE__ */ jsxRuntime.jsx("div", { style: { position: "absolute", pointerEvents: "none" }, contentEditable: false, children: /* @__PURE__ */ jsxRuntime.jsx(ui.EditorPlaceholder, { children: placeholder }) }),
        /* @__PURE__ */ jsxRuntime.jsx("div", { children: props.children })
      ] }) : /* @__PURE__ */ jsxRuntime.jsx("div", { children: props.children }),
      !!contentTemplate.nodeAfter && /* @__PURE__ */ jsxRuntime.jsx("div", { contentEditable: false, children: contentTemplate.nodeAfter })
    ] });
  } 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__ */ jsxRuntime.jsx(
      "div",
      {
        style: { display: "flex" },
        children: /* @__PURE__ */ jsxRuntime.jsx(EmbedElementRendererInner, { handler: embedHandler.datum })
      }
    );
  } else {
    blockBody = renderedBlock.children;
  }
  const alternate = renderedBlock.alternate ? /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { children: renderedBlock.alternate }) : void 0;
  const wrappedReference = /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Entity, { accessor: referencedEntity, children: /* @__PURE__ */ jsxRuntime.jsx(ui.ActionableBox, { editContents: alternate, onRemove, children: /* @__PURE__ */ jsxRuntime.jsx(
    ui.FieldSet,
    {
      heading: renderedBlock.label,
      isActive: selected && !isEditable,
      onClick: isEditable ? onContainerClick : void 0,
      children: blockBody
    }
  ) }) });
  return /* @__PURE__ */ jsxRuntime.jsx(BlockElement.BlockElement, { element: props.element, attributes: props.attributes, withBoundaries: true, children: isEditable ? wrappedReference : (
    // https://github.com/ianstormtaylor/slate/issues/3426#issuecomment-573939245
    /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
      /* @__PURE__ */ jsxRuntime.jsx(
        "div",
        {
          contentEditable: false,
          "data-slate-editor": false,
          style: isFullWidth ? void 0 : { display: "inline-block" },
          children: wrappedReference
        }
      ),
      props.children
    ] })
  ) });
});
ReferenceElementRenderer.displayName = "ReferenceElementRenderer";
function EmbedElementRendererInner({ handler }) {
  return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: handler.renderEmbed() });
}
exports.ReferenceElementRenderer = ReferenceElementRenderer;
//# sourceMappingURL=ReferenceElementRenderer.cjs.map
