"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePromptInput = usePromptInput;
const react_1 = require("react");
/**
 * Custom hook for managing PromptInput state and behavior
 *
 * @param props - Hook props
 * @returns Hook return value with state and handlers
 */
function usePromptInput(props) {
    const { onSend, onCancel, initialValue = '', maxLength, disabled = false, status = 'ready', } = props;
    const [value, setValue] = (0, react_1.useState)(initialValue);
    const [attachments, setAttachments] = (0, react_1.useState)([]);
    const isSubmitted = status === 'submitted';
    const trimmedValue = value.trim();
    const canSubmit = !disabled && !isSubmitted && trimmedValue.length > 0;
    // Map ChatStatus to submit button state
    // ChatStatus.ready → submitButtonState.enabled
    // ChatStatus.error → submitButtonState.enabled
    // ChatStatus.streaming → submitButtonState.cancelable
    // ChatStatus.streaming_loading → submitButtonState.cancelable (same as streaming)
    // ChatStatus.submitted → submitButtonState.loading
    let submitButtonState = 'disabled';
    // disabled by props or empty value and status is ready
    if (disabled) {
        submitButtonState = 'disabled';
    }
    else if (!trimmedValue && (status === 'ready' || status === 'error')) {
        submitButtonState = 'disabled';
    }
    else {
        switch (status) {
            case 'ready':
            case 'error':
                submitButtonState = 'enabled';
                break;
            case 'streaming':
            case 'streaming_loading':
                submitButtonState = onCancel ? 'cancelable' : 'enabled';
                break;
            case 'submitted':
                submitButtonState = 'loading';
                break;
            default:
                submitButtonState = 'enabled';
        }
    }
    // Input is disabled when loading
    const isInputDisabled = submitButtonState === 'loading';
    const handleChange = (0, react_1.useCallback)((newValue) => {
        if (maxLength && newValue.length > maxLength) {
            return;
        }
        setValue(newValue);
    }, [maxLength]);
    const handleSubmit = (0, react_1.useCallback)(async () => {
        if (submitButtonState === 'cancelable' && onCancel) {
            await onCancel();
            return;
        }
        if (!canSubmit) {
            return;
        }
        const submitData = Object.assign({ content: trimmedValue }, (attachments.length > 0 && { attachments }));
        onSend(submitData);
        setValue('');
        setAttachments([]);
    }, [submitButtonState, canSubmit, trimmedValue, attachments, onSend, onCancel]);
    const handleKeyDown = (0, react_1.useCallback)((event) => {
        const isEnter = event.code === 'Enter' || event.code === 'NumpadEnter';
        // Insert new line on Ctrl/Cmd + Enter
        if (isEnter && (event.ctrlKey || event.metaKey)) {
            setValue((prev) => prev + '\n');
            event.preventDefault();
            return;
        }
        // Prevent submission if can't submit
        if (isEnter && !event.shiftKey && !canSubmit) {
            event.preventDefault();
            return;
        }
        // Submit on Enter (without Shift)
        if (isEnter && !event.shiftKey && canSubmit) {
            event.preventDefault();
            handleSubmit();
        }
    }, [canSubmit, handleSubmit]);
    return {
        value,
        setValue,
        canSubmit,
        submitButtonState,
        isInputDisabled,
        handleChange,
        handleKeyDown,
        handleSubmit,
        attachments,
        setAttachments,
    };
}
//# sourceMappingURL=usePromptInput.js.map
