import { l as normalizeOptionalString } from "./string-coerce-CIXf7egm.js";
import { A as isGoogleGemini3ThinkingLevelModel, k as isGoogleGemini3ProModel } from "./provider-stream-shared-Df6R56gC.js";
import "./string-coerce-runtime-C8jKEm3h.js";
import { n as normalizeGoogleModelId, t as normalizeAntigravityModelId } from "./model-id-CAmKILzd.js";
import "./thinking-api-YCEumkJ-.js";
//#region extensions/google/src/google-api-base-url.ts
const DEFAULT_GOOGLE_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
function trimTrailingSlashes(value) {
	return value.replace(/\/+$/, "");
}
function isCanonicalGoogleApiOriginShorthand(value) {
	return /^https:\/\/generativelanguage\.googleapis\.com\/?$/i.test(value);
}
function isGoogleGenerativeAiUrl(url) {
	return url.protocol === "https:" && url.hostname.toLowerCase() === "generativelanguage.googleapis.com";
}
/** Exact official AI Studio request root eligible for native provider behavior. */
function isOfficialGoogleAiStudioBaseUrl(baseUrl) {
	const raw = normalizeOptionalString(baseUrl) ?? "https://generativelanguage.googleapis.com/v1beta";
	try {
		const href = trimTrailingSlashes(new URL(raw).href);
		return href === "https://generativelanguage.googleapis.com" || href === "https://generativelanguage.googleapis.com/v1beta";
	} catch {
		return false;
	}
}
function stripUrlUserInfo(url) {
	url.username = "";
	url.password = "";
}
const GOOGLE_VERTEX_HOST = "aiplatform.googleapis.com";
const GOOGLE_VERTEX_REGION_HOST_SUFFIX = "-aiplatform.googleapis.com";
const GOOGLE_VERTEX_MULTI_REGION_HOSTS = /* @__PURE__ */ new Set(["aiplatform.eu.rep.googleapis.com", "aiplatform.us.rep.googleapis.com"]);
function isGoogleVertexHostname(hostname) {
	const normalized = hostname.toLowerCase();
	return normalized === GOOGLE_VERTEX_HOST || normalized.endsWith(GOOGLE_VERTEX_REGION_HOST_SUFFIX) || GOOGLE_VERTEX_MULTI_REGION_HOSTS.has(normalized);
}
function isGoogleVertexBaseUrl(baseUrl) {
	const raw = normalizeOptionalString(baseUrl);
	if (!raw) return false;
	try {
		return isGoogleVertexHostname(new URL(raw).hostname);
	} catch {
		return false;
	}
}
function normalizeGoogleApiBaseUrl(baseUrl) {
	const raw = trimTrailingSlashes(normalizeOptionalString(baseUrl) || "https://generativelanguage.googleapis.com/v1beta");
	try {
		const url = new URL(raw);
		url.hash = "";
		url.search = "";
		stripUrlUserInfo(url);
		if (isGoogleGenerativeAiUrl(url)) url.pathname = trimTrailingSlashes(url.pathname || "") || "/v1beta";
		return trimTrailingSlashes(url.toString());
	} catch {
		if (isCanonicalGoogleApiOriginShorthand(raw)) return DEFAULT_GOOGLE_API_BASE_URL;
		return raw;
	}
}
function isGoogleGenerativeAiApi(api) {
	return api === "google-generative-ai";
}
function normalizeGoogleGenerativeAiBaseUrl(baseUrl) {
	const raw = normalizeOptionalString(baseUrl);
	if (!raw) return;
	const normalized = normalizeGoogleApiBaseUrl(raw);
	try {
		const url = new URL(normalized);
		stripUrlUserInfo(url);
		if (isGoogleGenerativeAiUrl(url)) {
			url.pathname = trimTrailingSlashes(url.pathname || "").replace(/\/openai$/i, "") || "/v1beta";
			return trimTrailingSlashes(url.toString());
		}
	} catch {}
	return normalized;
}
//#endregion
//#region extensions/google/provider-policy.ts
const GOOGLE_MODEL_ID_PROVIDERS = /* @__PURE__ */ new Set([
	"google",
	"google-gemini-cli",
	"google-vertex"
]);
function resolveGoogleGenerativeAiTransport(params) {
	const api = params.api ?? (params.provider === "google-vertex" && isGoogleVertexBaseUrl(params.baseUrl) ? "google-vertex" : void 0) ?? (params.provider === "google" && params.baseUrl ? "google-generative-ai" : params.api);
	return {
		api,
		baseUrl: isGoogleGenerativeAiApi(api) ? normalizeGoogleGenerativeAiBaseUrl(params.baseUrl) : params.baseUrl
	};
}
function resolveGoogleGenerativeAiApiOrigin(baseUrl) {
	return (normalizeGoogleGenerativeAiBaseUrl(baseUrl) ?? normalizeGoogleApiBaseUrl(baseUrl)).replace(/\/v1beta$/i, "");
}
function shouldNormalizeGoogleGenerativeAiProviderConfig(providerKey, provider) {
	if (providerKey === "google-vertex" && isGoogleVertexBaseUrl(provider.baseUrl)) return false;
	if (isGoogleGenerativeAiApi(provider.api)) return true;
	if (provider.models?.some((model) => isGoogleGenerativeAiApi(model?.api)) ?? false) return true;
	if (providerKey !== "google" && providerKey !== "google-vertex") return false;
	return !(normalizeOptionalString(provider.api) !== void 0);
}
function shouldNormalizeGoogleProviderConfig(providerKey, provider) {
	return providerKey === "google-antigravity" || shouldNormalizeGoogleGenerativeAiProviderConfig(providerKey, provider);
}
function normalizeProviderModels(provider, normalizeId) {
	const models = provider.models;
	if (!Array.isArray(models) || models.length === 0) return provider;
	let mutated = false;
	const nextModels = models.map((model) => {
		const nextId = normalizeId(model.id);
		if (nextId === model.id) return model;
		mutated = true;
		return Object.assign({}, model, { id: nextId });
	});
	return mutated ? {
		...provider,
		models: nextModels
	} : provider;
}
function normalizeGoogleProviderConfig(providerKey, provider) {
	let nextProvider = provider;
	if (GOOGLE_MODEL_ID_PROVIDERS.has(providerKey)) {
		const modelNormalized = normalizeProviderModels(nextProvider, normalizeGoogleModelId);
		if (shouldNormalizeGoogleGenerativeAiProviderConfig(providerKey, modelNormalized)) {
			const normalizedBaseUrl = normalizeGoogleGenerativeAiBaseUrl(modelNormalized.baseUrl);
			nextProvider = normalizedBaseUrl !== modelNormalized.baseUrl ? {
				...modelNormalized,
				baseUrl: normalizedBaseUrl ?? modelNormalized.baseUrl
			} : modelNormalized;
		} else nextProvider = modelNormalized;
	}
	if (providerKey === "google-antigravity") nextProvider = normalizeProviderModels(nextProvider, normalizeAntigravityModelId);
	return nextProvider;
}
function resolveGoogleThinkingProfile({ modelId, reasoning }) {
	const normalizedModelId = normalizeGoogleModelId(modelId);
	const isGemini3ThinkingModel = isGoogleGemini3ThinkingLevelModel(normalizedModelId);
	if (reasoning === false && !isGemini3ThinkingModel) return;
	return {
		levels: isGoogleGemini3ProModel(normalizedModelId) ? [
			{ id: "off" },
			{ id: "low" },
			{ id: "adaptive" },
			{ id: "high" }
		] : [
			{ id: "off" },
			{ id: "minimal" },
			{ id: "low" },
			{ id: "medium" },
			{ id: "adaptive" },
			{ id: "high" }
		],
		...isGemini3ThinkingModel ? { preserveWhenCatalogReasoningFalse: true } : {}
	};
}
//#endregion
export { shouldNormalizeGoogleGenerativeAiProviderConfig as a, isGoogleGenerativeAiApi as c, isOfficialGoogleAiStudioBaseUrl as d, normalizeGoogleApiBaseUrl as f, resolveGoogleThinkingProfile as i, isGoogleVertexBaseUrl as l, resolveGoogleGenerativeAiApiOrigin as n, shouldNormalizeGoogleProviderConfig as o, normalizeGoogleGenerativeAiBaseUrl as p, resolveGoogleGenerativeAiTransport as r, DEFAULT_GOOGLE_API_BASE_URL as s, normalizeGoogleProviderConfig as t, isGoogleVertexHostname as u };
