import z from "./index-extends.js";
import M from "./index-objectWithoutPropertiesLoose.js";
import * as a from "react";
import e from "./index-_virtual16.js";
import { jsxs as B, jsx as O } from "react/jsx-runtime";
import L from "./index-useForkRef.js";
import T from "./index-ownerWindow2.js";
import C from "./index-useEnhancedEffect2.js";
import V from "./index-debounce3.js";
const D = ["onChange", "maxRows", "minRows", "style", "value"];
function g(r) {
  return parseInt(r, 10) || 0;
}
const I = {
  shadow: {
    // Visibility needed to hide the extra text area on iPads
    visibility: "hidden",
    // Remove from the content flow
    position: "absolute",
    // Ignore the scrollbar width
    overflow: "hidden",
    height: 0,
    top: 0,
    left: 0,
    // Create a new layer, increase the isolation of the computed values
    transform: "translateZ(0)"
  }
};
function P(r) {
  return r == null || Object.keys(r).length === 0 || r.outerHeightStyle === 0 && !r.overflowing;
}
const j = /* @__PURE__ */ a.forwardRef(function(c, E) {
  const {
    onChange: v,
    maxRows: p,
    minRows: u = 1,
    style: x,
    value: b
  } = c, N = M(c, D), {
    current: A
  } = a.useRef(b != null), d = a.useRef(null), F = L(E, d), R = a.useRef(null), H = a.useRef(null), y = a.useCallback(() => {
    const t = d.current, n = T(t).getComputedStyle(t);
    if (n.width === "0px")
      return {
        outerHeightStyle: 0,
        overflowing: !1
      };
    const o = H.current;
    o.style.width = n.width, o.value = t.value || c.placeholder || "x", o.value.slice(-1) === `
` && (o.value += " ");
    const h = n.boxSizing, m = g(n.paddingBottom) + g(n.paddingTop), l = g(n.borderBottomWidth) + g(n.borderTopWidth), S = o.scrollHeight;
    o.value = "x";
    const w = o.scrollHeight;
    let s = S;
    u && (s = Math.max(Number(u) * w, s)), p && (s = Math.min(Number(p) * w, s)), s = Math.max(s, w);
    const _ = s + (h === "border-box" ? m + l : 0), k = Math.abs(s - S) <= 1;
    return {
      outerHeightStyle: _,
      overflowing: k
    };
  }, [p, u, c.placeholder]), f = a.useCallback(() => {
    const t = y();
    if (P(t))
      return;
    const i = t.outerHeightStyle, n = d.current;
    R.current !== i && (R.current = i, n.style.height = `${i}px`), n.style.overflow = t.overflowing ? "hidden" : "";
  }, [y]);
  C(() => {
    const t = () => {
      f();
    };
    let i;
    const n = () => {
      cancelAnimationFrame(i), i = requestAnimationFrame(() => {
        t();
      });
    }, o = V(t), h = d.current, m = T(h);
    m.addEventListener("resize", o);
    let l;
    return typeof ResizeObserver < "u" && (l = new ResizeObserver(process.env.NODE_ENV === "test" ? n : t), l.observe(h)), () => {
      o.clear(), cancelAnimationFrame(i), m.removeEventListener("resize", o), l && l.disconnect();
    };
  }, [y, f]), C(() => {
    f();
  });
  const W = (t) => {
    A || f(), v && v(t);
  };
  return /* @__PURE__ */ B(a.Fragment, {
    children: [/* @__PURE__ */ O("textarea", z({
      value: b,
      onChange: W,
      ref: F,
      rows: u,
      style: x
    }, N)), /* @__PURE__ */ O("textarea", {
      "aria-hidden": !0,
      className: c.className,
      readOnly: !0,
      ref: H,
      tabIndex: -1,
      style: z({}, I.shadow, x, {
        paddingTop: 0,
        paddingBottom: 0
      })
    })]
  });
});
process.env.NODE_ENV !== "production" && (j.propTypes = {
  // ┌────────────────────────────── Warning ──────────────────────────────┐
  // │ These PropTypes are generated from the TypeScript type definitions. │
  // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
  // └─────────────────────────────────────────────────────────────────────┘
  /**
   * @ignore
   */
  className: e.string,
  /**
   * Maximum number of rows to display.
   */
  maxRows: e.oneOfType([e.number, e.string]),
  /**
   * Minimum number of rows to display.
   * @default 1
   */
  minRows: e.oneOfType([e.number, e.string]),
  /**
   * @ignore
   */
  onChange: e.func,
  /**
   * @ignore
   */
  placeholder: e.string,
  /**
   * @ignore
   */
  style: e.object,
  /**
   * @ignore
   */
  value: e.oneOfType([e.arrayOf(e.string), e.number, e.string])
});
export {
  j as default
};
