/**
 * useSeekoraSearch Hook
 *
 * Hook for performing searches with the Seekora SDK
 */
import { useState, useCallback } from 'react';
import { log } from '@seekora-ai/ui-sdk-core';
export const useSeekoraSearch = ({ client, autoTrack = true, }) => {
    const [results, setResults] = useState(null);
    const [loading, setLoading] = useState(false);
    const [error, setError] = useState(null);
    const [context, setContext] = useState(null);
    const search = useCallback(async (options) => {
        setLoading(true);
        setError(null);
        const query = options.q || '';
        log.verbose('useSeekoraSearch: Starting search', { query, options });
        try {
            const { q, ...restOptions } = options;
            const query = typeof q === 'string' ? q : '';
            const response = await client.search(query, restOptions);
            log.info('useSeekoraSearch: Search completed', {
                query,
                resultsCount: Array.isArray(response?.results) ? response.results.length : 0,
                hasContext: !!response?.context,
            });
            setResults(response);
            // Extract context from response
            if (response.context != null) {
                setContext(response.context);
            }
            return response;
        }
        catch (err) {
            const error = err instanceof Error ? err : new Error(String(err));
            log.error('useSeekoraSearch: Search failed', {
                query,
                error: error.message,
                stack: error.stack,
            });
            setError(error);
            setResults(null);
            setContext(null);
            return null;
        }
        finally {
            setLoading(false);
        }
    }, [client]);
    const clearResults = useCallback(() => {
        setResults(null);
        setError(null);
        setContext(null);
    }, []);
    return {
        search,
        results,
        loading,
        error,
        context,
        clearResults,
    };
};
