"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 text_alignment_button_exports = {};
__export(text_alignment_button_exports, {
  TextAlignmentButton: () => TextAlignmentButton,
  createTextAlignmentButton: () => createTextAlignmentButton
});
module.exports = __toCommonJS(text_alignment_button_exports);
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_react = __toESM(require("react"));
var import_consts = require("./../../../ricos-common-plugins/text-align/dropdown/consts.js");
var import_text_alignment_dropdown = __toESM(require("./../../../ricos-common-plugins/text-align/dropdown/text-alignment-dropdown.js"));
var import_ricos_context = require("./../../../ricos-context/index.js");
var import_modal_service_provider = require("./../../../ricos-modals/modal-service-provider.js");
var import_create_plugin_toolbar_button = require("./../../../_shared/create-plugin-toolbar-button.js");
var import_get_alignment_in_selection = require("./../../../_shared/resolvers/get-alignment-in-selection.js");
var import_toolbar_button = require("./../../../_shared/toolbar-button/toolbar-button.js");
var import_consts2 = require("../../consts.js");
var import_data_hooks = require("../../data-hooks.js");
const textAlignmentIcons = {
  left: import_wix_ui_icons_common.TextAlignLeftLight,
  center: import_wix_ui_icons_common.TextAlignCenterLight,
  right: import_wix_ui_icons_common.TextAlignRightLight,
  justify: import_wix_ui_icons_common.JustifyTextLight
};
const TextAlignmentButton = ({ toolbarItem, dataHook }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext) || {};
  const [referenceElement, setReferenceElement] = (0, import_react.useState)(null);
  const alignment = toolbarItem.attributes.selectedAlignment || "left";
  const Icon = textAlignmentIcons[alignment] || import_wix_ui_icons_common.TextAlignLeftLight;
  const isModalOpen = (0, import_modal_service_provider.useModalState)((0, import_modal_service_provider.getIsModalOpen)(import_consts2.TABLE_BUTTONS_MODALS_ID.TEXT_ALIGNMENT));
  return /* @__PURE__ */ import_react.default.createElement(
    import_toolbar_button.ToolbarButton,
    {
      ref: setReferenceElement,
      disabled: toolbarItem.attributes.disabled,
      active: isModalOpen,
      tooltip: t("AlignTextDropdownButton_Tooltip"),
      onClick: () => {
        toolbarItem.commands?.click({ referenceElement });
      },
      prefix: Icon,
      suffix: import_wix_ui_icons_common.ChevronDownSmall,
      dataHook,
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
const createTextAlignmentButton = (services) => (0, import_create_plugin_toolbar_button.createPluginToolbarButton)({
  id: import_consts2.TABLE_BUTTONS.TEXT_ALIGNMENT,
  type: "dropdown",
  dataHook: import_data_hooks.PLUGIN_TOOLBAR_TEXT_ALIGNMENT_BUTTON,
  getIcon: ({ attributes }) => {
    const alignment = attributes?.selectedAlignment || "left";
    return import_consts.ALIGNMENT_BUTTON_CONFIGS[alignment]?.icon || textAlignmentIcons.left;
  },
  tooltip: "AlignTextDropdownButton_Tooltip",
  Component: import_text_alignment_dropdown.default,
  getComponentProps: ({ modals, publicCommands }) => {
    return {
      closeModal: () => modals.closeModal(import_consts2.TABLE_BUTTONS_MODALS_ID.TEXT_ALIGNMENT),
      dropdownOptions: Object.values(import_consts.ALIGNMENT_BUTTON_CONFIGS).map((item) => ({
        id: item.commandKey,
        dataHook: item.dataHook,
        label: services.t(item.text),
        icon: item.icon,
        iconMobile: item.iconMobile
      })),
      onSelect: (alignment) => {
        publicCommands.setTextAlignment(alignment);
      },
      selectedId: publicCommands.getTextAlignment() || "left"
    };
  },
  command: ({
    modals,
    referenceElement,
    publicCommands,
    getComponentProps,
    tiptapEditor,
    attributes
  }) => {
    const modalId = import_consts2.TABLE_BUTTONS_MODALS_ID.TEXT_ALIGNMENT;
    const closeModal = () => modals.closeModal(modalId);
    const isModalOpen = modals.isModalOpen(modalId);
    if (isModalOpen) {
      closeModal();
      return;
    }
    const componentProps = getComponentProps?.({
      tiptapEditor,
      attributes,
      publicCommands,
      t: services.t,
      modals,
      languageDir: services.context.languageDir
    });
    if (!componentProps) {
      return;
    }
    modals.openModal({
      id: modalId,
      Component: import_text_alignment_dropdown.default,
      componentProps,
      layout: services.context.isMobile ? "drawer" : "floating",
      positioning: { referenceElement, placement: "bottom" }
    });
  },
  attributes: {
    selectedAlignment: import_get_alignment_in_selection.getAlignmentInSelection
  },
  renderer: (toolbarItem) => /* @__PURE__ */ import_react.default.createElement(TextAlignmentButton, { toolbarItem })
});
//# sourceMappingURL=text-alignment-button.js.map
