import de from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import j from "../../@babel/runtime/helpers/esm/extends.mjs";
import T from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import ve from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import b from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import ge from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import I from "../../classnames/index.mjs";
import he from "../../rc-util/es/hooks/useMergedState.mjs";
import Ce from "../../rc-util/es/omit.mjs";
import l, { forwardRef as xe, useState as Se, useRef as ye, useImperativeHandle as Ee, useEffect as we } from "react";
import pe from "./BaseInput.mjs";
import Fe from "./hooks/useCount.mjs";
import { triggerFocus as _e, resolveOnChange as V } from "./utils/commonUtils.mjs";
var Ne = ["autoComplete", "onChange", "onFocus", "onBlur", "onPressEnter", "onKeyDown", "prefixCls", "disabled", "htmlSize", "className", "maxLength", "suffix", "showCount", "count", "type", "classes", "classNames", "styles", "onCompositionStart", "onCompositionEnd"], Te = /* @__PURE__ */ xe(function(t, k) {
  var H = t.autoComplete, B = t.onChange, v = t.onFocus, g = t.onBlur, P = t.onPressEnter, h = t.onKeyDown, D = t.prefixCls, f = D === void 0 ? "rc-input" : D, d = t.disabled, W = t.htmlSize, $ = t.className, q = t.maxLength, C = t.suffix, G = t.showCount, J = t.count, K = t.type, Q = K === void 0 ? "text" : K, U = t.classes, s = t.classNames, i = t.styles, x = t.onCompositionStart, S = t.onCompositionEnd, X = ge(t, Ne), Y = Se(!1), L = b(Y, 2), Z = L[0], y = L[1], E = l.useRef(!1), o = ye(null), w = function(e) {
    o.current && _e(o.current, e);
  }, ee = he(t.defaultValue, {
    value: t.value
  }), z = b(ee, 2), p = z[0], M = z[1], F = p == null ? "" : String(p), te = l.useState(null), O = b(te, 2), _ = O[0], ne = O[1], a = Fe(J, G), m = a.max || q, N = a.strategy(F), oe = !!m && N > m;
  Ee(k, function() {
    return {
      focus: w,
      blur: function() {
        var e;
        (e = o.current) === null || e === void 0 || e.blur();
      },
      setSelectionRange: function(e, r, u) {
        var c;
        (c = o.current) === null || c === void 0 || c.setSelectionRange(e, r, u);
      },
      select: function() {
        var e;
        (e = o.current) === null || e === void 0 || e.select();
      },
      input: o.current
    };
  }), we(function() {
    y(function(n) {
      return n && d ? !1 : n;
    });
  }, [d]);
  var A = function(e, r) {
    var u = r;
    if (!E.current && a.exceedFormatter && a.max && a.strategy(r) > a.max && (u = a.exceedFormatter(r, {
      max: a.max
    }), r !== u)) {
      var c, R;
      ne([((c = o.current) === null || c === void 0 ? void 0 : c.selectionStart) || 0, ((R = o.current) === null || R === void 0 ? void 0 : R.selectionEnd) || 0]);
    }
    M(u), o.current && V(o.current, e, B, u);
  };
  l.useEffect(function() {
    if (_) {
      var n;
      (n = o.current) === null || n === void 0 || n.setSelectionRange.apply(n, ve(_));
    }
  }, [_]);
  var ae = function(e) {
    A(e, e.target.value);
  }, re = function(e) {
    E.current = !1, A(e, e.currentTarget.value), S == null || S(e);
  }, ue = function(e) {
    P && e.key === "Enter" && P(e), h == null || h(e);
  }, le = function(e) {
    y(!0), v == null || v(e);
  }, se = function(e) {
    y(!1), g == null || g(e);
  }, ie = function(e) {
    M(""), w(), o.current && V(o.current, e, B);
  }, ce = oe && "".concat(f, "-out-of-range"), fe = function() {
    var e = Ce(t, [
      "prefixCls",
      "onPressEnter",
      "addonBefore",
      "addonAfter",
      "prefix",
      "suffix",
      "allowClear",
      // Input elements must be either controlled or uncontrolled,
      // specify either the value prop, or the defaultValue prop, but not both.
      "defaultValue",
      "showCount",
      "count",
      "classes",
      "htmlSize",
      "styles",
      "classNames"
    ]);
    return /* @__PURE__ */ l.createElement("input", j({
      autoComplete: H
    }, e, {
      onChange: ae,
      onFocus: le,
      onBlur: se,
      onKeyDown: ue,
      className: I(f, T({}, "".concat(f, "-disabled"), d), s == null ? void 0 : s.input),
      style: i == null ? void 0 : i.input,
      ref: o,
      size: W,
      type: Q,
      onCompositionStart: function(u) {
        E.current = !0, x == null || x(u);
      },
      onCompositionEnd: re
    }));
  }, me = function() {
    var e = Number(m) > 0;
    if (C || a.show) {
      var r = a.showFormatter ? a.showFormatter({
        value: F,
        count: N,
        maxLength: m
      }) : "".concat(N).concat(e ? " / ".concat(m) : "");
      return /* @__PURE__ */ l.createElement(l.Fragment, null, a.show && /* @__PURE__ */ l.createElement("span", {
        className: I("".concat(f, "-show-count-suffix"), T({}, "".concat(f, "-show-count-has-suffix"), !!C), s == null ? void 0 : s.count),
        style: de({}, i == null ? void 0 : i.count)
      }, r), C);
    }
    return null;
  };
  return /* @__PURE__ */ l.createElement(pe, j({}, X, {
    prefixCls: f,
    className: I($, ce),
    inputElement: fe(),
    handleReset: ie,
    value: F,
    focused: Z,
    triggerFocus: w,
    suffix: me(),
    disabled: d,
    classes: U,
    classNames: s,
    styles: i
  }));
});
export {
  Te as default
};
