import { useCallback } from "react";
import { hasPromptPlaceholders } from "./agentPromptPlaceholders";
const DEFAULT_INPUT_PLACEHOLDER = "Type your message... (Enter to send, Shift+Enter or Ctrl+Enter for new line)";
export function useAgentPromptComposerHandlers(input) {
    const { activePlaceholderInput, commands, currentHistoryIndex, documentListRef, enableDocumentUpload, hideBottomControls, isExecuting, prompt, promptHistory, shouldSubmitFilledPlaceholderInputRef, textareaRef, setActivePlaceholderInput, setAllPlaceholdersFilled, setCurrentHistoryIndex, setInputPlaceholder, setPrompt, } = input;
    const focusPromptAtEnd = useCallback(() => {
        if (!textareaRef.current)
            return;
        try {
            textareaRef.current.focus();
            const value = textareaRef.current.value || "";
            textareaRef.current.selectionStart = value.length;
            textareaRef.current.selectionEnd = value.length;
        }
        catch { }
    }, [textareaRef]);
    const setPromptSelectionToEnd = useCallback((text) => {
        if (!textareaRef.current)
            return;
        setTimeout(() => {
            if (textareaRef.current) {
                textareaRef.current.selectionStart = text.length;
                textareaRef.current.selectionEnd = text.length;
            }
        }, 0);
    }, [textareaRef]);
    const handlePaste = useCallback((event) => {
        if (!documentListRef.current)
            return;
        if (!enableDocumentUpload)
            return;
        const items = event.clipboardData?.items;
        if (!items)
            return;
        const files = [];
        for (let i = 0; i < items.length; i++) {
            const item = items[i];
            if (!item)
                continue;
            if (item.type.indexOf("image") !== -1 || item.kind === "file") {
                const file = item.getAsFile();
                if (file) {
                    if (file.name === "image.png" &&
                        item.type.indexOf("image") !== -1) {
                        const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
                        const extension = item.type.split("/")[1] || "png";
                        files.push(new File([file], `pasted-image-${timestamp}.${extension}`, {
                            type: file.type,
                        }));
                    }
                    else {
                        files.push(file);
                    }
                }
            }
        }
        if (files.length > 0) {
            documentListRef.current.uploadFiles(files);
        }
    }, [documentListRef, enableDocumentUpload]);
    const handleKeyPress = useCallback((event) => {
        if (event.key === "Enter" &&
            !event.ctrlKey &&
            !event.metaKey &&
            !event.shiftKey &&
            !event.altKey) {
            event.preventDefault();
            void commands.submitPrompt({
                text: prompt,
                source: "composer",
            });
        }
        if (event.key === "ArrowUp") {
            const canNavigateHistory = prompt.trim().length === 0 || currentHistoryIndex !== -1;
            if (canNavigateHistory) {
                event.preventDefault();
                if (promptHistory.length > 0) {
                    const newIndex = currentHistoryIndex < promptHistory.length - 1
                        ? currentHistoryIndex + 1
                        : currentHistoryIndex;
                    setCurrentHistoryIndex(newIndex);
                    const historicalPrompt = promptHistory[newIndex];
                    if (textareaRef.current && historicalPrompt) {
                        setPrompt(historicalPrompt);
                        setPromptSelectionToEnd(historicalPrompt);
                    }
                }
            }
        }
        if (event.key === "ArrowDown" && currentHistoryIndex >= 0) {
            event.preventDefault();
            const newIndex = currentHistoryIndex - 1;
            setCurrentHistoryIndex(newIndex);
            const historicalPrompt = newIndex >= 0 ? promptHistory[newIndex] || "" : "";
            setPrompt(historicalPrompt);
            setPromptSelectionToEnd(historicalPrompt);
        }
    }, [
        commands,
        currentHistoryIndex,
        prompt,
        promptHistory,
        setCurrentHistoryIndex,
        setPrompt,
        setPromptSelectionToEnd,
        textareaRef,
    ]);
    const handlePromptInputChange = useCallback((value) => {
        setPrompt(value);
        if (!hasPromptPlaceholders(value)) {
            setAllPlaceholdersFilled(false);
        }
        if (currentHistoryIndex !== -1) {
            setCurrentHistoryIndex(-1);
        }
    }, [
        currentHistoryIndex,
        setAllPlaceholdersFilled,
        setCurrentHistoryIndex,
        setPrompt,
    ]);
    const handleActivePlaceholderComplete = useCallback((filledText) => {
        const shouldSubmitFilledText = shouldSubmitFilledPlaceholderInputRef.current ||
            activePlaceholderInput?.behavior !== "compose" ||
            hideBottomControls;
        shouldSubmitFilledPlaceholderInputRef.current = false;
        setActivePlaceholderInput(null);
        setAllPlaceholdersFilled(false);
        if (!shouldSubmitFilledText) {
            setPrompt(filledText);
            setInputPlaceholder("Review and edit, then press Enter to send");
            focusPromptAtEnd();
            return;
        }
        if (isExecuting) {
            try {
                void commands.stopRun();
            }
            catch { }
        }
        void commands.submitPrompt({
            text: filledText,
            source: "composer",
        });
    }, [
        activePlaceholderInput?.behavior,
        commands,
        focusPromptAtEnd,
        hideBottomControls,
        isExecuting,
        setActivePlaceholderInput,
        setAllPlaceholdersFilled,
        setInputPlaceholder,
        setPrompt,
        shouldSubmitFilledPlaceholderInputRef,
    ]);
    const handleActivePlaceholderCancel = useCallback(() => {
        shouldSubmitFilledPlaceholderInputRef.current = false;
        setActivePlaceholderInput(null);
        setAllPlaceholdersFilled(false);
    }, [
        setActivePlaceholderInput,
        setAllPlaceholdersFilled,
        shouldSubmitFilledPlaceholderInputRef,
    ]);
    const handlePromptPlaceholderComplete = useCallback((filledText) => {
        shouldSubmitFilledPlaceholderInputRef.current = false;
        setAllPlaceholdersFilled(false);
        if (filledText.trim()) {
            void commands.submitPrompt({
                text: filledText,
                source: "composer",
            });
        }
        else {
            setPrompt("");
        }
    }, [
        commands,
        setAllPlaceholdersFilled,
        setPrompt,
        shouldSubmitFilledPlaceholderInputRef,
    ]);
    const handlePromptPlaceholderCancel = useCallback(() => {
        shouldSubmitFilledPlaceholderInputRef.current = false;
        setPrompt("");
        setAllPlaceholdersFilled(false);
        setInputPlaceholder(DEFAULT_INPUT_PLACEHOLDER);
    }, [
        setAllPlaceholdersFilled,
        setInputPlaceholder,
        setPrompt,
        shouldSubmitFilledPlaceholderInputRef,
    ]);
    return {
        focusPromptAtEnd,
        handlePaste,
        handleKeyPress,
        handlePromptInputChange,
        handleActivePlaceholderComplete,
        handleActivePlaceholderCancel,
        handlePromptPlaceholderComplete,
        handlePromptPlaceholderCancel,
    };
}
//# sourceMappingURL=useAgentPromptComposerHandlers.js.map