import { useState, useCallback } from 'react';
export function useFileMention() {
    const [showFileMention, setShowFileMention] = useState(false);
    const [mentionQuery, setMentionQuery] = useState('');
    const [mentionSelectedIndex, setMentionSelectedIndex] = useState(0);
    const [currentFileToSelect, setCurrentFileToSelect] = useState();
    const handleFileSelect = useCallback((filePath, textareaRef, setMessage) => {
        const textarea = textareaRef.current;
        if (!textarea)
            return;
        const value = textarea.value;
        const cursorPos = textarea.selectionStart;
        const textBeforeCursor = value.slice(0, cursorPos);
        const match = textBeforeCursor.match(/@(\S*)$/);
        if (!match)
            return;
        const atPos = cursorPos - match[0].length;
        const newValue = `${value.slice(0, atPos)}@${filePath} ${value.slice(cursorPos)}`;
        setMessage(newValue);
        setShowFileMention(false);
        setTimeout(() => {
            const newCursorPos = atPos + filePath.length + 2;
            textarea.setSelectionRange(newCursorPos, newCursorPos);
            textarea.focus();
        }, 0);
    }, []);
    const handleEnterSelect = useCallback((file) => {
        setCurrentFileToSelect(file);
    }, []);
    const checkForMention = useCallback((value, cursorPos) => {
        const textBeforeCursor = value.slice(0, cursorPos);
        const match = textBeforeCursor.match(/@(\S*)$/);
        if (match) {
            setShowFileMention(true);
            setMentionQuery(match[1]);
            setMentionSelectedIndex(0);
        }
        else {
            setShowFileMention(false);
        }
    }, []);
    return {
        showFileMention,
        mentionQuery,
        mentionSelectedIndex,
        currentFileToSelect,
        setShowFileMention,
        setMentionQuery,
        setMentionSelectedIndex,
        setCurrentFileToSelect,
        handleFileSelect,
        handleEnterSelect,
        checkForMention,
    };
}
//# sourceMappingURL=useFileMention.js.map