import Re from "../../@babel/runtime/helpers/esm/extends.mjs";
import D from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import je from "../../@babel/runtime/helpers/esm/typeof.mjs";
import K from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import Se from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import c, { toFixed as Y } from "../../@rc-component/mini-decimal/es/MiniDecimal.mjs";
import { getNumberPrecision as ye, num2str as Le, validateNumber as ze } from "../../@rc-component/mini-decimal/es/numberUtil.mjs";
import Ge from "../../classnames/index.mjs";
import Je from "../../rc-input/es/BaseInput.mjs";
import * as u from "react";
import { useLayoutUpdateEffect as Z } from "../../rc-util/es/hooks/useLayoutEffect.mjs";
import { triggerFocus as Qe } from "../../rc-input/es/utils/commonUtils.mjs";
import { composeRef as Ve } from "../../rc-util/es/ref.mjs";
import Xe from "./hooks/useCursor.mjs";
import Ye from "./StepHandler.mjs";
import { getDecupleSteps as xe } from "./utils/numberUtil.mjs";
import Ze from "./hooks/useFrame.mjs";
var Te = ["prefixCls", "className", "style", "min", "max", "step", "defaultValue", "value", "disabled", "readOnly", "upHandler", "downHandler", "keyboard", "controls", "classNames", "stringMode", "parser", "formatter", "precision", "decimalSeparator", "onChange", "onInput", "onPressEnter", "onStep", "changeOnBlur"], ea = ["disabled", "style", "prefixCls", "value", "prefix", "suffix", "addonBefore", "addonAfter", "classes", "className", "classNames"], be = function(v, l) {
  return v || l.isEmpty() ? l.toString() : l.toNumber();
}, Ce = function(v) {
  var l = c(v);
  return l.isInvalidate() ? null : l;
}, aa = /* @__PURE__ */ u.forwardRef(function(a, v) {
  var l, h = a.prefixCls, s = h === void 0 ? "rc-input-number" : h, M = a.className, k = a.style, C = a.min, R = a.max, B = a.step, d = B === void 0 ? 1 : B, g = a.defaultValue, o = a.value, S = a.disabled, p = a.readOnly, O = a.upHandler, T = a.downHandler, P = a.keyboard, ee = a.controls, Ee = ee === void 0 ? !0 : ee, H = a.classNames, ae = a.stringMode, $ = a.parser, N = a.formatter, I = a.precision, y = a.decimalSeparator, q = a.onChange, W = a.onInput, j = a.onPressEnter, L = a.onStep, re = a.changeOnBlur, _e = re === void 0 ? !0 : re, we = Se(a, Te), te = "".concat(s, "-input"), U = u.useRef(null), De = u.useState(!1), ne = K(De, 2), ue = ne[0], ie = ne[1], m = u.useRef(!1), V = u.useRef(!1), A = u.useRef(!1), he = u.useState(function() {
    return c(o ?? g);
  }), le = K(he, 2), i = le[0], oe = le[1];
  function Be(r) {
    o === void 0 && oe(r);
  }
  var z = u.useCallback(function(r, e) {
    if (!e)
      return I >= 0 ? I : Math.max(ye(r), ye(d));
  }, [I, d]), G = u.useCallback(function(r) {
    var e = String(r);
    if ($)
      return $(e);
    var n = e;
    return y && (n = n.replace(y, ".")), n.replace(/[^\w.-]+/g, "");
  }, [$, y]), J = u.useRef(""), se = u.useCallback(function(r, e) {
    if (N)
      return N(r, {
        userTyping: e,
        input: String(J.current)
      });
    var n = typeof r == "number" ? Le(r) : r;
    if (!e) {
      var t = z(n, e);
      if (ze(n) && (y || t >= 0)) {
        var f = y || ".";
        n = Y(n, f, t);
      }
    }
    return n;
  }, [N, z, y]), $e = u.useState(function() {
    var r = g ?? o;
    return i.isInvalidate() && ["string", "number"].includes(je(r)) ? Number.isNaN(r) ? "" : r : se(i.toString(), !1);
  }), fe = K($e, 2), E = fe[0], ce = fe[1];
  J.current = E;
  function _(r, e) {
    ce(se(
      // Invalidate number is sometime passed by external control, we should let it go
      // Otherwise is controlled by internal interactive logic which check by userTyping
      // You can ref 'show limited value when input is not focused' test for more info.
      r.isInvalidate() ? r.toString(!1) : r.toString(!e),
      e
    ));
  }
  var x = u.useMemo(function() {
    return Ce(R);
  }, [R, I]), b = u.useMemo(function() {
    return Ce(C);
  }, [C, I]), de = u.useMemo(function() {
    return !x || !i || i.isInvalidate() ? !1 : x.lessEquals(i);
  }, [x, i]), me = u.useMemo(function() {
    return !b || !i || i.isInvalidate() ? !1 : i.lessEquals(b);
  }, [b, i]), Ue = Xe(U.current, ue), ve = K(Ue, 2), Ae = ve[0], Fe = ve[1], pe = function(e) {
    return x && !e.lessEquals(x) ? x : b && !b.lessEquals(e) ? b : null;
  }, Q = function(e) {
    return !pe(e);
  }, F = function(e, n) {
    var t = e, f = Q(t) || t.isEmpty();
    if (!t.isEmpty() && !n && (t = pe(t) || t, f = !0), !p && !S && f) {
      var w = t.toString(), X = z(w, n);
      return X >= 0 && (t = c(Y(w, ".", X)), Q(t) || (t = c(Y(w, ".", X, !0)))), t.equals(i) || (Be(t), q == null || q(t.isEmpty() ? null : be(ae, t)), o === void 0 && _(t, n)), t;
    }
    return i;
  }, Ke = Ze(), ge = function r(e) {
    if (Ae(), J.current = e, ce(e), !V.current) {
      var n = G(e), t = c(n);
      t.isNaN() || F(t, !0);
    }
    W == null || W(e), Ke(function() {
      var f = e;
      $ || (f = e.replace(/。/g, ".")), f !== e && r(f);
    });
  }, Me = function() {
    V.current = !0;
  }, ke = function() {
    V.current = !1, ge(U.current.value);
  }, Oe = function(e) {
    ge(e.target.value);
  }, Ne = function(e) {
    var n;
    if (!(e && de || !e && me)) {
      m.current = !1;
      var t = c(A.current ? xe(d) : d);
      e || (t = t.negate());
      var f = (i || c(0)).add(t.toString()), w = F(f, !1);
      L == null || L(be(ae, w), {
        offset: A.current ? xe(d) : d,
        type: e ? "up" : "down"
      }), (n = U.current) === null || n === void 0 || n.focus();
    }
  }, Ie = function(e) {
    var n = c(G(E)), t = n;
    n.isNaN() ? t = F(i, e) : t = F(n, e), o !== void 0 ? _(i, !1) : t.isNaN() || _(t, !1);
  }, Pe = function() {
    m.current = !0;
  }, He = function(e) {
    var n = e.key, t = e.shiftKey;
    m.current = !0, A.current = t, n === "Enter" && (V.current || (m.current = !1), Ie(!1), j == null || j(e)), P !== !1 && !V.current && ["Up", "ArrowUp", "Down", "ArrowDown"].includes(n) && (Ne(n === "Up" || n === "ArrowUp"), e.preventDefault());
  }, qe = function() {
    m.current = !1, A.current = !1;
  }, We = function() {
    _e && Ie(!1), ie(!1), m.current = !1;
  };
  return Z(function() {
    i.isInvalidate() || _(i, !1);
  }, [I, N]), Z(function() {
    var r = c(o);
    oe(r);
    var e = c(G(E));
    (!r.equals(e) || !m.current || N) && _(r, m.current);
  }, [o]), Z(function() {
    N && Fe();
  }, [E]), /* @__PURE__ */ u.createElement("div", {
    className: Ge(s, H == null ? void 0 : H.input, M, (l = {}, D(l, "".concat(s, "-focused"), ue), D(l, "".concat(s, "-disabled"), S), D(l, "".concat(s, "-readonly"), p), D(l, "".concat(s, "-not-a-number"), i.isNaN()), D(l, "".concat(s, "-out-of-range"), !i.isInvalidate() && !Q(i)), l)),
    style: k,
    onFocus: function() {
      ie(!0);
    },
    onBlur: We,
    onKeyDown: He,
    onKeyUp: qe,
    onCompositionStart: Me,
    onCompositionEnd: ke,
    onBeforeInput: Pe
  }, Ee && /* @__PURE__ */ u.createElement(Ye, {
    prefixCls: s,
    upNode: O,
    downNode: T,
    upDisabled: de,
    downDisabled: me,
    onStep: Ne
  }), /* @__PURE__ */ u.createElement("div", {
    className: "".concat(te, "-wrap")
  }, /* @__PURE__ */ u.createElement("input", Re({
    autoComplete: "off",
    role: "spinbutton",
    "aria-valuemin": C,
    "aria-valuemax": R,
    "aria-valuenow": i.isInvalidate() ? null : i.toString(),
    step: d
  }, we, {
    ref: Ve(U, v),
    className: te,
    value: E,
    onChange: Oe,
    disabled: S,
    readOnly: p
  }))));
}), ra = /* @__PURE__ */ u.forwardRef(function(a, v) {
  var l = a.disabled, h = a.style, s = a.prefixCls, M = a.value, k = a.prefix, C = a.suffix, R = a.addonBefore, B = a.addonAfter, d = a.classes, g = a.className, o = a.classNames, S = Se(a, ea), p = u.useRef(null), O = function(P) {
    p.current && Qe(p.current, P);
  };
  return /* @__PURE__ */ u.createElement(Je, {
    inputElement: /* @__PURE__ */ u.createElement(aa, Re({
      prefixCls: s,
      disabled: l,
      classNames: o,
      ref: Ve(p, v)
    }, S)),
    className: g,
    triggerFocus: O,
    prefixCls: s,
    value: M,
    disabled: l,
    style: h,
    prefix: k,
    suffix: C,
    addonAfter: B,
    addonBefore: R,
    classes: d,
    classNames: o,
    components: {
      affixWrapper: "div",
      groupWrapper: "div",
      wrapper: "div",
      groupAddon: "div"
    }
  });
});
ra.displayName = "InputNumber";
export {
  ra as default
};
