import { jsxDEV } from "react/jsx-dev-runtime";
import { useEnvironment, VariableInputTransformer, Entity } from "@contember/react-binding";
import { useDialog, EditorToolbar } from "@contember/ui";
import { memo } from "react";
import { Transforms } from "slate";
import { useSlate } from "slate-react";
import { referenceElementType } from "../blockEditor/elements/ReferenceElement.js";
const HoveringToolbarContents = memo(({ buttons: rawButtons, showLabels }) => {
  const editor = useSlate();
  const { openDialog } = useDialog();
  const environment = 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 = VariableInputTransformer.transformValue(button.discriminateBy, environment);
            const selection = editor.selection;
            const targetPath = selection?.focus.path;
            if (targetPath === void 0) {
              return;
            }
            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__ */ jsxDEV(Entity, { accessor: reference, children: /* @__PURE__ */ jsxDEV(
                Content,
                {
                  referenceId: reference.id,
                  editor,
                  selection,
                  onSuccess: ({ createElement } = {}) => {
                    if (createElement !== void 0) {
                      if (!selection) {
                        return;
                      }
                      Transforms.select(editor, selection);
                      Transforms.wrapNodes(
                        editor,
                        {
                          type: referenceElementType,
                          children: [{ text: "" }],
                          referenceId: reference.id,
                          ...createElement
                        },
                        { split: true }
                      );
                      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__ */ jsxDEV(
    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
  );
});
export {
  HoveringToolbarContents
};
//# sourceMappingURL=HoveringToolbarContents.js.map
