'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var utils = require('./utils-CTr7wn5d.js');
var primitives_Field = require('./primitives/Field.js');
var primitives_Textarea = require('./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 = React.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 = React.useId();
    const id = otherProps.id ?? generatedId;
    const errorId = `error_${id}`;
    const helpTextId = `helpText_${id}`;
    // Controlled / uncontrolled value tracking
    const [valueInternal, setValueInternal] = React.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 = (jsxRuntime.jsx(primitives_Textarea.Textarea, { ref: ref, id: id, rows: rows, disabled: disabled, required: required, "aria-invalid": !!error || undefined, "aria-describedby": ariaDescribedBy, className: utils.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 ? (jsxRuntime.jsxs("div", { className: utils.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 && (jsxRuntime.jsx("div", { className: "shrink-0 pt-0.5 text-muted-foreground", children: prefix })), jsxRuntime.jsx(primitives_Textarea.Textarea, { ref: ref, id: id, rows: rows, disabled: disabled, required: required, "aria-invalid": !!error || undefined, "aria-describedby": ariaDescribedBy, className: utils.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 && (jsxRuntime.jsx("div", { className: "shrink-0 pt-0.5 text-muted-foreground", children: suffix }))] })) : (textareaElement);
    if (!hasField) {
        return jsxRuntime.jsx("div", { className: utils.cn(className), children: textareaWithAffixes });
    }
    return (jsxRuntime.jsxs(primitives_Field.Field, { "data-disabled": disabled || undefined, "data-invalid": !!error || undefined, className: className, children: [(label || isCharacterLimitVisible) && (jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [label && (jsxRuntime.jsxs(primitives_Field.FieldLabel, { htmlFor: id, ...labelProps, children: [label, required && (jsxRuntime.jsx("span", { "aria-hidden": "true", className: "text-destructive", children: "*" }))] })), isCharacterLimitVisible && (jsxRuntime.jsxs("span", { className: utils.cn("text-xs tabular-nums", maxLengthError ? "text-destructive" : "text-muted-foreground"), children: [valueLength, "/", maxLength] }))] })), jsxRuntime.jsxs(primitives_Field.FieldContent, { children: [textareaWithAffixes, !!error && jsxRuntime.jsx(primitives_Field.FieldError, { id: errorId, children: error }), helpText && (jsxRuntime.jsx(primitives_Field.FieldDescription, { id: helpTextId, children: helpText }))] })] }));
});
Textarea.displayName = "Textarea";

exports.Textarea = Textarea;
//# sourceMappingURL=Textarea-BfdlAJ59.js.map
