/**
 * Web Search Extension v4
 *
 * Native Anthropic hooks stay eager. Heavy tool registration is deferred in
 * interactive mode so startup is not blocked on the full search tool stack.
 */
import { importExtensionModule } from "@singularity-forge/coding-agent";
import { registerSearchProviderCommand } from "./command-search-provider.js";
import { registerNativeSearchHooks } from "./native-search.js";

let toolsPromise = null;
let resetSearchLoopGuardStateRef = null;
let resetGoogleSearchCacheRef = null;
async function registerSearchTools(pi) {
	if (!toolsPromise) {
		toolsPromise = (async () => {
			const [
				{ registerSearchTool, resetSearchLoopGuardState },
				{ registerFetchPageTool },
				{ registerLLMContextTool },
				{ registerGoogleSearchTool, resetGoogleSearchCache },
			] = await Promise.all([
				importExtensionModule(import.meta.url, "./tool-search.js"),
				importExtensionModule(import.meta.url, "./tool-fetch-page.js"),
				importExtensionModule(import.meta.url, "./tool-llm-context.js"),
				importExtensionModule(import.meta.url, "./tool-google-search.js"),
			]);
			resetSearchLoopGuardStateRef = resetSearchLoopGuardState;
			resetGoogleSearchCacheRef = resetGoogleSearchCache;
			registerSearchTool(pi);
			registerFetchPageTool(pi);
			registerLLMContextTool(pi);
			registerGoogleSearchTool(pi);
		})().catch((error) => {
			toolsPromise = null;
			throw error;
		});
	}
	return toolsPromise;
}
export default function (pi) {
	registerSearchProviderCommand(pi);
	registerNativeSearchHooks(pi);
	pi.on("session_start", async (_event, ctx) => {
		const resetLoopGuardState = () => {
			resetSearchLoopGuardStateRef?.();
		};
		if (ctx.hasUI) {
			resetLoopGuardState();
			resetGoogleSearchCacheRef?.();
			void registerSearchTools(pi)
				.then(() => {
					resetLoopGuardState();
					resetGoogleSearchCacheRef?.();
				})
				.catch((error) => {
					ctx.ui.notify(
						`search-the-web failed to load: ${error instanceof Error ? error.message : String(error)}`,
						"warning",
					);
				});
			return;
		}
		await registerSearchTools(pi);
		resetLoopGuardState();
		resetGoogleSearchCacheRef?.();
	});
}
