'use client';
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAutosizeTextArea = void 0;
exports.AutosizeTextarea = AutosizeTextarea;
const jsx_runtime_1 = require("react/jsx-runtime");
const React = __importStar(require("react"));
const utils_1 = require("../../../lib/utils");
const react_1 = require("react");
const useAutosizeTextArea = ({ textAreaRef, triggerAutoSize, maxHeight = Number.MAX_SAFE_INTEGER, minHeight = 0 }) => {
    const [init, setInit] = React.useState(true);
    React.useEffect(() => {
        // We need to reset the height momentarily to get the correct scrollHeight for the textarea
        const textAreaElement = textAreaRef.current;
        if (textAreaElement) {
            if (init) {
                textAreaElement.style.minHeight = `${minHeight}px`;
                if (maxHeight > minHeight) {
                    textAreaElement.style.maxHeight = `${maxHeight}px`;
                }
                setInit(false);
            }
            textAreaElement.style.height = `${minHeight}px`;
            const scrollHeight = textAreaElement.scrollHeight;
            // We then set the height directly, outside of the render loop
            // Trying to set this with state or a ref will segment an incorrect value.
            if (scrollHeight > maxHeight) {
                textAreaElement.style.height = `${maxHeight}px`;
                textAreaElement.style.overflowY = 'auto';
            }
            else {
                textAreaElement.style.height = `${scrollHeight}px`;
                textAreaElement.style.overflowY = 'hidden';
            }
        }
    }, [textAreaRef.current, triggerAutoSize]);
};
exports.useAutosizeTextArea = useAutosizeTextArea;
function AutosizeTextarea({ maxHeight = Number.MAX_SAFE_INTEGER, minHeight = 36, className, onChange, value, ref, ...props }) {
    const textAreaRef = React.useRef(null);
    const [triggerAutoSize, setTriggerAutoSize] = React.useState('');
    (0, exports.useAutosizeTextArea)({
        textAreaRef,
        triggerAutoSize: triggerAutoSize,
        maxHeight,
        minHeight
    });
    (0, react_1.useImperativeHandle)(ref, () => textAreaRef.current);
    React.useEffect(() => {
        setTriggerAutoSize(value);
    }, [props?.defaultValue, value]);
    return ((0, jsx_runtime_1.jsx)("textarea", { ...props, value: value, ref: textAreaRef, className: (0, utils_1.cn)('border-input-border ring-offset-background placeholder:text-muted-foreground focus-visible:ring-ring flex h-9 w-full overflow-y-hidden rounded-md border bg-transparent px-3 py-2 text-sm focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-hidden', 'read-only:bg-muted read-only:cursor-default read-only:caret-transparent read-only:opacity-50 read-only:select-text read-only:focus:ring-0 read-only:focus:ring-offset-0 read-only:focus:outline-hidden', 'disabled:cursor-not-allowed disabled:opacity-50', className), onChange: (e) => {
            setTriggerAutoSize(e.target.value);
            onChange?.(e);
        } }));
}
