import { jsx, jsxs } from 'react/jsx-runtime';
import { forwardRef, useId, useState } from 'react';
import { c as cn } from './utils-DdHUxIdC.js';
import { Field, FieldLabel, FieldContent, FieldError, FieldDescription } from './primitives/Field.js';
import { Textarea as Textarea$1 } from './primitives/Textarea.js';

const SIZE_ROWS = {
    small: 1,
    medium: 3,
    large: 4,
};
const SIZE_MIN_HEIGHT = {
    small: "min-h-8",
    medium: "min-h-20",
    large: "min-h-28",
};

function getTrimmedValue(value, disableTrimOnBlur) {
    if (disableTrimOnBlur || typeof value !== "string")
        return value;
    return value.trim();
}

const Textarea = forwardRef(({ size = "medium", resize = "vertical", label = "", error = "", helpText = "", disabled = false, required = false, maxLength, unlimitedChars = false, disableTrimOnBlur = false, prefix, suffix, nakedTextarea = false, labelProps, className = "", rows: rowsProp, onBlur, onChange, value: controlledValue, ...otherProps }, ref) => {
    const generatedId = useId();
    const id = otherProps.id ?? generatedId;
    const errorId = `error_${id}`;
    const helpTextId = `helpText_${id}`;
    // Controlled / uncontrolled value tracking
    const [valueInternal, setValueInternal] = useState(controlledValue ?? "");
    const isControlled = controlledValue !== undefined;
    const value = isControlled ? controlledValue : valueInternal;
    const valueLength = value?.toString().length || 0;
    const isMaxLengthPresent = !!maxLength || maxLength === 0;
    const isCharacterLimitVisible = isMaxLengthPresent && valueLength >= maxLength * 0.85;
    const maxLengthError = unlimitedChars && valueLength > maxLength;
    const rows = rowsProp ?? SIZE_ROWS[size];
    // --- Handlers ---
    const handleChange = (e) => {
        if (!isControlled) {
            setValueInternal(e.target.value);
        }
        onChange?.(e);
    };
    const handleBlur = (e) => {
        const trimmedValue = getTrimmedValue(value ?? "", disableTrimOnBlur);
        if (trimmedValue !== value) {
            e.target.value = trimmedValue.toString();
            handleChange(e);
        }
        onBlur?.(e);
    };
    // --- aria-describedby ---
    const ariaDescribedBy = [error ? errorId : null, helpText ? helpTextId : null]
        .filter(Boolean)
        .join(" ") || undefined;
    // --- Textarea element ---
    const hasField = !!(label || error || helpText);
    const textareaElement = (jsx(Textarea$1, { ref: ref, id: id, rows: rows, disabled: disabled, required: required, "aria-invalid": !!error || undefined, "aria-describedby": ariaDescribedBy, className: cn(
        // Override shadcn's field-sizing-content and min-h-16 so rows works
        "!field-sizing-normal", SIZE_MIN_HEIGHT[size], resize === "none" && "resize-none", resize === "vertical" && "resize-y", nakedTextarea &&
            "border-transparent shadow-none px-0 py-0 focus-visible:border-transparent focus-visible:ring-0", !!error && "border-destructive ring-destructive/20 ring-3"), value: value, onBlur: handleBlur, onChange: handleChange, ...(isMaxLengthPresent && !unlimitedChars ? { maxLength } : {}), ...otherProps }));
    const hasPrefixSuffix = !!(prefix || suffix);
    const textareaWithAffixes = hasPrefixSuffix ? (jsxs("div", { className: cn("flex items-start gap-2 rounded-lg border border-input px-2.5 py-2 focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50", !!error && "border-destructive ring-destructive/20 ring-3", nakedTextarea && "border-transparent shadow-none"), children: [prefix && (jsx("div", { className: "shrink-0 pt-0.5 text-muted-foreground", children: prefix })), jsx(Textarea$1, { ref: ref, id: id, rows: rows, disabled: disabled, required: required, "aria-invalid": !!error || undefined, "aria-describedby": ariaDescribedBy, className: cn("!field-sizing-normal border-0 p-0 shadow-none focus-visible:border-0 focus-visible:ring-0", SIZE_MIN_HEIGHT[size], resize === "none" && "resize-none", resize === "vertical" && "resize-y"), value: value, onBlur: handleBlur, onChange: handleChange, ...(isMaxLengthPresent && !unlimitedChars ? { maxLength } : {}), ...otherProps }), suffix && (jsx("div", { className: "shrink-0 pt-0.5 text-muted-foreground", children: suffix }))] })) : (textareaElement);
    if (!hasField) {
        return jsx("div", { className: cn(className), children: textareaWithAffixes });
    }
    return (jsxs(Field, { "data-disabled": disabled || undefined, "data-invalid": !!error || undefined, className: className, children: [(label || isCharacterLimitVisible) && (jsxs("div", { className: "flex items-center justify-between", children: [label && (jsxs(FieldLabel, { htmlFor: id, ...labelProps, children: [label, required && (jsx("span", { "aria-hidden": "true", className: "text-destructive", children: "*" }))] })), isCharacterLimitVisible && (jsxs("span", { className: cn("text-xs tabular-nums", maxLengthError ? "text-destructive" : "text-muted-foreground"), children: [valueLength, "/", maxLength] }))] })), jsxs(FieldContent, { children: [textareaWithAffixes, !!error && jsx(FieldError, { id: errorId, children: error }), helpText && (jsx(FieldDescription, { id: helpTextId, children: helpText }))] })] }));
});
Textarea.displayName = "Textarea";

export { Textarea as T };
//# sourceMappingURL=Textarea-C0z50h0N.js.map
