import React from 'react';
export const useChatInputState = ({ onSendMessage, isAttachmentCallbackPending, isProviderComingSoon, activeSlashCommand, setActiveSlashCommand, createUploads, clearAttachments, restoreAttachments, resolveSlashCommand, pendingAttachments }) => {
    const [inputValue, setInputValue] = React.useState('');
    const [inputHistory, setInputHistory] = React.useState([]);
    const [isSendingMessage, setIsSendingMessage] = React.useState(false);
    const pendingStateRef = React.useRef({
        inputValue: '',
        slashCommand: null,
        attachments: []
    });
    const isMountedRef = React.useRef(true);
    React.useEffect(() => {
        return () => {
            isMountedRef.current = false;
        };
    }, []);
    const handleSubmit = React.useCallback(async (event) => {
        event.preventDefault();
        if (isAttachmentCallbackPending) {
            return;
        }
        if (isProviderComingSoon) {
            return;
        }
        const rawInput = inputValue;
        const trimmed = rawInput.trim();
        const slashCommandSelection = activeSlashCommand;
        const hasSlashCommand = Boolean(slashCommandSelection);
        if (!hasSlashCommand && trimmed.length === 0) {
            return;
        }
        const attachmentsSnapshot = pendingAttachments.map((attachment) => ({ ...attachment }));
        pendingStateRef.current = {
            inputValue: rawInput,
            slashCommand: slashCommandSelection,
            attachments: attachmentsSnapshot
        };
        try {
            setIsSendingMessage(true);
            setInputValue('');
            setActiveSlashCommand(null);
            clearAttachments();
            const attachments = await createUploads(attachmentsSnapshot);
            const { commandText, payload } = await resolveSlashCommand(slashCommandSelection);
            const messageSections = [];
            if (commandText) {
                messageSections.push(commandText);
            }
            if (trimmed.length > 0) {
                messageSections.push(trimmed);
            }
            const content = messageSections.join('\n\n').trim();
            if (content.length === 0) {
                return;
            }
            await onSendMessage({
                content,
                attachments,
                slashCommand: payload
            });
            const historyEntry = rawInput.length > 0 ? rawInput : content;
            if (historyEntry.trim().length > 0) {
                setInputHistory((prev) => [...prev, historyEntry]);
            }
            pendingStateRef.current.attachments = [];
        }
        catch (error) {
            if (!isMountedRef.current) {
                return;
            }
            setInputValue(pendingStateRef.current.inputValue);
            setActiveSlashCommand(pendingStateRef.current.slashCommand);
            restoreAttachments(pendingStateRef.current.attachments);
            throw error;
        }
        finally {
            if (isMountedRef.current) {
                setIsSendingMessage(false);
            }
        }
    }, [
        activeSlashCommand,
        clearAttachments,
        createUploads,
        inputValue,
        isAttachmentCallbackPending,
        isProviderComingSoon,
        onSendMessage,
        pendingAttachments,
        resolveSlashCommand,
        restoreAttachments,
        setActiveSlashCommand
    ]);
    const resetStateAfterNewSession = React.useCallback(() => {
        setInputValue('');
        setInputHistory([]);
        setActiveSlashCommand(null);
        pendingStateRef.current = {
            inputValue: '',
            slashCommand: null,
            attachments: []
        };
    }, [setActiveSlashCommand]);
    return {
        inputValue,
        setInputValue,
        inputHistory,
        setInputHistory,
        isSendingMessage,
        setIsSendingMessage,
        handleSubmit,
        resetStateAfterNewSession
    };
};
