import { Tooltip } from "@wix/design-system";
import { ChevronDownSmall } from "@wix/wix-ui-icons-common";
import cx from "classnames";
import React, { useContext, useEffect, useRef, useState } from "react";
import { RICOS_FONT_SIZE_TYPE } from "./../../../../ricos-content/index.js";
import { EditorContext, RicosContext } from "./../../../../ricos-context/index.js";
import { getIsModalOpen, useModalState } from "./../../../../ricos-modals/modal-service-provider.js";
import { Decoration_Type } from "./../../../../ricos-types/index.js";
import toolbarClasses from "./../../../../_shared/toolbar-button/toolbar-button-scss.js";
import styles from "./FontSizeButton-scss.js";
const MAX_FONT_SIZE = 900;
const MIN_FONT_SIZE = 1;
const onInputChange = (e, setInputValue, publicCommands) => {
  const { value } = e.target;
  const valueAsNumber = Number.parseInt(value);
  if (!valueAsNumber && value !== "") {
    return;
  }
  setFontSize(valueAsNumber, setInputValue, publicCommands);
};
const setFontSize = (valueAsNumber, setInputValue, publicCommands) => {
  setInputValue(Number.isNaN(valueAsNumber) ? "" : valueAsNumber);
  const fontSize = Math.min(Math.max(MIN_FONT_SIZE, valueAsNumber), MAX_FONT_SIZE);
  publicCommands.insertDecoration(RICOS_FONT_SIZE_TYPE, { fontSize: `${fontSize}` });
};
const FontSizeButton = ({ toolbarItem, dataHook }) => {
  const { t } = useContext(RicosContext);
  const { getPublicCommands } = useContext(EditorContext);
  const inputRef = useRef(null);
  const [referenceElement, setReferenceElement] = useState(null);
  const [inputValue, setInputValue] = useState("");
  const publicCommands = getPublicCommands?.();
  const isModalOpen = useModalState(getIsModalOpen(`${Decoration_Type.FONT_SIZE}.modal`));
  const selectedFontSize = toolbarItem.attributes.selectedFontSize;
  useEffect(() => {
    setInputValue(selectedFontSize);
  }, [selectedFontSize]);
  const disabled = toolbarItem.attributes.disabled;
  const tooltip = t(toolbarItem.presentation?.tooltip);
  const handleClick = () => toolbarItem.commands?.click({ referenceElement });
  const handleKeyDown = (e) => {
    const currentFontSize = parseInt(selectedFontSize, 10);
    if (e.key === "ArrowUp") {
      e.preventDefault();
      const newFontSize = Math.min(currentFontSize + 1, MAX_FONT_SIZE);
      setFontSize(newFontSize, setInputValue, publicCommands);
    } else if (e.key === "ArrowDown") {
      e.preventDefault();
      const newFontSize = Math.max(currentFontSize - 1, MIN_FONT_SIZE);
      setFontSize(newFontSize, setInputValue, publicCommands);
    } else if (e.key === "Enter" || e.key === "Space") {
      e.preventDefault();
      handleClick();
    }
  };
  return /* @__PURE__ */ React.createElement(Tooltip, { key: tooltip, content: tooltip, size: "small", moveBy: { y: 8 } }, /* @__PURE__ */ React.createElement(
    "div",
    {
      className: cx(toolbarClasses.toolbarButton, {
        [toolbarClasses.active]: isModalOpen,
        [toolbarClasses.disabled]: disabled
      }),
      ref: setReferenceElement
    },
    /* @__PURE__ */ React.createElement(
      "div",
      {
        "data-hook": dataHook,
        className: styles.fontSizeModalButton,
        role: "button",
        onClick: handleClick,
        onKeyDown: handleKeyDown,
        tabIndex: -1,
        "data-avoid-focus": true
      },
      /* @__PURE__ */ React.createElement(
        "input",
        {
          "aria-label": t("FormattingToolbar_CustomFontSizeButton_Tooltip"),
          className: styles.fontSizeModalInputButton,
          required: true,
          value: inputValue,
          onChange: (e) => onInputChange(e, setInputValue, publicCommands),
          onClick: () => inputRef.current?.select(),
          ref: inputRef,
          tabIndex: toolbarItem.presentation.tabIndex
        }
      ),
      /* @__PURE__ */ React.createElement(ChevronDownSmall, null)
    )
  ));
};
export {
  FontSizeButton
};
//# sourceMappingURL=font-size-button.js.map
