"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);

// packages/components/src/font-size-picker/index.tsx
var font_size_picker_exports = {};
__export(font_size_picker_exports, {
  FontSizePicker: () => FontSizePicker,
  default: () => font_size_picker_default
});
module.exports = __toCommonJS(font_size_picker_exports);
var import_i18n = require("@wordpress/i18n");
var import_icons = require("@wordpress/icons");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_button = require("../button/index.cjs");
var import_range_control = __toESM(require("../range-control/index.cjs"), 1);
var import_flex = require("../flex/index.cjs");
var import_unit_control = __toESM(require("../unit-control/index.cjs"), 1);
var import_styles = require("./styles.cjs");
var import_spacer = require("../spacer/index.cjs");
var import_font_size_picker_select = __toESM(require("./font-size-picker-select.cjs"), 1);
var import_font_size_picker_toggle_group = __toESM(require("./font-size-picker-toggle-group.cjs"), 1);
var import_deprecated_36px_size = require("../utils/deprecated-36px-size.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var DEFAULT_UNITS = ["px", "em", "rem", "vw", "vh"];
var MAX_TOGGLE_GROUP_SIZES = 5;
var UnforwardedFontSizePicker = (props, ref) => {
  const {
    __next40pxDefaultSize = false,
    fallbackFontSize,
    fontSizes = [],
    disableCustomFontSizes = false,
    onChange,
    size = "default",
    units: unitsProp = DEFAULT_UNITS,
    value,
    valueMode = "literal",
    withSlider = false,
    withReset = true
  } = props;
  const labelId = (0, import_compose.useInstanceId)(UnforwardedFontSizePicker, "font-size-picker-label");
  const units = (0, import_unit_control.useCustomUnits)({
    availableUnits: unitsProp
  });
  const selectedFontSize = (() => {
    if (!value) {
      return void 0;
    }
    if (valueMode === "slug") {
      return fontSizes.find((fontSize) => fontSize.slug === value);
    }
    return fontSizes.find((fontSize) => fontSize.size === value);
  })();
  const isCustomValue = !!value && !selectedFontSize;
  const [userRequestedCustom, setUserRequestedCustom] = (0, import_element.useState)(isCustomValue);
  const resolvedValueForControls = valueMode === "slug" ? selectedFontSize?.size : value;
  let currentPickerType;
  if (!disableCustomFontSizes && userRequestedCustom) {
    currentPickerType = "custom";
  } else {
    currentPickerType = fontSizes.length > MAX_TOGGLE_GROUP_SIZES ? "select" : "togglegroup";
  }
  if (fontSizes.length === 0 && disableCustomFontSizes) {
    return null;
  }
  const hasUnits = typeof resolvedValueForControls === "string" || typeof fontSizes[0]?.size === "string";
  const [valueQuantity, valueUnit] = (0, import_unit_control.parseQuantityAndUnitFromRawValue)(resolvedValueForControls, units);
  const isValueUnitRelative = !!valueUnit && ["em", "rem", "vw", "vh"].includes(valueUnit);
  const isDisabled = value === void 0;
  (0, import_deprecated_36px_size.maybeWarnDeprecated36pxSize)({
    componentName: "FontSizePicker",
    __next40pxDefaultSize,
    size
  });
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Container, {
    ref,
    className: "components-font-size-picker",
    "aria-labelledby": labelId,
    children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_spacer.Spacer, {
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_styles.Header, {
        className: "components-font-size-picker__header",
        children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.HeaderLabel, {
          id: labelId,
          children: (0, import_i18n.__)("Font size")
        }), !disableCustomFontSizes && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_styles.HeaderToggle, {
          label: currentPickerType === "custom" ? (0, import_i18n.__)("Use size preset") : (0, import_i18n.__)("Set custom size"),
          icon: import_icons.settings,
          onClick: () => setUserRequestedCustom(!userRequestedCustom),
          isPressed: currentPickerType === "custom",
          size: "small"
        })]
      })
    }), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
      children: [currentPickerType === "select" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_font_size_picker_select.default, {
        __next40pxDefaultSize,
        fontSizes,
        value,
        valueMode,
        disableCustomFontSizes,
        size,
        onChange: (newValue, selectedItem) => {
          if (newValue === void 0) {
            onChange?.(void 0, selectedItem);
          } else {
            onChange?.(hasUnits ? newValue : Number(newValue), selectedItem);
          }
        },
        onSelectCustom: () => setUserRequestedCustom(true)
      }), currentPickerType === "togglegroup" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_font_size_picker_toggle_group.default, {
        fontSizes,
        value,
        valueMode,
        __next40pxDefaultSize,
        size,
        onChange: (newValue, selectedItem) => {
          if (newValue === void 0) {
            onChange?.(void 0, selectedItem);
          } else {
            onChange?.(hasUnits ? newValue : Number(newValue), selectedItem);
          }
        }
      }), currentPickerType === "custom" && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_flex.Flex, {
        className: "components-font-size-picker__custom-size-control",
        children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_flex.FlexItem, {
          isBlock: true,
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_unit_control.default, {
            __next40pxDefaultSize,
            __shouldNotWarnDeprecated36pxSize: true,
            label: (0, import_i18n.__)("Font size"),
            labelPosition: "top",
            hideLabelFromVision: true,
            value: hasUnits ? `${valueQuantity !== null && valueQuantity !== void 0 ? valueQuantity : ""}${valueUnit !== null && valueUnit !== void 0 ? valueUnit : ""}` : resolvedValueForControls,
            onChange: (newValue) => {
              setUserRequestedCustom(true);
              if (newValue === void 0 || newValue === "") {
                onChange?.(void 0);
              } else {
                onChange?.(hasUnits ? newValue : parseInt(newValue, 10));
              }
            },
            size,
            units: hasUnits ? units : [],
            min: 0
          })
        }), withSlider && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_flex.FlexItem, {
          isBlock: true,
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_spacer.Spacer, {
            marginX: 2,
            marginBottom: 0,
            children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_range_control.default, {
              __next40pxDefaultSize,
              __shouldNotWarnDeprecated36pxSize: true,
              className: "components-font-size-picker__custom-input",
              label: (0, import_i18n.__)("Font size"),
              hideLabelFromVision: true,
              value: valueQuantity,
              initialPosition: fallbackFontSize,
              withInputField: false,
              onChange: (newValue) => {
                setUserRequestedCustom(true);
                if (newValue === void 0) {
                  onChange?.(void 0);
                } else if (hasUnits) {
                  onChange?.(newValue + (valueUnit !== null && valueUnit !== void 0 ? valueUnit : "px"));
                } else {
                  onChange?.(newValue);
                }
              },
              min: 0,
              max: isValueUnitRelative ? 10 : 100,
              step: isValueUnitRelative ? 0.1 : 1
            })
          })
        }), withReset && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_flex.FlexItem, {
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_button.Button, {
            disabled: isDisabled,
            accessibleWhenDisabled: true,
            onClick: () => {
              onChange?.(void 0);
            },
            variant: "secondary",
            __next40pxDefaultSize: true,
            size: size === "__unstable-large" || props.__next40pxDefaultSize ? "default" : "small",
            children: (0, import_i18n.__)("Reset")
          })
        })]
      })]
    })]
  });
};
var FontSizePicker = (0, import_element.forwardRef)(UnforwardedFontSizePicker);
var font_size_picker_default = FontSizePicker;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FontSizePicker
});
//# sourceMappingURL=index.cjs.map
