"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, {
  SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID: () => SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID,
  SMART_BLOCK_COLOR_PICKER_MODAL_ID: () => SMART_BLOCK_COLOR_PICKER_MODAL_ID,
  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_create_color_button = require("./../_shared/plugin-toolbar/create-color-button.js");
var import_create_delete_button = require("./../_shared/plugin-toolbar/create-delete-button.js");
var import_create_separator = require("./../_shared/plugin-toolbar/separator/create-separator.js");
var import_pm_node_types = require("./../_shared/pm-node-types.js");
var import_get_node_in_selection = require("./../_shared/resolvers/get-node-in-selection.js");
var import_state = require("./../tiptap/pm/state.js");
var import_toolbars_v3 = require("./../toolbars-v3/index.js");
var import_smart_block_shape_picker_modal = require("./components/smart-block-shape-picker-modal.js");
var import_data_hooks = require("./data-hooks.js");
var import_border_settings = require("./modals/border-settings.js");
var import_color_picker = require("./modals/color-picker.js");
var import_variant_picker = require("./modals/variant-picker.js");
var import_buttons = require("./toolbar/buttons/index.js");
var import_create_add_cell_dropdown_button = require("./toolbar/create-add-cell-dropdown-button.js");
var import_create_column_size_button = require("./toolbar/create-column-size-button.js");
var import_create_numbered_mode_dropdown_button = require("./toolbar/create-numbered-mode-dropdown-button.js");
var import_create_orientation_dropdown_button = require("./toolbar/create-orientation-dropdown-button.js");
var import_create_text_alignment_button = require("./toolbar/create-text-alignment-button.js");
var import_resolvers = require("./toolbar/resolvers.js");
var import_shape_colors = require("./toolbar/shape-colors.js");
var import_variant_features = require("./utils/variant-features.js");
const SMART_BLOCK_COLOR_PICKER_MODAL_ID = "smart-block-color-picker-modal";
const SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID = "smart-block-cell-shape-color-picker-modal";
const getToolbarButtons = (_config, services) => {
  const { modals } = services;
  const variantSwitcher = {
    id: "smartBlockVariant",
    type: "modal",
    icon: import_wix_ui_icons_common.Replace,
    label: "SmartBlock_Toolbar_SwitchLayout_Label",
    tooltip: "SmartBlock_Toolbar_SwitchLayout_Tooltip",
    dataHook: import_data_hooks.DATA_HOOKS.VARIANT_BUTTON_DATA_HOOK,
    attributes: {
      selectedNode: import_resolvers.getSmartBlockNode,
      visible: import_resolvers.isLayoutSelected,
      active: () => Boolean(modals.getModal(import_variant_picker.SMART_BLOCK_VARIANT_PICKER_MODAL_ID))
    },
    command: ({ referenceElement, attributes, tiptapEditor }) => {
      const selectedNode = attributes.selectedNode;
      if (!selectedNode) {
        return;
      }
      modals.togglePluginPanel({
        id: import_variant_picker.SMART_BLOCK_VARIANT_PICKER_MODAL_ID,
        Component: import_variant_picker.VariantPicker,
        componentProps: {
          currentType: selectedNode.attrs.type,
          nodeId: selectedNode.attrs.id,
          onSelect: (variant, isPreview) => tiptapEditor.commands.switchSmartBlockVariant(variant, { isPreview })
        },
        isMobile: services.context.isMobile,
        positioning: {
          referenceElement: referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
          placement: "right-start"
        }
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.VariantSwitcherButton, { toolbarItem })
  };
  const columnSizeDropdown = (0, import_create_column_size_button.createColumnSizeButton)({ services });
  const addCellDropdown = (0, import_create_add_cell_dropdown_button.createAddCellDropdownButton)({ services });
  const textAlignmentButton = (0, import_create_text_alignment_button.createTextAlignmentButton)({ services });
  const orientationDropdown = (0, import_create_orientation_dropdown_button.createOrientationDropdownButton)({ services });
  const numberedModeDropdown = (0, import_create_numbered_mode_dropdown_button.createNumberedModeDropdownButton)({ services });
  const colorButton = {
    id: "smartBlockColor",
    type: "modal",
    icon: import_wix_ui_icons_common.ColorBucket,
    tooltip: "SmartBlock_Toolbar_Color_Tooltip",
    dataHook: import_data_hooks.DATA_HOOKS.COLOR_BUTTON,
    attributes: {
      selectedNode: import_resolvers.getSelectedSmartNode,
      visible: import_resolvers.isCellOrLayoutSelected,
      active: () => Boolean(modals.getModal(SMART_BLOCK_COLOR_PICKER_MODAL_ID)),
      variant: (_, __, editor) => {
        if (!editor?.state) {
          return void 0;
        }
        const sel = editor.state.selection;
        if (sel instanceof import_state.NodeSelection && sel.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
          return (0, import_resolvers.getParentVariant)(editor);
        }
        return (0, import_resolvers.getSmartBlockNode)(_, __, editor)?.attrs?.type;
      },
      colors: (_, __, editor) => {
        if (!editor?.state) {
          return [];
        }
        const sel = editor.state.selection;
        if (sel instanceof import_state.NodeSelection && sel.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
          const variant = (0, import_resolvers.getParentVariant)(editor);
          if (!variant) {
            return [];
          }
          const c = sel.node.attrs[(0, import_variant_features.getCellColorAttr)(variant)];
          return c ? [c] : [];
        }
        const node = (0, import_resolvers.getSmartBlockNode)(_, __, editor);
        if (!node) {
          return [];
        }
        const colorAttr = (0, import_variant_features.getCellColorAttr)(node.attrs.type);
        const colors = /* @__PURE__ */ new Set();
        node.forEach((child) => {
          const c = child.attrs[colorAttr];
          if (c) {
            colors.add(c);
          }
        });
        return [...colors];
      }
    },
    command: ({ referenceElement, attributes, tiptapEditor }) => {
      const selectedNode = attributes.selectedNode;
      if (!selectedNode) {
        return;
      }
      const variant = selectedNode.attrs.type;
      if (!variant) {
        return;
      }
      const colorAttr = (0, import_variant_features.getCellColorAttr)(variant);
      const isCellSelected2 = tiptapEditor.state.selection instanceof import_state.NodeSelection && tiptapEditor.state.selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL;
      const toolbarReferenceElement = referenceElement?.closest(
        `[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`
      ) ?? referenceElement;
      const cellNodeId = isCellSelected2 ? selectedNode.attrs.id : selectedNode.firstChild?.attrs?.id ?? selectedNode.attrs.id;
      clearPreviewOnModalClose(modals, tiptapEditor, SMART_BLOCK_COLOR_PICKER_MODAL_ID);
      modals.togglePluginPanel({
        id: SMART_BLOCK_COLOR_PICKER_MODAL_ID,
        Component: import_color_picker.ColorPicker,
        componentProps: {
          modalId: SMART_BLOCK_COLOR_PICKER_MODAL_ID,
          nodeId: cellNodeId,
          colorAttr,
          variant,
          referenceElement,
          // Preview during drag (no doc change → no undo entries); commit once on release.
          onColorPreview: (color) => {
            tiptapEditor.commands.setSmartBlockColorPreview({ attr: colorAttr, color });
          },
          onColorPreviewEnd: () => {
            tiptapEditor.commands.clearSmartBlockColorPreview();
          },
          onColorSelected: (color) => {
            tiptapEditor.commands.clearSmartBlockColorPreview();
            if (isCellSelected2) {
              tiptapEditor.chain().focus(void 0, { scrollIntoView: false }).command(({ tr }) => {
                const pos = tiptapEditor.state.selection.from;
                tr.setNodeAttribute(pos, colorAttr, color ?? null);
                return true;
              }).run();
              return;
            }
            tiptapEditor.commands.applyDesignToAllCells({ color });
          }
        },
        isMobile: services.context.isMobile,
        positioning: {
          referenceElement: toolbarReferenceElement,
          placement: "bottom"
        }
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.SmartBlockColorButton, { toolbarItem })
  };
  const borderButton = {
    id: "smartBlockBorder",
    type: "modal",
    icon: import_wix_ui_icons_common.BorderWidth,
    tooltip: "SmartBlock_Toolbar_Borders_Tooltip",
    dataHook: import_data_hooks.DATA_HOOKS.DESIGN_BUTTON,
    attributes: {
      selectedNode: import_resolvers.getSelectedSmartNode,
      visible: import_resolvers.isBorderSupported,
      active: () => Boolean(modals.getModal(import_border_settings.SMART_BLOCK_BORDER_SETTINGS_MODAL_ID))
    },
    command: ({ referenceElement, attributes, tiptapEditor }) => {
      const sel = tiptapEditor.state.selection;
      const toolbar = referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement;
      const selectedNode = attributes.selectedNode;
      if (!selectedNode) {
        return;
      }
      modals.togglePluginPanel({
        id: import_border_settings.SMART_BLOCK_BORDER_SETTINGS_MODAL_ID,
        Component: import_border_settings.BorderSettings,
        componentProps: {
          nodeId: sel instanceof import_state.NodeSelection && sel.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL ? selectedNode.attrs.id : void 0
        },
        isMobile: services.context.isMobile,
        positioning: { referenceElement: toolbar, placement: "bottom" },
        onClickOutside: {
          on: "pointerup",
          handler: (e) => {
            if (modals.isModalOpen(import_border_settings.SMART_BLOCK_BORDER_COLOR_PICKER_MODAL_ID)) {
              return;
            }
            if (!referenceElement?.contains?.(e.target)) {
              modals.closeModal(import_border_settings.SMART_BLOCK_BORDER_SETTINGS_MODAL_ID);
            }
          }
        }
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.BorderSettingsButton, { toolbarItem })
  };
  const blockSeparator = (0, import_create_separator.createSeparator)();
  const deleteSeparator = (0, import_create_separator.createSeparator)();
  const deleteButton = (0, import_create_delete_button.createDeleteButton)({
    attributes: {
      selectedNode: import_resolvers.getSelectedSmartNode,
      visible: import_resolvers.isCellOrLayoutSelected
    },
    command: ({ tiptapEditor, attributes }) => {
      const sel = tiptapEditor.state.selection;
      if (sel instanceof import_state.NodeSelection && sel.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL) {
        const cellId = attributes.selectedNode?.attrs?.id;
        if (cellId) {
          tiptapEditor.commands.removeSmartBlockCell(cellId);
        }
      } else {
        tiptapEditor.chain().focus().deleteNode(attributes.selectedNode?.attrs?.id).run();
      }
    }
  });
  const addIconButton = {
    id: "smartBlockCellAddIcon",
    type: "modal",
    icon: import_wix_ui_icons_common.Objects,
    tooltip: "SmartBlock_CellAddShape_Label",
    dataHook: import_data_hooks.DATA_HOOKS.CELL_ADD_ICON_BUTTON,
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      visible: import_resolvers.isCellInLabelVariantWithoutImage,
      active: () => Boolean(modals.getModal(import_data_hooks.DATA_HOOKS.CELL_SHAPE_PICKER))
    },
    command: ({ referenceElement, attributes }) => {
      const cellId = attributes.selectedNode?.attrs?.id;
      if (!cellId) {
        return;
      }
      modals.togglePluginPanel({
        id: import_data_hooks.DATA_HOOKS.CELL_SHAPE_PICKER,
        Component: import_smart_block_shape_picker_modal.SmartBlockShapePickerModal,
        componentProps: { cellId },
        isMobile: services.context.isMobile,
        positioning: {
          referenceElement: referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
          placement: "right-start"
        }
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.AddIconButton, { toolbarItem })
  };
  const replaceShapeButton = {
    id: "smartBlockCellReplaceShape",
    type: "toggle",
    icon: import_wix_ui_icons_common.Replace,
    tooltip: "SmartBlock_CellReplaceShape_Label",
    dataHook: import_data_hooks.DATA_HOOKS.CELL_REPLACE_SHAPE_BUTTON,
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      visible: import_resolvers.isCellInLabelVariantWithImage,
      active: () => Boolean(modals.getModal(import_data_hooks.DATA_HOOKS.CELL_SHAPE_PICKER))
    },
    command: ({ referenceElement, attributes }) => {
      const cellId = attributes.selectedNode?.attrs?.id;
      if (!cellId) {
        return;
      }
      modals.togglePluginPanel({
        id: import_data_hooks.DATA_HOOKS.CELL_SHAPE_PICKER,
        Component: import_smart_block_shape_picker_modal.SmartBlockShapePickerModal,
        componentProps: { cellId },
        isMobile: services.context.isMobile,
        positioning: {
          referenceElement: referenceElement?.closest(`[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`) ?? referenceElement,
          placement: "right-start"
        }
      });
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.ReplaceShapeButton, { toolbarItem })
  };
  const shapeColorButton = {
    ...(0, import_create_color_button.createColorButton)({
      getColors: import_shape_colors.getShapeColors,
      tooltip: "SmartBlock_ShapeColor_Label",
      modalId: SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID,
      dataHook: import_data_hooks.DATA_HOOKS.CELL_SHAPE_COLOR_BUTTON,
      command: ({ referenceElement, attributes: { selectedNode }, tiptapEditor }) => {
        if (!selectedNode) {
          return;
        }
        const variant = selectedNode.attrs.type;
        if (!variant) {
          return;
        }
        const isCellSelection = tiptapEditor.state.selection instanceof import_state.NodeSelection && tiptapEditor.state.selection.node?.type?.name === import_pm_node_types.PmNodeType.SMART_BLOCK_CELL;
        const toolbarReferenceElement = referenceElement?.closest(
          `[data-hook~=${import_toolbars_v3.FLOATING_PLUGIN_TOOLBAR}]`
        ) ?? referenceElement;
        const cellNodeId = isCellSelection ? selectedNode.attrs.id : selectedNode.firstChild?.attrs?.id ?? selectedNode.attrs.id;
        clearPreviewOnModalClose(
          modals,
          tiptapEditor,
          SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID
        );
        modals.togglePluginPanel({
          id: SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID,
          Component: import_color_picker.ColorPicker,
          componentProps: {
            modalId: SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID,
            nodeId: cellNodeId,
            colorAttr: "shapeColor",
            variant,
            referenceElement,
            // Preview during drag (no doc change → no undo entries); commit once on release.
            onColorPreview: (color) => {
              tiptapEditor.commands.setSmartBlockColorPreview({ attr: "shapeColor", color });
            },
            onColorPreviewEnd: () => {
              tiptapEditor.commands.clearSmartBlockColorPreview();
            },
            onColorSelected: (color) => {
              tiptapEditor.commands.clearSmartBlockColorPreview();
              if (isCellSelection) {
                tiptapEditor.chain().focus(void 0, { scrollIntoView: false }).command(({ tr }) => {
                  const pos = tiptapEditor.state.selection.from;
                  tr.setNodeAttribute(pos, "shapeColor", color ?? null);
                  return true;
                }).run();
                return;
              }
              tiptapEditor.commands.setShapeColorForAllCells(color ?? null);
            }
          },
          isMobile: services.context.isMobile,
          positioning: {
            referenceElement: toolbarReferenceElement,
            placement: "bottom"
          }
        });
      }
    }),
    attributes: {
      selectedNode: import_resolvers.getSelectedSmartNode,
      active: () => Boolean(modals.getModal(SMART_BLOCK_CELL_SHAPE_COLOR_PICKER_MODAL_ID)),
      visible: (_, __, editor) => (0, import_resolvers.isCellInLabelVariantWithImage)(_, __, editor) || (0, import_resolvers.isLayoutInLabelVariantWithImage)(_, __, editor)
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.SmartBlockShapeColorButton, { toolbarItem })
  };
  const duplicateButton = {
    id: "smartBlockCellDuplicate",
    type: "toggle",
    icon: import_wix_ui_icons_common.Duplicate,
    tooltip: "SmartBlock_CellDuplicate_Tooltip",
    dataHook: import_data_hooks.DATA_HOOKS.CELL_DUPLICATE_BUTTON,
    attributes: {
      selectedNode: import_get_node_in_selection.getNodeInSelection,
      visible: import_resolvers.isCellSelected,
      canAddCell: import_resolvers.canAddCell
    },
    command: ({ tiptapEditor, attributes }) => {
      const cellId = attributes.selectedNode?.attrs?.id;
      if (cellId) {
        tiptapEditor.commands.duplicateSmartBlockCell(cellId);
      }
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.DuplicateCellButton, { toolbarItem })
  };
  const selectBlockButton = {
    id: "smartBlockSelectBlock",
    type: "toggle",
    icon: import_wix_ui_icons_common.ChevronLeft,
    tooltip: "SmartBlock_SelectBlock_Tooltip",
    dataHook: import_data_hooks.DATA_HOOKS.SELECT_BLOCK_BUTTON,
    attributes: { visible: import_resolvers.isCellSelected },
    command: ({ tiptapEditor }) => {
      tiptapEditor.commands.selectSmartBlock();
    },
    renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(import_buttons.SelectBlockButton, { toolbarItem })
  };
  return [
    selectBlockButton,
    // cell toolbar
    blockSeparator,
    addIconButton,
    // cell toolbar
    replaceShapeButton,
    // cell toolbar
    variantSwitcher,
    blockSeparator,
    columnSizeDropdown,
    textAlignmentButton,
    numberedModeDropdown,
    orientationDropdown,
    addCellDropdown,
    // cell toolbar
    duplicateButton,
    // cell toolbar
    deleteSeparator,
    colorButton,
    shapeColorButton,
    // cell toolbar
    borderButton,
    deleteSeparator,
    deleteButton
  ];
};
const clearPreviewOnModalClose = (modals, tiptapEditor, modalId) => {
  const sub = modals.onModalClosed((modal) => {
    if (modal.id === modalId) {
      tiptapEditor.commands.clearSmartBlockColorPreview();
      sub.cancel();
    }
  });
};
//# sourceMappingURL=get-toolbar-buttons.js.map
