import { createLogger } from "../../../utils/logger/index.js";
import { init_logger } from "../../../utils/logger.js";
import { BraveProvider } from "./providers/brave.js";
import { TavilyProvider } from "./providers/tavily.js";
import { BingApiProvider } from "./providers/bing-api.js";
import { BingHtmlProvider } from "./providers/bing-html.js";
import { DuckDuckGoHtmlProvider } from "./providers/duckduckgo-html.js";
import { SearXNGProvider } from "./providers/searxng.js";
//#region src/agent/tools/search/registry.ts
init_logger();
const log = createLogger("web-search");
var SearchProviderRegistry = class {
	providers;
	fallbackProvider;
	constructor(config) {
		this.providers = this.buildProviders(config);
		this.fallbackProvider = config.region === "cn" ? new BingHtmlProvider() : new DuckDuckGoHtmlProvider();
		log.debug({
			region: config.region,
			configuredCount: this.providers.filter((p) => p.isAvailable()).length
		}, "Web search registry ready");
	}
	buildProviders(config) {
		const list = [];
		for (const p of config.providers) {
			if (p.disabled) continue;
			switch (p.type) {
				case "brave":
					list.push(new BraveProvider(p.apiKey ?? ""));
					break;
				case "tavily":
					list.push(new TavilyProvider(p.apiKey ?? ""));
					break;
				case "bing":
					list.push(new BingApiProvider(p.apiKey ?? ""));
					break;
				case "searxng":
					list.push(new SearXNGProvider(p.url ?? ""));
					break;
				default: break;
			}
		}
		return list;
	}
	hasConfiguredApiProvider() {
		return this.providers.some((p) => p.isAvailable());
	}
	async search(query, count, signal) {
		for (const provider of this.providers) {
			if (!provider.isAvailable()) continue;
			try {
				return {
					results: await provider.search(query, count, signal),
					provider: provider.name
				};
			} catch (err) {
				log.debug({
					provider: provider.name,
					err
				}, "Search provider failed, trying next");
			}
		}
		try {
			return {
				results: await this.fallbackProvider.search(query, count, signal),
				provider: this.fallbackProvider.name
			};
		} catch (err) {
			log.warn({ err }, "HTML fallback search failed");
			return {
				results: [],
				provider: "none"
			};
		}
	}
};
//#endregion
export { SearchProviderRegistry };

//# sourceMappingURL=registry.js.map