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

// src/pro/NumericStepper/index.tsx
var NumericStepper_exports = {};
__export(NumericStepper_exports, {
  default: () => NumericStepper_default
});
module.exports = __toCommonJS(NumericStepper_exports);
var React = __toESM(require("react"));
var import_react = require("react");
var import_classnames = __toESM(require("classnames"));
var import_utils = require("@pisell/utils");
var import_keyboards = require("../keyboards");
var import_useThemeTokens = __toESM(require("../../hooks/useThemeTokens"));
var import_index = require("./index.less");
var NumericStepper = ({
  value,
  defaultValue = 0,
  min = 0,
  max = Infinity,
  step = 1,
  disabled = false,
  onChange,
  className,
  styles,
  size = "middle",
  shape = "round",
  minusIcon,
  plusIcon,
  horizontalPadding = 0,
  variant = "separated",
  enableInput = true,
  displayMaxWidth,
  theme,
  plusOnlyAtZero = true
}) => {
  const [open, setOpen] = (0, import_react.useState)(false);
  const [internalValue, setInternalValue] = (0, import_react.useState)(
    value !== void 0 ? value : defaultValue
  );
  const _isMobile = (0, import_react.useMemo)(() => (0, import_utils.isMobile)(), [import_utils.isMobile]);
  const { colorPrimary } = (0, import_useThemeTokens.default)(theme);
  const [tempValue, setTempValue] = (0, import_react.useState)("");
  const currentValue = value !== void 0 ? value : internalValue;
  (0, import_react.useEffect)(() => {
    if (value !== void 0) {
      setInternalValue(value);
    }
  }, [value]);
  const handleValueChange = (0, import_react.useCallback)(
    (newValue) => {
      const clampedValue = Math.max(min, Math.min(max, newValue));
      if (value === void 0) {
        setInternalValue(clampedValue);
      }
      onChange == null ? void 0 : onChange(clampedValue);
    },
    [min, max, value, onChange]
  );
  const handleDecrease = (0, import_react.useCallback)(() => {
    if (disabled) return;
    const newValue = currentValue - step;
    if (newValue >= min) {
      handleValueChange(newValue);
    }
  }, [currentValue, step, min, disabled, handleValueChange]);
  const handleIncrease = (0, import_react.useCallback)(() => {
    if (disabled) return;
    if (currentValue < min) {
      handleValueChange(min);
      return;
    }
    const newValue = currentValue + step;
    if (newValue <= max) {
      handleValueChange(newValue);
    }
  }, [currentValue, step, min, max, disabled, handleValueChange]);
  const handleChange = (e) => {
    if (!e) {
      setTempValue(min.toString());
      return;
    }
    if (e === "delete") {
      const newTempValue = tempValue.slice(0, -1);
      setTempValue(newTempValue || "0");
      return;
    } else if (e === "verify") {
      const newValue = parseInt(tempValue) || 0;
      const clampedValue = Math.max(min, Math.min(max, newValue));
      onChange == null ? void 0 : onChange(clampedValue);
      setOpen(false);
      return;
    } else {
      setTempValue(e);
    }
  };
  const handleNumberClick = () => {
    if (disabled || !enableInput) return;
    setTempValue(currentValue.toString());
    if (_isMobile) {
      setOpen(true);
    }
  };
  const handleOpenChange = (visible) => {
    setOpen(visible);
    if (visible) {
      setTempValue(currentValue.toString());
    } else {
      const tempNumber = parseInt(tempValue) || 0;
      const clampedValue = Math.max(min, Math.min(max, tempNumber));
      if (clampedValue !== value) {
        onChange == null ? void 0 : onChange(clampedValue);
      }
      setTempValue(currentValue.toString());
    }
  };
  const isDecreaseDisabled = disabled || currentValue <= min;
  const isIncreaseDisabled = disabled || currentValue >= max;
  const isPlusOnly = variant === "capsule" ? false : Boolean(
    plusOnlyAtZero && (currentValue === 0 || currentValue === void 0)
  );
  const outOfRange = currentValue < min || currentValue > max;
  const stepperClasses = (0, import_classnames.default)(
    "numeric-stepper",
    `numeric-stepper-${size}`,
    `numeric-stepper-${variant}`,
    {
      // shape 仅在分离式下生效
      [`numeric-stepper-${shape}`]: variant === "separated",
      "numeric-stepper-disabled": disabled,
      "numeric-stepper-simple": currentValue === 0 || currentValue === void 0,
      "numeric-stepper-out-of-range": outOfRange,
      "numeric-stepper-plus-only": isPlusOnly,
      "numeric-stepper-input-disabled": !enableInput
    },
    className
  );
  const _minus = !isPlusOnly && /* @__PURE__ */ React.createElement(
    "div",
    {
      className: "numeric-stepper-button-wrapper numeric-stepper-minus-button-wrapper",
      onClick: handleDecrease,
      style: {
        paddingLeft: horizontalPadding
      }
    },
    /* @__PURE__ */ React.createElement(
      "button",
      {
        type: "button",
        className: (0, import_classnames.default)(
          "numeric-stepper-button",
          "numeric-stepper-minus",
          {
            "numeric-stepper-button-disabled": isDecreaseDisabled
          }
        ),
        disabled: isDecreaseDisabled
      },
      minusIcon || /* @__PURE__ */ React.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none" }, /* @__PURE__ */ React.createElement(
        "path",
        {
          d: "M3 8h10",
          stroke: "currentColor",
          strokeWidth: "1.5",
          strokeLinecap: "round"
        }
      ))
    )
  );
  const _display = (0, import_react.useMemo)(() => {
    if (isPlusOnly) {
      return null;
    }
    const displayWrapperStyle = variant === "capsule" && displayMaxWidth ? {
      maxWidth: typeof displayMaxWidth === "number" ? `${displayMaxWidth}px` : displayMaxWidth
    } : {};
    const displayElement = /* @__PURE__ */ React.createElement(
      "span",
      {
        className: "numeric-stepper-display-wrapper",
        onClick: handleNumberClick,
        style: displayWrapperStyle
      },
      /* @__PURE__ */ React.createElement("span", { className: (0, import_classnames.default)("numeric-stepper-display") }, currentValue)
    );
    if (!enableInput) {
      return displayElement;
    }
    if (_isMobile) {
      return displayElement;
    }
    return /* @__PURE__ */ React.createElement(
      import_keyboards.NumberKeyboard,
      {
        max,
        min,
        originalValue: currentValue.toString(),
        defaultValue: currentValue.toString(),
        onEnter: (newValue) => {
          handleValueChange == null ? void 0 : handleValueChange(Number(newValue));
          setOpen(false);
        },
        containerProps: {
          className: "numeric-stepper-keyboard-popover",
          renderType: "popover",
          open,
          onOpenChange: handleOpenChange,
          content: /* @__PURE__ */ React.createElement(
            import_keyboards.NumberKeyboard,
            {
              max,
              min,
              originalValue: currentValue.toString(),
              defaultValue: currentValue.toString(),
              onEnter: (newValue) => {
                handleValueChange == null ? void 0 : handleValueChange(Number(newValue));
                setOpen(false);
              }
            }
          )
        }
      },
      displayElement
    );
  }, [
    _isMobile,
    open,
    isPlusOnly,
    currentValue,
    handleNumberClick,
    handleOpenChange,
    handleChange,
    max,
    min,
    onChange,
    tempValue,
    enableInput,
    variant,
    displayMaxWidth
  ]);
  const _plus = /* @__PURE__ */ React.createElement(
    "div",
    {
      className: "numeric-stepper-button-wrapper numeric-stepper-plus-button-wrapper",
      onClick: handleIncrease,
      style: {
        paddingLeft: isPlusOnly ? horizontalPadding : 0,
        paddingRight: horizontalPadding
      }
    },
    /* @__PURE__ */ React.createElement(
      "button",
      {
        type: "button",
        className: (0, import_classnames.default)(
          "numeric-stepper-button",
          "numeric-stepper-plus",
          {
            "numeric-stepper-button-disabled": isIncreaseDisabled
          }
        ),
        disabled: isIncreaseDisabled
      },
      plusIcon || /* @__PURE__ */ React.createElement("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none" }, /* @__PURE__ */ React.createElement(
        "path",
        {
          d: "M8 3v10M3 8h10",
          stroke: "currentColor",
          strokeWidth: "1.5",
          strokeLinecap: "round"
        }
      ))
    )
  );
  return /* @__PURE__ */ React.createElement(
    "div",
    {
      className: stepperClasses,
      style: {
        ...(styles == null ? void 0 : styles.container) || {},
        ["--color-primary"]: colorPrimary
      },
      onClick: (e) => e.stopPropagation()
    },
    _minus,
    _display,
    _plus,
    _isMobile && /* @__PURE__ */ React.createElement(
      import_keyboards.NumberKeyboard,
      {
        containerProps: {
          className: "numeric-stepper-keyboard-modal",
          renderType: "modal",
          open,
          onCancel: () => {
            setOpen(false);
          }
        },
        max,
        min,
        defaultValue: currentValue.toString(),
        originalValue: currentValue.toString(),
        onEnter: (newValue) => {
          handleValueChange == null ? void 0 : handleValueChange(Number(newValue));
          setOpen(false);
        }
      }
    )
  );
};
var NumericStepper_default = NumericStepper;
