"use client";
import { jsx as N } from "react/jsx-runtime";
import { i as S } from "./input-ckl5misj0jd9cu5r.js";
import * as Y from "react";
import { useRef as v, useCallback as k, useLayoutEffect as F, useEffect as A } from "react";
import { F as B, n as L } from "./field-bmglk138wa8bf3ei.js";
import { c as M } from "./vendor-styling-ofvnbcuht98q88oo.js";
import { aY as O } from "./vendor-base-ui-elpbzr8xeffrz4h2.js";
const W = typeof window < "u" ? F : A;
function f(n) {
  const t = Number.parseFloat(n);
  return Number.isNaN(t) ? 0 : t;
}
function P({
  enabled: n,
  minRows: t,
  maxRows: R
}) {
  const d = v(null), z = v(n);
  z.current = n;
  const m = v(t);
  m.current = t;
  const b = v(R);
  b.current = R;
  const a = k(() => {
    const e = d.current;
    if (!z.current || !e || typeof window > "u")
      return;
    const r = window.getComputedStyle(e), s = f(r.borderTopWidth) + f(r.borderBottomWidth), l = f(r.paddingTop) + f(r.paddingBottom), p = r.boxSizing === "border-box";
    e.style.height = "auto";
    let c = p ? e.scrollHeight + s : e.scrollHeight - l;
    const w = m.current, o = b.current;
    if (w > 0 || o && o > 0) {
      const C = f(r.fontSize), u = r.lineHeight, g = u === "normal" || u === "" ? C * 1.2 : u.endsWith("px") ? f(u) : f(u) * C, x = p ? l + s : 0, h = g * w + x;
      if (c = Math.max(c, h), o && o > 0) {
        const y = g * o + x;
        c > y ? (c = y, e.style.overflowY = "auto") : e.style.overflowY = "hidden";
      } else
        e.style.overflowY = "hidden";
    } else
      e.style.overflowY = "hidden";
    e.style.height = `${c}px`;
  }, []);
  return W(() => {
    n && a();
  }), W(() => {
    if (!n) return;
    const e = d.current;
    if (!e) return;
    let r = e.clientWidth;
    const s = typeof ResizeObserver < "u" ? new ResizeObserver(() => {
      e.clientWidth !== r && (r = e.clientWidth, a());
    }) : null;
    return s?.observe(e), () => {
      s?.disconnect(), e.style.height = "", e.style.overflowY = "";
    };
  }, [n, a]), { textareaRef: d, resize: a };
}
const I = Y.forwardRef(
  (n, t) => {
    const {
      className: R,
      onValueChange: d,
      size: z = "base",
      variant: m,
      onChange: b,
      label: a,
      labelTooltip: e,
      description: r,
      error: s,
      autoResize: l = !1,
      minRows: p = 1,
      maxRows: c,
      rows: w,
      ...o
    } = n;
    process.env.NODE_ENV !== "production" && m === "error" && console.warn(
      '[Kumo InputArea]: variant="error" is deprecated. Error styling is now automatically applied when the `error` prop is truthy. Simply remove the variant prop and pass an error message instead.'
    );
    const C = m ?? (s ? "error" : "default"), { required: u } = o, { textareaRef: g, resize: x } = P({
      enabled: l,
      minRows: p,
      maxRows: c
    }), h = v(void 0), y = k(
      (i) => {
        g.current = i, typeof t == "function" ? i ? h.current = t(i) : h.current ? (h.current(), h.current = void 0) : t(null) : t && (t.current = i);
      },
      [t, g]
    ), E = o.value !== void 0, H = k(
      (i) => {
        b?.(i), d?.(i.target.value), E || x();
      },
      [b, d, x, E]
    ), T = M(
      S({ size: z, variant: C, focusIndicator: !0 }),
      "h-auto py-2",
      // Input variant always comes with size, but it does not apply for textarea
      l && "w-full field-sizing-content resize-none scroll-pb-2 [scrollbar-color:var(--color-kumo-line)_transparent] [scrollbar-width:thin] [&::-webkit-scrollbar]:w-2 [&::-webkit-scrollbar]:bg-transparent [&::-webkit-scrollbar-track]:my-2 [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:bg-kumo-line [&::-webkit-scrollbar-corner]:bg-transparent",
      R
    );
    return a || s || r ? /* @__PURE__ */ N(
      B,
      {
        label: a,
        required: u,
        labelTooltip: e,
        description: r,
        error: L(s),
        children: /* @__PURE__ */ N(
          O,
          {
            render: (i) => /* @__PURE__ */ N(
              "textarea",
              {
                ...i,
                ref: y,
                className: T,
                onChange: H,
                rows: l ? p : w,
                ...o
              }
            )
          }
        )
      }
    ) : /* @__PURE__ */ N(
      "textarea",
      {
        ref: y,
        className: T,
        onChange: H,
        rows: l ? p : w,
        ...o
      }
    );
  }
);
I.displayName = "InputArea";
const $ = I;
export {
  I,
  $ as T
};
//# sourceMappingURL=input-area-sg518xu4pyg4m0km.js.map
