"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 ReferenceElement = require("../blockEditor/elements/ReferenceElement.cjs");
const HoveringToolbarContents = react.memo(({ buttons: rawButtons, showLabels }) => {
  const editor = slateReact.useSlate();
  const { openDialog } = ui.useDialog();
  const environment = reactBinding.useEnvironment();
  if (!rawButtons.length) {
    return null;
  }
  const buttons = Array.isArray(rawButtons[0]) ? rawButtons : [rawButtons];
  const groups = buttons.map(
    (buttons2) => ({
      buttons: buttons2.map((button) => {
        let shouldDisplay;
        let isActive;
        let onMouseDown;
        if ("toggle" in button) {
          shouldDisplay = button.shouldDisplay?.({ editor }) ?? true;
          isActive = button.isActive?.({ editor }) ?? false;
          onMouseDown = () => {
            button.toggle({ editor });
          };
        } else if ("marks" in button) {
          shouldDisplay = editor.canToggleMarks(button.marks);
          isActive = editor.hasMarks(button.marks);
          onMouseDown = () => {
            editor.toggleMarks(button.marks);
          };
        } else if ("elementType" in button) {
          shouldDisplay = editor.canToggleElement(button.elementType, button.suchThat);
          isActive = editor.isElementActive(button.elementType, button.suchThat);
          onMouseDown = () => {
            editor.toggleElement(button.elementType, button.suchThat);
          };
        } else if ("referenceContent" in button) {
          shouldDisplay = true;
          isActive = false;
          onMouseDown = async () => {
            const discriminateBy = reactBinding.VariableInputTransformer.transformValue(button.discriminateBy, environment);
            const selection = editor.selection;
            const targetPath = selection?.focus.path;
            if (targetPath === void 0) {
              return;
            }
            Slate.Transforms.deselect(editor);
            const reference = editor.createElementReference(
              targetPath,
              discriminateBy,
              button.initializeReference
            );
            const Content = button.referenceContent;
            const result = await openDialog({
              heading: button.label,
              content: (props) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Entity, { accessor: reference, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
                Content,
                {
                  referenceId: reference.id,
                  editor,
                  selection,
                  onSuccess: ({ createElement } = {}) => {
                    if (createElement !== void 0) {
                      if (!selection) {
                        return;
                      }
                      Slate.Transforms.select(editor, selection);
                      Slate.Transforms.wrapNodes(
                        editor,
                        {
                          type: ReferenceElement.referenceElementType,
                          children: [{ text: "" }],
                          referenceId: reference.id,
                          ...createElement
                        },
                        { split: true }
                      );
                      Slate.Transforms.collapse(editor, { edge: "end" });
                    }
                    props.resolve(true);
                  },
                  onCancel: () => props.resolve()
                },
                void 0,
                false,
                {
                  fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/toolbars/HoveringToolbarContents.tsx",
                  lineNumber: 75,
                  columnNumber: 12
                },
                void 0
              ) }, void 0, false, {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/toolbars/HoveringToolbarContents.tsx",
                lineNumber: 74,
                columnNumber: 11
              }, void 0)
            });
            if (result !== true) {
              reference.deleteEntity();
            }
          };
        } else {
          return void 0;
        }
        if (!shouldDisplay) {
          return void 0;
        }
        return {
          label: button.title,
          //layout?: ToolbarButtonLayout
          isActive,
          onClick: (e) => {
            e.preventDefault();
            e.nativeEvent.stopPropagation();
            onMouseDown();
          },
          blueprintIcon: button.blueprintIcon,
          contemberIcon: button.contemberIcon,
          customIcon: button.customIcon
        };
      }).filter((item) => !!item)
    })
  ).filter((item) => item.buttons.length);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
    ui.EditorToolbar,
    {
      groups,
      scope: "contextual",
      showLabels
    },
    void 0,
    false,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/richText/toolbars/HoveringToolbarContents.tsx",
      lineNumber: 136,
      columnNumber: 3
    },
    void 0
  );
});
exports.HoveringToolbarContents = HoveringToolbarContents;
//# sourceMappingURL=HoveringToolbarContents.cjs.map
