import { createBraveSearchProvider } from './providers/brave.js';
import { createDuckDuckGoProvider } from './providers/duckduckgo.js';
import { createExaSearchProvider } from './providers/exa.js';
import { createFirecrawlSearchProvider } from './providers/firecrawl.js';
import { createPerplexitySearchProvider } from './providers/perplexity.js';
import { createSearxngSearchProvider } from './providers/searxng.js';
import { createTavilySearchProvider } from './providers/tavily.js';
export class WebSearchProviderRegistry {
    providers = new Map();
    constructor(context) {
        this.register(createDuckDuckGoProvider(), { replace: true });
        this.register(createSearxngSearchProvider(context), { replace: true });
        this.register(createBraveSearchProvider(context), { replace: true });
        this.register(createExaSearchProvider(context), { replace: true });
        this.register(createFirecrawlSearchProvider(context), { replace: true });
        this.register(createTavilySearchProvider(context), { replace: true });
        this.register(createPerplexitySearchProvider(context), { replace: true });
    }
    register(provider, options = {}) {
        const id = provider.id.trim();
        if (!id)
            throw new Error('Web search provider id is required');
        if (this.providers.has(id) && !options.replace) {
            throw new Error(`Web search provider already registered: ${id}`);
        }
        const registered = { ...provider, id };
        this.providers.set(id, registered);
        return () => {
            if (this.providers.get(id) === registered)
                this.unregister(id);
        };
    }
    unregister(id) {
        return this.providers.delete(id);
    }
    get(id) {
        return this.providers.get(id) ?? null;
    }
    list() {
        return [...this.providers.values()]
            .map((provider) => provider.descriptor?.() ?? {
            id: provider.id,
            label: provider.label,
            capabilities: [...provider.capabilities],
            requiresAuth: false,
            configured: true,
        })
            .sort((a, b) => a.label.localeCompare(b.label) || a.id.localeCompare(b.id));
    }
    find(providerId) {
        if (providerId)
            return this.get(providerId);
        return [...this.providers.values()][0] ?? null;
    }
}
