'use client';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { useState, useEffect, useCallback, useRef } from 'react';
import { useRouter } from 'next/navigation';
import { FolderKanban, FileText, Search, BookOpen } from 'lucide-react';
import { Dialog, DialogContent } from '../../ui/dialog.js';
import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem, CommandSeparator, } from '../../ui/command.js';
import { globalSearch } from '../../../app/actions/global-search.js';
export function GlobalSearchDialog({ className }) {
    const [open, setOpen] = useState(false);
    const [query, setQuery] = useState('');
    const [results, setResults] = useState([]);
    const [loading, setLoading] = useState(false);
    const router = useRouter();
    const debounceRef = useRef(undefined);
    // Register Cmd+K / Ctrl+K keyboard shortcut
    useEffect(() => {
        const handleKeyDown = (e) => {
            if (e.key === 'k' && (e.metaKey || e.ctrlKey)) {
                e.preventDefault();
                setOpen((prev) => !prev);
            }
        };
        window.addEventListener('keydown', handleKeyDown);
        return () => window.removeEventListener('keydown', handleKeyDown);
    }, []);
    // Debounced search
    const performSearch = useCallback(async (searchQuery) => {
        if (!searchQuery.trim()) {
            setResults([]);
            return;
        }
        setLoading(true);
        try {
            const { results: searchResults } = await globalSearch(searchQuery);
            setResults(searchResults ?? []);
        }
        finally {
            setLoading(false);
        }
    }, []);
    const handleQueryChange = useCallback((e) => {
        const value = e.target.value;
        setQuery(value);
        if (debounceRef.current)
            clearTimeout(debounceRef.current);
        debounceRef.current = setTimeout(() => performSearch(value), 200);
    }, [performSearch]);
    const handleSelect = useCallback((result) => {
        setOpen(false);
        setQuery('');
        setResults([]);
        router.push(result.url);
    }, [router]);
    const handleOpenChange = useCallback((newOpen) => {
        setOpen(newOpen);
        if (!newOpen) {
            setQuery('');
            setResults([]);
        }
    }, []);
    const projectResults = results.filter((r) => r.type === 'project');
    const workItemResults = results.filter((r) => r.type === 'workItem');
    const pageResults = results.filter((r) => r.type === 'page');
    return (_jsx(Dialog, { open: open, onOpenChange: handleOpenChange, children: _jsx(DialogContent, { "data-testid": "global-search-dialog", className: "max-w-lg gap-0 overflow-hidden p-0", children: _jsxs(Command, { className: className, children: [_jsx(CommandInput, { placeholder: "Search projects, work items, and pages...", value: query, onChange: handleQueryChange, "data-testid": "global-search-input" }), _jsxs(CommandList, { children: [query.trim() && !loading && results.length === 0 ? (_jsx(CommandEmpty, { children: "No results found." })) : null, loading ? (_jsx("div", { className: "text-muted-foreground py-6 text-center text-xs", children: "Searching..." })) : null, projectResults.length > 0 ? (_jsxs(CommandGroup, { children: [_jsx("div", { className: "text-muted-foreground px-2 py-1.5 text-[10px] font-medium", children: "Projects" }), projectResults.map((result) => (_jsxs(CommandItem, { onClick: () => handleSelect(result), "data-testid": `search-result-${result.id}`, children: [_jsx(FolderKanban, { className: "mr-2 h-3.5 w-3.5 shrink-0 opacity-50" }), _jsx("span", { className: "flex-1 truncate text-xs", children: result.title }), result.subtitle ? (_jsx("span", { className: "text-muted-foreground ml-2 shrink-0 font-mono text-[10px]", children: result.subtitle })) : null] }, result.id)))] })) : null, projectResults.length > 0 && workItemResults.length > 0 ? _jsx(CommandSeparator, {}) : null, workItemResults.length > 0 ? (_jsxs(CommandGroup, { children: [_jsx("div", { className: "text-muted-foreground px-2 py-1.5 text-[10px] font-medium", children: "Work Items" }), workItemResults.map((result) => (_jsxs(CommandItem, { onClick: () => handleSelect(result), "data-testid": `search-result-${result.id}`, children: [_jsx(FileText, { className: "mr-2 h-3.5 w-3.5 shrink-0 opacity-50" }), _jsx("span", { className: "flex-1 truncate text-xs", children: result.title }), result.subtitle ? (_jsx("span", { className: "text-muted-foreground ml-2 shrink-0 font-mono text-[10px]", children: result.subtitle })) : null] }, result.id)))] })) : null, pageResults.length > 0 ? (_jsxs(_Fragment, { children: [projectResults.length > 0 || workItemResults.length > 0 ? (_jsx(CommandSeparator, {})) : null, _jsxs(CommandGroup, { children: [_jsx("div", { className: "text-muted-foreground px-2 py-1.5 text-[10px] font-medium", children: "Pages" }), pageResults.map((result) => (_jsxs(CommandItem, { onClick: () => handleSelect(result), "data-testid": `search-result-${result.id}`, children: [_jsx(BookOpen, { className: "mr-2 h-3.5 w-3.5 shrink-0 opacity-50" }), _jsx("span", { className: "flex-1 truncate text-xs", children: result.title }), result.subtitle ? (_jsx("span", { className: "text-muted-foreground ml-2 shrink-0 font-mono text-[10px]", children: result.subtitle })) : null] }, result.id)))] })] })) : null, !query.trim() && !loading ? (_jsxs("div", { className: "text-muted-foreground py-6 text-center text-xs", children: [_jsx(Search, { className: "mx-auto mb-2 h-5 w-5 opacity-20" }), _jsx("p", { children: "Type to search projects, work items, and pages" }), _jsxs("p", { className: "mt-1 text-[10px]", children: ["Press", ' ', _jsx("kbd", { className: "bg-muted rounded px-1 py-0.5 font-mono text-[10px]", children: "\u2318K" }), ' ', "to open anytime"] })] })) : null] })] }) }) }));
}
