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