import { a as normalizeLowercaseStringOrEmpty } from "./string-coerce-mnp54Vah.js";
import { r as logVerbose } from "./globals-CogQFSsi.js";
import { n as getActiveRuntimeWebToolsMetadata } from "./runtime-web-tools-state-fE_he60Y.js";
import { o as sortWebFetchProvidersForAutoDetect } from "./web-search-providers.shared-u3XDjavA.js";
import { n as resolveRuntimeWebFetchProviders, t as resolvePluginWebFetchProviders } from "./web-fetch-providers.runtime.js";
import { a as resolveWebProviderDefinition, i as resolveWebProviderConfig, n as providerRequiresCredential, r as readWebProviderEnvValue, t as hasWebProviderEntryCredential } from "./provider-runtime-shared-Da_3Al2P.js";
//#region src/web-fetch/runtime.ts
/** Runtime provider selection and tool construction for the `web_fetch` tool. */
/** Resolves whether web_fetch is enabled for the current config/sandbox. */
function resolveWebFetchEnabled(params) {
	if (typeof params.fetch?.enabled === "boolean") return params.fetch.enabled;
	return true;
}
function resolveFetchConfig(config) {
	return resolveWebProviderConfig(config, "fetch");
}
function hasEntryCredential(provider, config, fetch) {
	return hasWebProviderEntryCredential({
		provider,
		config,
		toolConfig: fetch,
		resolveRawValue: ({ provider: currentProvider, config: currentConfig, toolConfig }) => currentProvider.getConfiguredCredentialValue?.(currentConfig) ?? currentProvider.getCredentialValue(toolConfig),
		resolveFallbackRawValue: ({ provider: currentProvider, config: currentConfig }) => currentProvider.getConfiguredCredentialFallback?.(currentConfig)?.value,
		resolveEnvValue: ({ provider: currentProvider }) => readWebProviderEnvValue(currentProvider.envVars)
	});
}
function hasAutoDetectCredential(provider, config, fetch) {
	return hasEntryCredential({
		...provider,
		requiresCredential: true
	}, config, fetch);
}
/** Reports whether a web_fetch provider has usable credentials. */
function isWebFetchProviderConfigured(params) {
	return hasEntryCredential(params.provider, params.config, resolveFetchConfig(params.config));
}
/** Lists web_fetch providers available to runtime selection. */
function listWebFetchProviders(params) {
	return resolvePluginWebFetchProviders({ config: params?.config });
}
/** Resolves the configured or auto-detected web_fetch provider id. */
function resolveWebFetchProviderId(params) {
	const providers = sortWebFetchProvidersForAutoDetect(params.providers ?? resolvePluginWebFetchProviders({ config: params.config }));
	const raw = params.fetch && "provider" in params.fetch ? normalizeLowercaseStringOrEmpty(params.fetch.provider) : "";
	if (raw) {
		const explicit = providers.find((provider) => provider.id === raw);
		if (explicit) return explicit.id;
	}
	for (const provider of providers) {
		if (!providerRequiresCredential(provider)) {
			if (!hasAutoDetectCredential(provider, params.config, params.fetch)) continue;
			logVerbose(`web_fetch: ${raw ? `invalid configured provider "${raw}", ` : ""}auto-detected keyless provider "${provider.id}"`);
			return provider.id;
		}
		if (!hasEntryCredential(provider, params.config, params.fetch)) continue;
		logVerbose(`web_fetch: ${raw ? `invalid configured provider "${raw}", ` : ""}auto-detected "${provider.id}" from available API keys`);
		return provider.id;
	}
	return "";
}
function resolveConfiguredWebFetchProviderId(params) {
	const raw = params.fetch && "provider" in params.fetch ? normalizeLowercaseStringOrEmpty(params.fetch.provider) : "";
	if (!raw) return;
	return params.providers.find((provider) => provider.id === raw)?.id;
}
/** Resolves the executable web_fetch provider tool definition. */
function resolveWebFetchDefinition(options) {
	const fetch = resolveWebProviderConfig(options?.config, "fetch");
	const runtimeWebFetch = options?.runtimeWebFetch ?? getActiveRuntimeWebToolsMetadata()?.fetch;
	const providers = sortWebFetchProvidersForAutoDetect(options?.sandboxed ? resolvePluginWebFetchProviders({
		config: options?.config,
		sandboxed: true
	}) : options?.preferRuntimeProviders ? resolveRuntimeWebFetchProviders({ config: options?.config }) : resolvePluginWebFetchProviders({ config: options?.config }));
	return resolveWebProviderDefinition({
		config: options?.config,
		toolConfig: fetch,
		runtimeMetadata: runtimeWebFetch,
		sandboxed: options?.sandboxed,
		providerId: options?.providerId ?? resolveConfiguredWebFetchProviderId({
			fetch,
			providers
		}),
		providers,
		resolveEnabled: ({ toolConfig, sandboxed }) => resolveWebFetchEnabled({
			fetch: toolConfig,
			sandboxed
		}),
		resolveAutoProviderId: ({ config, toolConfig, providers: providersLocal }) => resolveWebFetchProviderId({
			config,
			fetch: toolConfig,
			providers: providersLocal
		}),
		createTool: ({ provider, config, toolConfig, runtimeMetadata }) => provider.createTool({
			config,
			fetchConfig: toolConfig,
			runtimeMetadata
		})
	});
}
//#endregion
export { listWebFetchProviders as n, resolveWebFetchDefinition as r, isWebFetchProviderConfigured as t };
