import { memo, useMemo, createElement } from "react";
import { useEnvironment, VariableInputTransformer, BindingError } from "@contember/binding";
import { EditorToolbar } from "@contember/ui";
import { useSlateStatic } from "slate-react";
import { getDiscriminatedDatum } from "../../discrimination/getDiscriminatedDatum.js";
import { referenceElementType } from "./elements/ReferenceElement.js";
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 = VariableInputTransformer.transformValue(buttonProps.discriminateBy, environment);
              const targetBlock = getDiscriminatedDatum(editorReferenceBlocks, discriminateBy);
              if (targetBlock === void 0) {
                throw new 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: referenceElementType,
                  children: [{ text: "" }]
                };
              } else {
                insertedElement = {
                  type: referenceElementType,
                  children: [editor.createDefaultElement([{ text: "" }])]
                };
              }
              editor.insertElementWithReference(insertedElement, discriminateBy);
            } else {
              editor.toggleElement(buttonProps.elementType, buttonProps.suchThat);
            }
          }
        };
      })
    };
  });
}
const BlockHoveringToolbarContents = memo((props) => {
  const editor = useSlateStatic();
  const environment = useEnvironment();
  const { editorReferenceBlocks, blockButtons, otherBlockButtons } = props;
  const groups = useMemo(() => {
    return toToolbarGroups(editorReferenceBlocks, blockButtons, environment, editor);
  }, [editorReferenceBlocks, blockButtons, environment, editor]);
  const restGroups = useMemo(() => {
    return otherBlockButtons ? toToolbarGroups(editorReferenceBlocks, otherBlockButtons, environment, editor) : void 0;
  }, [otherBlockButtons, editorReferenceBlocks, environment, editor]);
  if (!props.blockButtons || !props.blockButtons.length) {
    return null;
  }
  return /* @__PURE__ */ createElement(EditorToolbar, {
    isActive: true,
    groups,
    restGroups
  });
});
BlockHoveringToolbarContents.displayName = "BlockHoveringToolbarContents";
export { BlockHoveringToolbarContents };
//# sourceMappingURL=BlockHoveringToolbarContents.js.map
