"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var get_toolbar_buttons_exports = {};
__export(get_toolbar_buttons_exports, {
  getToolbarButtons: () => getToolbarButtons
});
module.exports = __toCommonJS(get_toolbar_buttons_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../ricos-content/index.js");
var import_v1 = require("./../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_next_schema = require("./../ricos-schema/next-schema/index.js");
var import_create_plugin_toolbar_button = require("./../_shared/create-plugin-toolbar-button.js");
var import_constants = require("./../_shared/link-modal/constants.js");
var import_data_hooks = require("./../_shared/link-modal/data-hooks/data-hooks.js");
var import_create_delete_button = require("./../_shared/plugin-toolbar/create-delete-button.js");
var import_create_node_alignment_button = require("./../_shared/plugin-toolbar/create-node-alignment-button.js");
var import_create_settings_button = require("./../_shared/plugin-toolbar/create-settings-button.js");
var import_create_node_size_button = require("./../_shared/plugin-toolbar/node-size-button/create-node-size-button.js");
var import_create_separator = require("./../_shared/plugin-toolbar/separator/create-separator.js");
var import_get_node_in_selection = require("./../_shared/resolvers/get-node-in-selection.js");
var import_handle_gallery_upload = require(".//handle-gallery-upload.js");
var import_toolbars_v3 = require("./../toolbars-v3/index.js");
var import_consts = require("./consts.js");
var import_data_hooks2 = require("./data-hooks.js");
var import_layout_data_provider = require("./layout-data-provider.js");
var import_settings = require("./modals/settings/index.js");
var import_gallery_layout_button = require("./toolbar/buttons/gallery-layout-button.js");
var import_gallery_manage_media_button = require("./toolbar/buttons/gallery-manage-media-button.js");
var import_layout_dropdown = require("./toolbar/layout-dropdown.js");
const selectedLayoutResolver = (content) => {
  const galleryNode = Array.isArray(content) && content.find((node) => node.type.name === import_v1.Node_Type.GALLERY);
  if (!galleryNode) {
    return void 0;
  }
  return galleryNode.attrs.options?.layout.type;
};
const isValidIndex = (index) => typeof index === "number" && index >= 0;
const GALLERY_TOOLBAR_BUTTON_IDS = {
  MANAGE_MEDIA: "manage-media-btn",
  LAYOUT: "gallery-layout-btn"
};
const getToolbarButtons = (config, services) => {
  const { accept = import_consts.fileInputAccept } = config;
  const { isMobile } = services?.context ?? {};
  const { modals } = services;
  const toggleSettingsModal = ({
    modalId,
    node,
    referenceElement,
    activeTab,
    publicCommands
  }) => {
    if (modals?.isModalOpen(modalId)) {
      modals?.closeModal(modalId);
      return;
    }
    modals?.openModal({
      id: modalId,
      Component: import_settings.GallerySettingsModal,
      withTransition: true,
      enableDrag: !isMobile,
      dragHandleSelector: "[data-drag-handler]",
      layout: isMobile ? "fullscreen" : "floating",
      componentProps: {
        nodeId: node.attrs.id,
        modalId,
        onFileSelect: (index) => {
          (0, import_handle_gallery_upload.handleGalleryUpload)({
            publicCommands,
            services,
            config,
            nodeId: node.attrs.id,
            fileState: isValidIndex(index) ? { itemIndex: index } : void 0,
            referenceElement
          });
        },
        accept,
        activeTab
      },
      positioning: {
        referenceElement: referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
        placement: "right-start"
      },
      focusReturnElement: referenceElement,
      disableArrowNavigation: true,
      onClickOutside: {
        on: "pointerup",
        handler: (e) => {
          if (e.target instanceof Element && e.target?.closest?.(`[data-hook~=${import_data_hooks.MODAL_ROOT}]`) !== null) {
            return;
          }
          if (modals.getModal(import_constants.LINK_MODAL_ID)) {
            modals.closeModal(import_constants.LINK_MODAL_ID);
          }
          if (referenceElement?.contains?.(e.target)) {
            return;
          }
          modals.closeModal(modalId);
        }
      }
    });
  };
  const manageMediaButton = {
    id: GALLERY_TOOLBAR_BUTTON_IDS.MANAGE_MEDIA,
    type: "modal",
    dataHook: import_data_hooks2.PLUGIN_TOOLBAR_MANAGE_MEDIA_BUTTON,
    icon: import_wix_ui_icons_common.ManagePhotos,
    tooltip: "ManageMediaButton_Tooltip",
    label: "GallerySettings_Toolbar_Button_MangeMedia",
    command: ({ referenceElement, attributes: { selectedNode }, publicCommands }) => {
      toggleSettingsModal({
        modalId: import_consts.GALLERY_MEDIA_SETTINGS_MODAL,
        node: selectedNode,
        referenceElement,
        activeTab: import_consts.SETTINGS_TAB.design,
        publicCommands
      });
    },
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      active: (_, { modals: modals2 }) => Boolean(modals2?.getModal(import_consts.GALLERY_MEDIA_SETTINGS_MODAL))
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_gallery_manage_media_button.GalleryManageMediaButton, { toolbarItem })
  };
  const separator = (0, import_create_separator.createSeparator)();
  const nodeSizeButton = (0, import_create_node_size_button.createNodeSizeButton)({
    services,
    modalId: "size"
  });
  const nodeAlignmentButton = (0, import_create_node_alignment_button.createNodeAlignmentButton)({ services, modalId: "alignment" });
  const layoutButton = (0, import_create_plugin_toolbar_button.createPluginToolbarButton)({
    id: GALLERY_TOOLBAR_BUTTON_IDS.LAYOUT,
    type: "dropdown",
    dataHook: import_data_hooks2.PLUGIN_TOOLBAR_LAYOUT_DROPDOWN_BUTTON,
    getIcon: ({ attributes }) => {
      const selectedLayout = import_layout_dropdown.LAYOUT_DROPDOWN_OPTIONS[attributes.layout] || import_layout_dropdown.LAYOUT_DROPDOWN_OPTIONS[import_next_schema.GalleryOptions_Layout_Type.GRID];
      return selectedLayout.icon || import_wix_ui_icons_common.LayoutGallery;
    },
    label: "GalleryPlugin_Layout_Select_Label",
    tooltip: "GalleryPlugin_Layout_Select_Tooltip",
    Component: import_layout_dropdown.LayoutDropdown,
    getComponentProps: ({ tiptapEditor, attributes, publicCommands, t }) => {
      return {
        options: [
          ...Object.entries(import_layout_dropdown.LAYOUT_DROPDOWN_OPTIONS).map(
            ([layout, { icon: Icon, iconSmall: IconSmall, text }]) => ({
              id: layout,
              text: t(text),
              dataHook: `${import_data_hooks2.LAYOUT_DROPDOWN_OPTION}${layout}`,
              iconMobile: Icon,
              icon: IconSmall
            })
          ),
          {
            id: import_layout_dropdown.CUSTOMIZE_LAYOUT_OPTION_ID,
            text: t("GalleryPlugin_Layouts_Customize_Button"),
            dataHook: import_data_hooks2.LAYOUT_DROPDOWN_OPTION_CUSTOMIZE_LAYOUT
          }
        ],
        onSelect: ({ referenceElement }) => (layout) => {
          if (layout === import_layout_dropdown.CUSTOMIZE_LAYOUT_OPTION_ID) {
            toggleSettingsModal({
              modalId: import_consts.GALLERY_LAYOUT_SETTINGS_MODAL,
              node: attributes.selectedNode,
              referenceElement: referenceElement ?? void 0,
              activeTab: import_consts.SETTINGS_TAB.layout,
              publicCommands
            });
            return;
          }
          const {
            selection: { anchor: pos }
          } = tiptapEditor.state;
          tiptapEditor.chain().focus().updateAttributes(import_ricos_content.TIPTAP_GALLERY_TYPE, {
            options: import_layout_data_provider.layoutRicosData[layout]
          }).run();
          setTimeout(() => tiptapEditor.chain().focus().setNodeSelection(pos).run(), 50);
        },
        selectedId: attributes?.layout ?? import_next_schema.GalleryOptions_Layout_Type.GRID,
        closeModal: () => modals?.closeModal(import_consts.GALLERY_LAYOUT_DROPDOWN)
      };
    },
    command: ({
      referenceElement,
      attributes,
      publicCommands,
      getComponentProps,
      tiptapEditor
    }) => {
      if (modals?.isModalOpen(import_consts.GALLERY_LAYOUT_DROPDOWN)) {
        modals?.closeModal(import_consts.GALLERY_LAYOUT_DROPDOWN);
        return;
      }
      if (modals?.isModalOpen(import_consts.GALLERY_LAYOUT_SETTINGS_MODAL)) {
        modals?.closeModal(import_consts.GALLERY_LAYOUT_SETTINGS_MODAL);
      }
      const componentProps = getComponentProps?.({
        tiptapEditor,
        attributes,
        publicCommands,
        t: services.t,
        modals,
        languageDir: services.context.languageDir
      });
      if (!componentProps) {
        return;
      }
      modals?.openModal({
        id: import_consts.GALLERY_LAYOUT_DROPDOWN,
        Component: import_layout_dropdown.LayoutDropdown,
        componentProps: {
          options: componentProps.options,
          onSelect: componentProps.onSelect({
            referenceElement: referenceElement || null
          }),
          selectedId: componentProps.selectedId,
          closeModal: componentProps.closeModal
        },
        layout: isMobile ? "drawer" : "floating",
        positioning: { referenceElement, placement: "bottom" }
      });
    },
    attributes: {
      layout: selectedLayoutResolver,
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      active: (_, { modals: modals2 }) => Boolean(modals2?.getModal(import_consts.GALLERY_LAYOUT_DROPDOWN)) || Boolean(modals2?.getModal(import_consts.GALLERY_LAYOUT_SETTINGS_MODAL))
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_gallery_layout_button.GalleryLayoutButton, { toolbarItem })
  });
  const settingsButton = (0, import_create_settings_button.createSettingsButton)({
    modalId: import_consts.GALLERY_SETTINGS_MODAL_ID,
    command: ({ referenceElement, attributes: { selectedNode }, publicCommands }) => {
      toggleSettingsModal({
        modalId: import_consts.GALLERY_SETTINGS_MODAL_ID,
        node: selectedNode,
        referenceElement,
        activeTab: import_consts.SETTINGS_TAB.settings,
        publicCommands
      });
    }
  });
  const deleteButton = (0, import_create_delete_button.createDeleteButton)();
  return services.context.environment === "wixel" ? [
    manageMediaButton,
    settingsButton,
    layoutButton,
    nodeSizeButton,
    nodeAlignmentButton,
    deleteButton
  ] : [
    manageMediaButton,
    separator,
    nodeSizeButton,
    nodeAlignmentButton,
    layoutButton,
    settingsButton,
    separator,
    deleteButton
  ];
};
//# sourceMappingURL=get-toolbar-buttons.js.map
