import { c as normalizeOptionalString } from "./string-coerce-mnp54Vah.js";
import { s as coerceSecretRef } from "./types.secrets-NEX6PJ-w.js";
import { g as resolveNonEnvSecretRefApiKeyMarker } from "./model-auth-markers-DS14LrSu.js";
import "./string-coerce-runtime-6HjB7EiL.js";
import "./provider-auth-bASKOTJ4.js";
import { a as resolveCloudflareAiGatewayBaseUrl, i as buildCloudflareAiGatewayModelDefinition } from "./models-Ds0snX1K.js";
//#region extensions/cloudflare-ai-gateway/catalog-provider.ts
/**
* Builds runtime model catalog entries from stored Cloudflare AI Gateway auth
* profiles.
*/
function resolveCloudflareAiGatewayApiKey(cred) {
	if (!cred || cred.type !== "api_key") return;
	const keyRef = coerceSecretRef(cred.keyRef);
	const keyRefId = normalizeOptionalString(keyRef?.id);
	if (keyRef && keyRefId) return keyRef.source === "env" ? keyRefId : resolveNonEnvSecretRefApiKeyMarker(keyRef.source);
	return normalizeOptionalString(cred.key);
}
function resolveCloudflareAiGatewayMetadata(cred) {
	if (!cred || cred.type !== "api_key") return {};
	return {
		accountId: normalizeOptionalString(cred.metadata?.accountId),
		gatewayId: normalizeOptionalString(cred.metadata?.gatewayId)
	};
}
/**
* Returns a provider catalog entry when credentials and Gateway metadata are
* complete enough to construct an Anthropic-compatible base URL.
*/
function buildCloudflareAiGatewayCatalogProvider(params) {
	const apiKey = normalizeOptionalString(params.envApiKey) ?? resolveCloudflareAiGatewayApiKey(params.credential);
	if (!apiKey) return null;
	const { accountId, gatewayId } = resolveCloudflareAiGatewayMetadata(params.credential);
	if (!accountId || !gatewayId) return null;
	const baseUrl = resolveCloudflareAiGatewayBaseUrl({
		accountId,
		gatewayId
	});
	if (!baseUrl) return null;
	return {
		baseUrl,
		api: "anthropic-messages",
		apiKey,
		models: [buildCloudflareAiGatewayModelDefinition()]
	};
}
//#endregion
export { buildCloudflareAiGatewayCatalogProvider as t };
