import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useMemo } from 'react';
import Fuse from 'fuse.js';
import { File, FileCode, FileJson, FileText, Image, Braces, FileType, Plus, Pencil, } from 'lucide-react';
function getFileIcon(filePath) {
    const ext = filePath.split('.').pop()?.toLowerCase();
    switch (ext) {
        case 'ts':
        case 'tsx':
        case 'js':
        case 'jsx':
        case 'mjs':
        case 'cjs':
        case 'py':
        case 'rb':
        case 'go':
        case 'rs':
        case 'java':
        case 'c':
        case 'cpp':
        case 'h':
        case 'hpp':
            return FileCode;
        case 'json':
        case 'yaml':
        case 'yml':
        case 'toml':
            return FileJson;
        case 'md':
        case 'txt':
        case 'log':
            return FileText;
        case 'png':
        case 'jpg':
        case 'jpeg':
        case 'gif':
        case 'svg':
        case 'webp':
            return Image;
        case 'css':
        case 'scss':
        case 'sass':
        case 'less':
            return Braces;
        case 'html':
        case 'xml':
            return FileType;
        default:
            return File;
    }
}
function getGitStatusInfo(filePath, changedFilesMap) {
    const status = changedFilesMap.get(filePath);
    if (!status) {
        return null;
    }
    const icons = {
        added: { icon: Plus, label: 'Added', className: 'text-green-500' },
        modified: { icon: Pencil, label: 'Modified', className: 'text-yellow-500' },
        untracked: { icon: Plus, label: 'Untracked', className: 'text-blue-500' },
    };
    return (icons[status] || {
        icon: Pencil,
        label: 'Modified',
        className: 'text-yellow-500',
    });
}
export function FileMentionPopup({ files, changedFiles = [], query, selectedIndex, onSelect, onEnterSelect, onClose, }) {
    const changedFilesMap = useMemo(() => new Map(changedFiles?.map((f) => [f.path, f.status]) || []), [changedFiles]);
    const fuse = useMemo(() => new Fuse(files, {
        threshold: 0.4,
        distance: 100,
        ignoreLocation: true,
        includeMatches: true,
    }), [files]);
    const results = useMemo(() => {
        if (!query) {
            return files.slice(0, 20);
        }
        const searchResults = fuse.search(query).map((r) => r.item);
        searchResults.sort((a, b) => {
            const aChanged = changedFilesMap.has(a);
            const bChanged = changedFilesMap.has(b);
            if (aChanged && !bChanged)
                return -1;
            if (!aChanged && bChanged)
                return 1;
            return 0;
        });
        return searchResults.slice(0, 20);
    }, [fuse, query, files, changedFilesMap]);
    useEffect(() => {
        const element = document.getElementById(`file-item-${selectedIndex}`);
        element?.scrollIntoView({ block: 'nearest' });
    }, [selectedIndex]);
    useEffect(() => {
        onEnterSelect(results[selectedIndex]);
    }, [results, selectedIndex, onEnterSelect]);
    useEffect(() => {
        const handleClickOutside = (e) => {
            const target = e.target;
            if (!target.closest('[data-file-mention-popup]')) {
                onClose();
            }
        };
        document.addEventListener('mousedown', handleClickOutside);
        return () => document.removeEventListener('mousedown', handleClickOutside);
    }, [onClose]);
    if (results.length === 0) {
        return (_jsx("div", { "data-file-mention-popup": true, className: "absolute bottom-full left-0 right-0 mb-2 bg-card border border-border rounded-lg shadow-lg z-50 p-3", children: _jsx("span", { className: "text-muted-foreground text-sm", children: "No files found" }) }));
    }
    return (_jsx("div", { "data-file-mention-popup": true, className: "absolute bottom-full left-0 right-0 mb-2 bg-card border border-border rounded-lg shadow-lg max-h-[300px] overflow-y-auto z-50", children: results.map((filePath, index) => (_jsx("button", { type: "button", id: `file-item-${index}`, onMouseDown: (e) => {
                e.preventDefault();
                onSelect(filePath);
            }, className: `w-full text-left px-3 py-2 hover:bg-accent ${index === selectedIndex ? 'bg-accent' : ''}`, children: _jsxs("div", { className: "flex items-center gap-2 w-full", children: [(() => {
                        const Icon = getFileIcon(filePath);
                        return (_jsx(Icon, { className: "w-4 h-4 flex-shrink-0 text-muted-foreground" }));
                    })(), _jsx("span", { className: "font-mono text-sm flex-1 truncate", children: filePath }), (() => {
                        const status = getGitStatusInfo(filePath, changedFilesMap);
                        return (status && (_jsx(status.icon, { className: `w-3.5 h-3.5 flex-shrink-0 ${status.className}`, title: status.label })));
                    })()] }) }, filePath))) }));
}
//# sourceMappingURL=FileMentionPopup.js.map