import { jsxs as F, jsx as u } from "react/jsx-runtime";
import { useFocusRing as A } from "@react-aria/focus";
import { useEffect as N } from "react";
import B from "react-textarea-autosize";
import { useMemoizedId as H } from "../../hooks/useMemoizedId.es.js";
import { FOCUS_STYLE as J } from "../../utilities/focusStyle.es.js";
import { InputStyles as i, GetStatusIcon as Q, InputActions as W, InputExtraActions as X } from "../../utilities/input.es.js";
import { merge as b } from "../../utilities/merge.es.js";
import { useForwardedRef as Z } from "../../utilities/useForwardedRef.es.js";
import { validationClassMap as K, Validation as n } from "../../utilities/validation.es.js";
const ee = ({
  autocomplete: R,
  autosize: f = !1,
  clearable: l = !1,
  decorator: m,
  defaultValue: S,
  disabled: d = !1,
  extraActions: c = void 0,
  focusOnMount: v = !1,
  hugWidth: y,
  id: $,
  minRows: p,
  maxRows: j,
  placeholder: k,
  readOnly: w,
  resizable: E = !1,
  required: I,
  selectable: O = !1,
  status: s = n.Default,
  value: o,
  onBlur: T,
  onChange: h,
  onEnterPressed: C,
  onKeyDown: g,
  onFocus: z,
  "data-test-id": a = "fondue-textarea",
  ...V
}) => {
  const P = f ? B : "textarea", r = Z(null), x = d || w, { isFocusVisible: G, focusProps: L } = A({
    isTextInput: !0,
    within: !0
  }), D = () => {
    if (r.current) {
      const { value: e } = r.current;
      h == null || h(e);
    }
  }, M = (e) => {
    var t;
    C && e.key === "Enter" ? C((t = r.current) == null ? void 0 : t.value) : g == null || g(e);
  }, U = () => {
    r.current && (r.current.value = "", D());
  };
  N(() => {
    if (v) {
      const e = setTimeout(() => {
        var t;
        (t = r.current) == null || t.focus();
      }, 0);
      return () => clearTimeout(e);
    }
  }, [v, r]), N(() => {
    var t;
    const e = o !== ((t = r.current) == null ? void 0 : t.value);
    r.current && o && !e && (r.current.value = o);
  }, [o, r]);
  const Y = { minRows: p, maxRows: j }, _ = () => {
    let e = 1;
    const t = c ? c.length : 0;
    switch (!0) {
      case (t > 0 && l && s !== n.Default):
        return e = t + 4.5, `${e}rem`;
      case (t > 0 && (l || s !== n.Default)):
        return e = t + 3, `${e}rem`;
      case t > 0:
        return e += t * 1.5, `${e}rem`;
      case (l && s !== n.Default):
        return "3.5rem";
      case (s !== n.Default || l):
        return "2.5rem";
      default:
        return e;
    }
  }, q = () => !x && E ? "tw-resize-y" : "tw-resize-none";
  return /* @__PURE__ */ F("div", { className: "tw-relative", children: [
    m ? /* @__PURE__ */ u(
      "div",
      {
        className: b([
          "tw-absolute tw-top-[0.7rem] tw-left-[0.7rem] tw-z-10",
          d ? i.disabled : "",
          w ? i.readOnly : ""
        ]),
        "data-test-id": `${a}-decorator`,
        children: m
      }
    ) : null,
    /* @__PURE__ */ u("span", { ...L, children: /* @__PURE__ */ u(
      P,
      {
        ...f ? Y : { rows: p },
        autoComplete: R ? "on" : "off",
        disabled: d,
        id: H($),
        readOnly: w,
        ref: r,
        required: I,
        defaultValue: S,
        value: o,
        placeholder: k,
        onBlur: T,
        onChange: D,
        onClick: () => {
          var e;
          return (e = r.current) == null ? void 0 : e.focus();
        },
        onFocus: (e) => {
          O && e.target.select(), z && z(e);
        },
        onKeyDown: M,
        "data-test-id": a,
        className: b([
          y ? "" : i.width,
          p ? "" : i.height,
          i.disabled,
          i.readOnly,
          i.element,
          i.base,
          i.focus,
          i.hover,
          G && J,
          s !== n.Default && K[s],
          m ? "tw-pl-[2rem]" : "",
          q()
        ]),
        style: { paddingRight: _() },
        ...V
      }
    ) }),
    /* @__PURE__ */ F(
      "span",
      {
        className: b([
          "tw-absolute tw-top-[0.5rem] tw-right-[0rem] tw-flex tw-items-center tw-justify-between tw-w-auto",
          f ? "tw-pr-2" : "tw-pr-5"
        ]),
        children: [
          /* @__PURE__ */ u(
            W,
            {
              clearable: l,
              disabled: x,
              callbacks: { clearable: U },
              dataTestId: a
            }
          ),
          c ? /* @__PURE__ */ u(X, { actions: c, disabled: x, dataTestId: a }) : null,
          s ? Q(s, a) : null
        ]
      }
    )
  ] });
};
ee.displayName = "FondueTextarea";
export {
  ee as Textarea
};
//# sourceMappingURL=Textarea.es.js.map
