"use strict";
const React = require("react");
const index_es = require("../../../../../../number-precision@1.5.2/node_modules/number-precision/build/index.es.js");
const index$1 = require("../../icon/react-icon/IconUp/index.js");
const index$2 = require("../../icon/react-icon/IconDown/index.js");
const index$4 = require("../../icon/react-icon/IconPlus/index.js");
const index$3 = require("../../icon/react-icon/IconMinus/index.js");
const is = require("../_util/is.js");
const classNames = require("../_util/classNames.js");
const keycode = require("../_util/keycode.js");
const index = require("../ConfigProvider/index.js");
const input = require("../Input/input.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const omit = require("../_util/omit.js");
const utils = require("./utils.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
index_es.default.enableBoundaryChecking(false);
var AUTO_CHANGE_INTERVAL = 200;
var AUTO_CHANGE_START_DELAY = 1e3;
var defaultProps = {
  max: Infinity,
  min: -Infinity,
  step: 1,
  mode: "embed",
  parser: function(input2) {
    return input2.replace(/[^\w\.-]+/g, "");
  }
};
function InputNumber(baseProps, ref) {
  var _a;
  var _b = React.useContext(index.ConfigContext), getPrefixCls = _b.getPrefixCls, ctxSize = _b.size, componentConfig = _b.componentConfig;
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.InputNumber);
  var className = props.className, style = props.style, defaultValue = props.defaultValue, disabled = props.disabled, error = props.error, readOnly = props.readOnly, placeholder = props.placeholder, hideControl = props.hideControl, suffix = props.suffix, prefix = props.prefix, icons = props.icons, mode = props.mode, size = props.size, step = props.step, precision = props.precision, min = props.min, max = props.max, parser = props.parser, formatter = props.formatter, onBlur = props.onBlur, onFocus = props.onFocus, onChange = props.onChange, onKeyDown = props.onKeyDown, rest = __rest(props, ["className", "style", "defaultValue", "disabled", "error", "readOnly", "placeholder", "hideControl", "suffix", "prefix", "icons", "mode", "size", "step", "precision", "min", "max", "parser", "formatter", "onBlur", "onFocus", "onChange", "onKeyDown"]);
  var prefixCls = getPrefixCls("input-number");
  var mergedSize = size || ctxSize;
  var mergedPrecision = function() {
    if (is.isNumber(precision)) {
      var decimal = ("" + step).split(".")[1];
      var stepPrecision = decimal && decimal.length || 0;
      return Math.max(stepPrecision, precision);
    }
    return null;
  }();
  var _c = __read(React.useState("defaultValue" in props ? defaultValue : void 0), 2), innerValue = _c[0], setInnerValue = _c[1];
  var value = function() {
    var mergedValue = "value" in props ? props.value : innerValue;
    return typeof mergedValue === "string" && mergedValue !== "" ? +mergedValue : mergedValue;
  }();
  var _d = __read(React.useState(""), 2), inputValue = _d[0], setInputValue = _d[1];
  var _e = __read(React.useState(false), 2), isOutOfRange = _e[0], setIsOutOfRange = _e[1];
  var _f = __read(React.useState(false), 2), isUserInputting = _f[0], setIsUserInputting = _f[1];
  var isEmptyValue = value === "" || value === void 0 || value === null;
  var refAutoTimer = React.useRef(null);
  var refInput = React.useRef(null);
  var refHasOperateSincePropValueChanged = React.useRef(false);
  React.useImperativeHandle(ref, function() {
    return refInput.current;
  }, []);
  var setValue = function(newVal) {
    setInnerValue(newVal);
    var newValue = is.isNumber(+newVal) ? +newVal : void 0;
    if (newValue !== value) {
      onChange && onChange(newValue);
    }
  };
  var stop = function() {
    refAutoTimer.current && clearTimeout(refAutoTimer.current);
    refAutoTimer.current = null;
  };
  var getLegalValue = React.useCallback(function(changedValue) {
    var finalValue = Number(changedValue);
    if (!changedValue && changedValue !== 0) {
      finalValue = void 0;
    } else if (!is.isNumber(finalValue)) {
      finalValue = changedValue === "-" ? changedValue : "";
    }
    if (finalValue < min) {
      finalValue = min;
    }
    if (finalValue > max) {
      finalValue = max;
    }
    return is.isNumber(finalValue) ? is.isNumber(mergedPrecision) ? Number(utils.toFixed(finalValue, mergedPrecision)) : finalValue : void 0;
  }, [min, max, mergedPrecision]);
  React.useEffect(function() {
    return function() {
      return stop();
    };
  }, []);
  React.useEffect(function() {
    refHasOperateSincePropValueChanged.current = false;
  }, [props.value]);
  React.useEffect(function() {
    var _isOutOfRange = is.isNumber(min) && value < min || is.isNumber(max) && value > max;
    if (_isOutOfRange && refHasOperateSincePropValueChanged.current) {
      setValue(getLegalValue(value));
    }
    setIsOutOfRange(_isOutOfRange);
  }, [min, max, value, getLegalValue]);
  var handleArrowKey = function(event, method, needRepeat) {
    if (needRepeat === void 0) {
      needRepeat = false;
    }
    event.persist();
    event.preventDefault();
    setIsUserInputting(false);
    if (disabled || readOnly) {
      return;
    }
    var finalValue = min === -Infinity ? 0 : min;
    if (!isEmptyValue) {
      finalValue = index_es.default[method](value, step);
    }
    setValue(getLegalValue(finalValue));
    refInput.current && refInput.current.focus();
    if (needRepeat) {
      var isFirstRepeat = refAutoTimer.current === null;
      refAutoTimer.current = setTimeout(function() {
        return event.target.dispatchEvent(event.nativeEvent);
      }, isFirstRepeat ? AUTO_CHANGE_START_DELAY : AUTO_CHANGE_INTERVAL);
    }
  };
  var getDisplayInputValue = function() {
    var _value;
    if (isUserInputting) {
      _value = inputValue;
    } else if (is.isNumber(value) && is.isNumber(mergedPrecision)) {
      _value = utils.toFixed(value, mergedPrecision);
    } else if (value == null) {
      _value = "";
    } else {
      _value = utils.toSafeString(value);
    }
    return formatter ? formatter(_value) : _value;
  };
  var inputEventHandlers = {
    onChange: function(value2) {
      setIsUserInputting(true);
      var targetValue = value2.trim().replace(/。/g, ".");
      targetValue = parser ? parser(targetValue) : targetValue;
      if (is.isNumber(+targetValue) || targetValue === "-" || !targetValue || targetValue === ".") {
        var formatValue = getLegalValue(targetValue);
        setInputValue(targetValue);
        setValue(formatValue);
      }
    },
    onKeyDown: function(e) {
      e.stopPropagation();
      var key = e.key;
      if (key === keycode.ArrowDown.key) {
        handleArrowKey(e, "minus");
      } else if (key === keycode.ArrowUp.key) {
        handleArrowKey(e, "plus");
      }
      stop();
      onKeyDown && onKeyDown(e);
    },
    onFocus: function(e) {
      var _a2, _b2;
      refHasOperateSincePropValueChanged.current = true;
      setInputValue((_b2 = (_a2 = refInput.current) === null || _a2 === void 0 ? void 0 : _a2.dom) === null || _b2 === void 0 ? void 0 : _b2.value);
      onFocus && onFocus(e);
    },
    onBlur: function(e) {
      setValue(getLegalValue(value));
      setIsUserInputting(false);
      onBlur && onBlur(e);
    }
  };
  var getControlButtonEventsHandlers = function(method) {
    return readOnly ? {} : {
      onMouseDown: function(e) {
        return handleArrowKey(e, method, true);
      },
      onMouseLeave: stop,
      onMouseUp: stop
    };
  };
  var shouldRenderButton = !hideControl && mode === "button";
  var shouldRenderLayer = !hideControl && !readOnly && mode === "embed";
  var renderStepButton = function(method, icon) {
    var _a2;
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      ...__assign({
        className: classNames(prefixCls + "-step-button", (_a2 = {}, _a2[prefixCls + "-step-button-disabled"] = disabled || (method === "plus" ? +value >= +max : +value <= +min), _a2))
      }, getControlButtonEventsHandlers(method)),
      children: icon
    });
  };
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(input.default, {
    ...__assign({
      _ignorePropsFromGlobal: true,
      role: "spinbutton",
      "aria-valuemax": max,
      "aria-valuemin": min,
      "aria-valuenow": value
    }, omit(rest, ["allowClear"]), inputEventHandlers, {
      style,
      className: classNames(prefixCls, prefixCls + "-mode-" + mode, prefixCls + "-size-" + mergedSize, (_a = {}, _a[prefixCls + "-readonly"] = readOnly, _a[prefixCls + "-illegal-value"] = !isEmptyValue && isOutOfRange, _a), className),
      ref: refInput,
      size: mergedSize,
      error,
      disabled,
      readOnly,
      value: getDisplayInputValue(),
      placeholder,
      prefix: prefix && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        className: prefixCls + "-prefix",
        children: prefix
      }),
      suffix: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
        children: [shouldRenderLayer && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
          className: prefixCls + "-step-layer",
          children: [renderStepButton("plus", icons && icons.up ? icons.up : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {})), renderStepButton("minus", icons && icons.down ? icons.down : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {}))]
        }), suffix && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
          className: prefixCls + "-suffix",
          children: suffix
        })]
      }),
      addBefore: shouldRenderButton && renderStepButton("minus", icons && icons.minus ? icons.minus : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3, {})),
      addAfter: shouldRenderButton && renderStepButton("plus", icons && icons.plus ? icons.plus : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$4, {}))
    })
  });
}
var InputNumberComponent = React__default.default.forwardRef(InputNumber);
InputNumberComponent.displayName = "InputNumber";
const InputNumber$1 = InputNumberComponent;
module.exports = InputNumber$1;
//# sourceMappingURL=index.js.map
