"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 slateReact = require("slate-react");
const getDiscriminatedDatum = require("../../discrimination/getDiscriminatedDatum.cjs");
const ReferenceElement = require("./elements/ReferenceElement.cjs");
function toToolbarGroups(editorReferenceBlocks, buttons, environment, editor) {
  if (!buttons) {
    return [];
  }
  const sections = Array.isArray(buttons[0]) ? buttons : [buttons];
  return sections.map((section) => {
    return {
      buttons: section.map((buttonProps) => {
        const { title, ...rest } = buttonProps;
        return {
          label: title,
          ...rest,
          onClick: (e) => {
            e.nativeEvent.preventDefault();
            e.nativeEvent.stopPropagation();
            if ("discriminateBy" in buttonProps) {
              const discriminateBy = reactBinding.VariableInputTransformer.transformValue(buttonProps.discriminateBy, environment);
              const targetBlock = getDiscriminatedDatum.getDiscriminatedDatum(editorReferenceBlocks, discriminateBy);
              if (targetBlock === void 0) {
                throw new reactBinding.BindingError(
                  `BlockEditor: Trying to insert a block discriminated by '${discriminateBy}' but no such block has been found!`
                );
              }
              let insertedElement;
              if (targetBlock.datum.template === void 0) {
                insertedElement = {
                  type: ReferenceElement.referenceElementType,
                  children: [{ text: "" }]
                };
              } else {
                insertedElement = {
                  type: ReferenceElement.referenceElementType,
                  children: [editor.createDefaultElement([{ text: "" }])]
                };
              }
              editor.insertElementWithReference(insertedElement, discriminateBy);
            } else {
              editor.toggleElement(buttonProps.elementType, buttonProps.suchThat);
            }
          }
        };
      })
    };
  });
}
const BlockHoveringToolbarContents = react.memo(({
  editorReferenceBlocks,
  blockButtons,
  otherBlockButtons,
  showLabels,
  ...props
}) => {
  const editor = slateReact.useSlateStatic();
  const environment = reactBinding.useEnvironment();
  const groups = react.useMemo(() => {
    return toToolbarGroups(editorReferenceBlocks, blockButtons, environment, editor);
  }, [editorReferenceBlocks, blockButtons, environment, editor]);
  const restGroups = react.useMemo(() => {
    return otherBlockButtons ? toToolbarGroups(editorReferenceBlocks, otherBlockButtons, environment, editor) : void 0;
  }, [otherBlockButtons, editorReferenceBlocks, environment, editor]);
  if (!blockButtons || !blockButtons.length) {
    return null;
  }
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    ui.EditorToolbar,
    {
      isActive: true,
      groups,
      restGroups,
      showLabels
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/blockEditor/BlockHoveringToolbarContents.tsx",
      lineNumber: 116,
      columnNumber: 3
    },
    void 0
  );
});
BlockHoveringToolbarContents.displayName = "BlockHoveringToolbarContents";
exports.BlockHoveringToolbarContents = BlockHoveringToolbarContents;
//# sourceMappingURL=BlockHoveringToolbarContents.cjs.map
