"use client";
import { useCallback, useEffect, useRef, useState, } from "react";
/**
 * Headless behavior hook for a self-resizing, Enter-to-submit textarea.
 *
 * Encapsulates the common behavior shared across all Stigmer composer
 * surfaces (session launcher, follow-up input, and future variants):
 *
 * - Auto-resizing textarea up to a configurable max height
 * - Enter to submit, Shift+Enter for newline
 * - Disabled state management
 * - Auto-focus when transitioning from disabled to enabled
 *
 * Platform builders who want custom composer UI use this hook directly.
 * The {@link SessionComposer} styled component uses it internally.
 *
 * @example
 * ```tsx
 * function CustomInput({ onSend }: { onSend: (msg: string) => void }) {
 *   const composer = useComposer({ onSubmit: onSend });
 *
 *   return (
 *     <div>
 *       <textarea {...composer.textareaProps} rows={2} />
 *       <button onClick={composer.submit} disabled={!composer.canSubmit}>
 *         Send
 *       </button>
 *     </div>
 *   );
 * }
 * ```
 */
export function useComposer({ onSubmit, disabled = false, maxHeight = 240, autoFocusOnEnable = true, }) {
    const textareaRef = useRef(null);
    const [message, setMessage] = useState("");
    const prevDisabledRef = useRef(disabled);
    const canSubmit = message.trim().length > 0 && !disabled;
    useEffect(() => {
        if (autoFocusOnEnable && prevDisabledRef.current && !disabled) {
            textareaRef.current?.focus();
        }
        prevDisabledRef.current = disabled;
    }, [disabled, autoFocusOnEnable]);
    const resize = useCallback(() => {
        const el = textareaRef.current;
        if (!el)
            return;
        el.style.height = "auto";
        el.style.height = `${Math.min(el.scrollHeight, maxHeight)}px`;
    }, [maxHeight]);
    const resetHeight = useCallback(() => {
        const el = textareaRef.current;
        if (el)
            el.style.height = "auto";
    }, []);
    const clear = useCallback(() => {
        setMessage("");
        resetHeight();
    }, [resetHeight]);
    const submit = useCallback(() => {
        const trimmed = message.trim();
        if (!trimmed || disabled)
            return;
        onSubmit(trimmed);
        clear();
    }, [message, disabled, onSubmit, clear]);
    const handleKeyDown = useCallback((e) => {
        if (e.key === "Enter" && !e.shiftKey) {
            e.preventDefault();
            submit();
        }
    }, [submit]);
    const handleChange = useCallback((e) => {
        setMessage(e.target.value);
        resize();
    }, [resize]);
    const textareaProps = {
        ref: textareaRef,
        value: message,
        onChange: handleChange,
        onKeyDown: handleKeyDown,
        disabled,
    };
    return {
        message,
        setMessage,
        textareaRef,
        textareaProps,
        canSubmit,
        submit,
        clear,
    };
}
//# sourceMappingURL=useComposer.js.map