'use client';
import { useState, useEffect, useCallback, useMemo } from 'react';
export function useSearch({ requests, folders, docGroups }) {
    const [isOpen, setIsOpen] = useState(false);
    // Build search items from collection
    const searchItems = useMemo(()=>{
        const items = [];
        // Add endpoints from requests
        requests.forEach((request)=>{
            items.push({
                id: request.id,
                title: request.name,
                description: request.description || undefined,
                type: 'endpoint',
                method: request.method,
                path: request.endpoint,
                href: `#api/${request.id}`
            });
        });
        // Add endpoints from folders recursively
        const addFolderItems = (folder, parentPath = '')=>{
            folder.requests?.forEach((request)=>{
                items.push({
                    id: request.id,
                    title: request.name,
                    description: request.description || undefined,
                    type: 'endpoint',
                    method: request.method,
                    path: request.endpoint,
                    href: `#api/${request.id}`
                });
            });
            folder.folders?.forEach((subFolder)=>{
                addFolderItems(subFolder, `${parentPath}/${folder.name}`);
            });
        };
        folders.forEach((folder)=>addFolderItems(folder));
        // Add doc pages recursively (including nested children)
        const addDocPages = (pages, groupPath)=>{
            pages.forEach((page)=>{
                items.push({
                    id: `doc-${page.slug}`,
                    title: page.title,
                    description: page.description,
                    type: 'doc',
                    path: groupPath,
                    href: `#guides/page/${page.slug}`
                });
                // Recursively add children
                if (page.children && page.children.length > 0) {
                    addDocPages(page.children, `${groupPath} / ${page.title}`);
                }
            });
        };
        // Add doc groups recursively
        const addDocGroup = (group, parentPath = '')=>{
            const groupPath = parentPath ? `${parentPath} / ${group.title}` : group.title;
            addDocPages(group.pages, groupPath);
        };
        docGroups?.forEach((group)=>{
            addDocGroup(group);
        });
        return items;
    }, [
        requests,
        folders,
        docGroups
    ]);
    // Global keyboard shortcut (Cmd+K or Ctrl+K)
    useEffect(()=>{
        const handleKeyDown = (e)=>{
            if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
                e.preventDefault();
                setIsOpen(true);
            }
        };
        document.addEventListener('keydown', handleKeyDown);
        return ()=>document.removeEventListener('keydown', handleKeyDown);
    }, []);
    const openSearch = useCallback(()=>setIsOpen(true), []);
    const closeSearch = useCallback(()=>setIsOpen(false), []);
    return {
        isOpen,
        setIsOpen,
        openSearch,
        closeSearch,
        searchItems
    };
}
