import { LayoutGallery } from "@wix/wix-ui-icons-common";
import React from "react";
import { TIPTAP_POLL_TYPE } from "./../ricos-content/index.js";
import { createDeleteButton } from "./../_shared/plugin-toolbar/create-delete-button.js";
import { createSettingsButton } from "./../_shared/plugin-toolbar/create-settings-button.js";
import { createSeparator } from "./../_shared/plugin-toolbar/separator/create-separator.js";
import { getNodeInSelection } from "./../_shared/resolvers/get-node-in-selection.js";
import { FLOATING_PLUGIN_TOOLBAR } from "./../toolbars-v3/index.js";
import { SettingsModal } from "./components/settings-modal/settings-modal.js";
import { POLL_LAYOUT_DROPDOWN_MODAL_ID, POLL_SETTINGS_MODAL_ID, SETTINGS_TAB } from "./consts.js";
import { PLUGIN_TOOLBAR_LAYOUT_DROPDOWN_BUTTON } from "./data-hooks.js";
import { PollLayoutDropdownButton } from "./toolbar/poll-layout-dropdown-button.js";
import { PollLayoutDropdown, dropdownOptions } from "./toolbar/poll-layout-dropdown.js";
const getToolbarButtons = (config, services) => {
  const { modals } = services;
  const { isMobile } = services?.context ?? {};
  const openSettingsModal = ({
    modalId,
    node,
    referenceElement,
    activeTab
  }) => {
    modals?.togglePluginPanel({
      id: modalId,
      Component: SettingsModal,
      componentProps: {
        nodeId: node.attrs.id,
        activeTab,
        config,
        modalId
      },
      positioning: {
        referenceElement: referenceElement?.closest(`[data-hook~=${FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
        placement: "right-start"
      },
      focusReturnElement: referenceElement,
      isMobile
    });
  };
  const settingsButton = createSettingsButton({
    modalId: POLL_SETTINGS_MODAL_ID,
    showLabel: true,
    command: ({ referenceElement, attributes: { selectedNode } }) => {
      openSettingsModal({
        modalId: POLL_SETTINGS_MODAL_ID,
        node: selectedNode,
        referenceElement,
        activeTab: SETTINGS_TAB.settings
      });
    }
  });
  const separator1 = createSeparator();
  const layoutButton = {
    id: POLL_LAYOUT_DROPDOWN_MODAL_ID,
    type: "modal",
    dataHook: PLUGIN_TOOLBAR_LAYOUT_DROPDOWN_BUTTON,
    attributes: {
      layout: selectedLayoutResolver,
      selectedNode: getNodeInSelection,
      active: (_, { modals: modals2 }) => Boolean(modals2?.getModal(POLL_LAYOUT_DROPDOWN_MODAL_ID))
    },
    getIcon: ({ attributes }) => {
      const selectedLayout = dropdownOptions[attributes.layout] || dropdownOptions.GRID;
      return selectedLayout?.icon || LayoutGallery;
    },
    tooltip: "Poll_PollSettings_Tab_Layout_TabName",
    command: ({ referenceElement, tiptapEditor, attributes }) => {
      if (modals?.isModalOpen(POLL_LAYOUT_DROPDOWN_MODAL_ID)) {
        modals?.closeModal(POLL_LAYOUT_DROPDOWN_MODAL_ID);
        return;
      }
      modals?.openModal({
        id: POLL_LAYOUT_DROPDOWN_MODAL_ID,
        Component: PollLayoutDropdown,
        componentProps: {
          selectedLayout: attributes?.layout || "GRID",
          onLayoutClick: (layout) => {
            tiptapEditor.chain().focus().updateAttributes(TIPTAP_POLL_TYPE, {
              layout: {
                poll: { ...attributes.selectedNode.attrs.layout.poll, type: layout }
              }
            }).run();
            modals?.closeModal(POLL_LAYOUT_DROPDOWN_MODAL_ID);
          },
          onCustomizeButtonClick: () => {
            openSettingsModal({
              modalId: POLL_SETTINGS_MODAL_ID,
              node: attributes.selectedNode,
              referenceElement,
              activeTab: SETTINGS_TAB.layout
            });
          }
        },
        layout: isMobile ? "drawer" : "floating",
        positioning: { referenceElement, placement: "bottom" },
        type: "exclusive"
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ React.createElement(PollLayoutDropdownButton, { toolbarItem })
  };
  const separator2 = createSeparator();
  const deleteButton = createDeleteButton();
  if (services.context.environment === "wixel") {
    return [settingsButton, layoutButton, deleteButton];
  }
  return [settingsButton, separator1, layoutButton, separator2, deleteButton];
};
const selectedLayoutResolver = (content) => {
  const pollNode = Array.isArray(content) && content.find((node) => node.type.name === "POLL");
  if (!pollNode) {
    return void 0;
  }
  return pollNode.attrs.layout?.poll.type;
};
export {
  getToolbarButtons
};
//# sourceMappingURL=get-toolbar-buttons.js.map
