"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 font_size_button_exports = {};
__export(font_size_button_exports, {
  FontSizeButton: () => FontSizeButton
});
module.exports = __toCommonJS(font_size_button_exports);
var import_design_system = require("@wix/design-system");
var import_wix_ui_icons_common = require("@wix/wix-ui-icons-common");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_ricos_content = require("./../../../../ricos-content/index.js");
var import_ricos_context = require("./../../../../ricos-context/index.js");
var import_modal_service_provider = require("./../../../../ricos-modals/modal-service-provider.js");
var import_ricos_types = require("./../../../../ricos-types/index.js");
var import_toolbar_button_scss = __toESM(require("./../../../../_shared/toolbar-button/toolbar-button-scss.js"));
var import_FontSizeButton_scss = __toESM(require("./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(import_ricos_content.RICOS_FONT_SIZE_TYPE, { fontSize: `${fontSize}` });
};
const FontSizeButton = ({ toolbarItem, dataHook }) => {
  const { t } = (0, import_react.useContext)(import_ricos_context.RicosContext);
  const { getPublicCommands } = (0, import_react.useContext)(import_ricos_context.EditorContext);
  const inputRef = (0, import_react.useRef)(null);
  const [referenceElement, setReferenceElement] = (0, import_react.useState)(null);
  const [inputValue, setInputValue] = (0, import_react.useState)("");
  const publicCommands = getPublicCommands?.();
  const isModalOpen = (0, import_modal_service_provider.useModalState)((0, import_modal_service_provider.getIsModalOpen)(`${import_ricos_types.Decoration_Type.FONT_SIZE}.modal`));
  const selectedFontSize = toolbarItem.attributes.selectedFontSize;
  (0, import_react.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__ */ import_react.default.createElement(import_design_system.Tooltip, { key: tooltip, content: tooltip, size: "small", moveBy: { y: 8 } }, /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)(import_toolbar_button_scss.default.toolbarButton, {
        [import_toolbar_button_scss.default.active]: isModalOpen,
        [import_toolbar_button_scss.default.disabled]: disabled
      }),
      ref: setReferenceElement
    },
    /* @__PURE__ */ import_react.default.createElement(
      "div",
      {
        "data-hook": dataHook,
        className: import_FontSizeButton_scss.default.fontSizeModalButton,
        role: "button",
        onClick: handleClick,
        onKeyDown: handleKeyDown,
        tabIndex: -1,
        "data-avoid-focus": true
      },
      /* @__PURE__ */ import_react.default.createElement(
        "input",
        {
          "aria-label": t("FormattingToolbar_CustomFontSizeButton_Tooltip"),
          className: import_FontSizeButton_scss.default.fontSizeModalInputButton,
          required: true,
          value: inputValue,
          onChange: (e) => onInputChange(e, setInputValue, publicCommands),
          onClick: () => inputRef.current?.select(),
          ref: inputRef,
          tabIndex: toolbarItem.presentation.tabIndex
        }
      ),
      /* @__PURE__ */ import_react.default.createElement(import_wix_ui_icons_common.ChevronDownSmall, null)
    )
  ));
};
//# sourceMappingURL=font-size-button.js.map
