import {
  ChevronDownSmall,
  JustifyTextLight,
  TextAlignCenterLight,
  TextAlignLeftLight,
  TextAlignRightLight
} from "@wix/wix-ui-icons-common";
import React, { useContext, useState } from "react";
import { ALIGNMENT_BUTTON_CONFIGS } from "./../../../ricos-common-plugins/text-align/dropdown/consts.js";
import TextAlignmentDropdown from "./../../../ricos-common-plugins/text-align/dropdown/text-alignment-dropdown.js";
import { RicosContext } from "./../../../ricos-context/index.js";
import { getIsModalOpen, useModalState } from "./../../../ricos-modals/modal-service-provider.js";
import { createPluginToolbarButton } from "./../../../_shared/create-plugin-toolbar-button.js";
import { getAlignmentInSelection } from "./../../../_shared/resolvers/get-alignment-in-selection.js";
import { ToolbarButton as ToolbarButtonComponent } from "./../../../_shared/toolbar-button/toolbar-button.js";
import { TABLE_BUTTONS, TABLE_BUTTONS_MODALS_ID } from "../../consts.js";
import { PLUGIN_TOOLBAR_TEXT_ALIGNMENT_BUTTON } from "../../data-hooks.js";
const textAlignmentIcons = {
  left: TextAlignLeftLight,
  center: TextAlignCenterLight,
  right: TextAlignRightLight,
  justify: JustifyTextLight
};
const TextAlignmentButton = ({ toolbarItem, dataHook }) => {
  const { t } = useContext(RicosContext) || {};
  const [referenceElement, setReferenceElement] = useState(null);
  const alignment = toolbarItem.attributes.selectedAlignment || "left";
  const Icon = textAlignmentIcons[alignment] || TextAlignLeftLight;
  const isModalOpen = useModalState(getIsModalOpen(TABLE_BUTTONS_MODALS_ID.TEXT_ALIGNMENT));
  return /* @__PURE__ */ React.createElement(
    ToolbarButtonComponent,
    {
      ref: setReferenceElement,
      disabled: toolbarItem.attributes.disabled,
      active: isModalOpen,
      tooltip: t("AlignTextDropdownButton_Tooltip"),
      onClick: () => {
        toolbarItem.commands?.click({ referenceElement });
      },
      prefix: Icon,
      suffix: ChevronDownSmall,
      dataHook,
      tabIndex: toolbarItem.presentation.tabIndex
    }
  );
};
const createTextAlignmentButton = (services) => createPluginToolbarButton({
  id: TABLE_BUTTONS.TEXT_ALIGNMENT,
  type: "dropdown",
  dataHook: PLUGIN_TOOLBAR_TEXT_ALIGNMENT_BUTTON,
  getIcon: ({ attributes }) => {
    const alignment = attributes?.selectedAlignment || "left";
    return ALIGNMENT_BUTTON_CONFIGS[alignment]?.icon || textAlignmentIcons.left;
  },
  tooltip: "AlignTextDropdownButton_Tooltip",
  Component: TextAlignmentDropdown,
  getComponentProps: ({ modals, publicCommands }) => {
    return {
      closeModal: () => modals.closeModal(TABLE_BUTTONS_MODALS_ID.TEXT_ALIGNMENT),
      dropdownOptions: Object.values(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 = 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: TextAlignmentDropdown,
      componentProps,
      layout: services.context.isMobile ? "drawer" : "floating",
      positioning: { referenceElement, placement: "bottom" }
    });
  },
  attributes: {
    selectedAlignment: getAlignmentInSelection
  },
  renderer: (toolbarItem) => /* @__PURE__ */ React.createElement(TextAlignmentButton, { toolbarItem })
});
export {
  TextAlignmentButton,
  createTextAlignmentButton
};
//# sourceMappingURL=text-alignment-button.js.map
