import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CIXf7egm.js";
import { c as getPluginMetadataSnapshotCache } from "./plugin-cache-DMPn6vOK.js";
import { r as normalizeProviderId } from "./provider-id-DMd-TDFp.js";
import { t as buildModelCatalogMergeKey } from "./model-catalog-refs-BdjEHOKQ.js";
import { n as planManifestModelCatalogSuppressions } from "./manifest-planner-BpBF2xF-.js";
import { c as loadManifestMetadataSnapshot, n as isManifestPluginAvailableForControlPlane } from "./manifest-contract-eligibility-B5yM4KNC.js";
import "./model-catalog-YrXw0PBH.js";
//#region src/plugins/manifest-model-suppression.ts
function listManifestModelCatalogSuppressions(params) {
	const snapshot = params.snapshot;
	return planManifestModelCatalogSuppressions({ registry: {
		diagnostics: snapshot.diagnostics,
		plugins: snapshot.plugins.filter((plugin) => isManifestPluginAvailableForControlPlane({
			snapshot,
			plugin,
			config: params.config
		}))
	} }).suppressions;
}
function buildManifestSuppressionError(params) {
	const ref = `${params.provider}/${params.modelId}`;
	return params.reason ? `Unknown model: ${ref}. ${params.reason}` : `Unknown model: ${ref}.`;
}
function normalizeBaseUrlHost(baseUrl) {
	const trimmed = baseUrl?.trim();
	if (!trimmed) return "";
	try {
		return normalizeSuppressionHost(new URL(trimmed).hostname);
	} catch {
		return "";
	}
}
function normalizeSuppressionHost(host) {
	return normalizeLowercaseStringOrEmpty(host).replace(/\.+$/, "");
}
function resolveConfiguredProviderValue(params) {
	const providers = params.config?.models?.providers;
	if (!providers) return;
	for (const [providerId, entry] of Object.entries(providers)) {
		if (normalizeLowercaseStringOrEmpty(providerId) !== params.provider) continue;
		return {
			api: normalizeLowercaseStringOrEmpty(entry?.api),
			baseUrl: typeof entry?.baseUrl === "string" ? entry.baseUrl : void 0
		};
	}
}
function manifestSuppressionMatchesConditions(params) {
	const when = params.suppression.when;
	if (!when) return true;
	const configuredProvider = resolveConfiguredProviderValue({
		provider: params.provider,
		config: params.config
	});
	if (when.providerConfigApiIn?.length) {
		const allowedApis = new Set(when.providerConfigApiIn.map(normalizeLowercaseStringOrEmpty));
		const effectiveApi = configuredProvider ? normalizeLowercaseStringOrEmpty(configuredProvider.api) : params.provider;
		if (!effectiveApi || !allowedApis.has(effectiveApi)) return false;
	}
	if (when.baseUrlHosts?.length) {
		const baseUrlHost = normalizeBaseUrlHost(params.baseUrl ?? configuredProvider?.baseUrl);
		if (!baseUrlHost && !params.baseUrl && !configuredProvider?.baseUrl) return true;
		if (!baseUrlHost) return false;
		if (!new Set(when.baseUrlHosts.map(normalizeSuppressionHost)).has(baseUrlHost)) return false;
	}
	return true;
}
function buildManifestBuiltInModelSuppressionResolver(params) {
	const snapshot = loadManifestMetadataSnapshot(params);
	const cache = getPluginMetadataSnapshotCache(snapshot).metadata.modelSuppressionResolvers;
	let compiled = cache.get(snapshot);
	if (!compiled) {
		compiled = { byConfig: /* @__PURE__ */ new WeakMap() };
		cache.set(snapshot, compiled);
	}
	const cached = params.config ? compiled.byConfig.get(params.config) : compiled.unconfigured;
	if (cached) return cached;
	const suppressions = listManifestModelCatalogSuppressions({
		snapshot,
		config: params.config
	});
	const resolver = (input) => {
		const provider = normalizeLowercaseStringOrEmpty(input.provider);
		const modelId = normalizeLowercaseStringOrEmpty(input.id);
		if (!provider || !modelId) return;
		const mergeKey = buildModelCatalogMergeKey(provider, modelId);
		const suppression = suppressions.find((entry) => entry.mergeKey === mergeKey && (!input.unconditionalOnly || !entry.when) && manifestSuppressionMatchesConditions({
			suppression: entry,
			provider,
			baseUrl: input.baseUrl,
			config: params.config
		}));
		if (!suppression) return;
		return {
			suppress: true,
			errorMessage: buildManifestSuppressionError({
				provider,
				modelId,
				reason: suppression.reason
			})
		};
	};
	if (params.config) compiled.byConfig.set(params.config, resolver);
	else compiled.unconfigured = resolver;
	return resolver;
}
//#endregion
//#region src/agents/model-suppression.ts
/**
* Built-in model suppression helpers.
* Resolves prepared plugin manifest suppression rules so
* built-in catalog entries can be hidden or blocked consistently.
*/
function resolveBuiltInModelSuppressionFromManifest(params) {
	const provider = normalizeProviderId(params.provider ?? "");
	const modelId = normalizeLowercaseStringOrEmpty(params.id);
	if (!provider || !modelId) return;
	return buildManifestBuiltInModelSuppressionResolver({
		env: process.env,
		...params.config ? { config: params.config } : {},
		...params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}
	})({
		provider,
		id: modelId,
		...params.baseUrl ? { baseUrl: params.baseUrl } : {},
		...params.unconditionalOnly !== void 0 ? { unconditionalOnly: params.unconditionalOnly } : {}
	});
}
/** Return true when plugin manifest metadata suppresses a built-in model entry. */
function shouldSuppressBuiltInModelCore(params) {
	return resolveBuiltInModelSuppressionFromManifest(params)?.suppress ?? false;
}
/**
* Return true only for unconditional manifest suppressions.
* Inline model entries may override conditional suppressions, but not absolute
* provider capability blocks.
*/
function shouldUnconditionallySuppress(params) {
	return resolveBuiltInModelSuppressionFromManifest({
		...params,
		unconditionalOnly: true
	})?.suppress ?? false;
}
/** Resolve the user-facing suppression error message for a built-in model. */
function buildSuppressedBuiltInModelError(params) {
	return resolveBuiltInModelSuppressionFromManifest(params)?.errorMessage;
}
/** Build a reusable suppression predicate for repeated catalog filtering. */
function buildShouldSuppressBuiltInModelCore(params) {
	const resolver = buildManifestBuiltInModelSuppressionResolver({
		env: process.env,
		...params.config ? { config: params.config } : {},
		...params.workspaceDir ? { workspaceDir: params.workspaceDir } : {}
	});
	return (input) => {
		const provider = normalizeProviderId(input.provider ?? "");
		const id = normalizeLowercaseStringOrEmpty(input.id);
		if (!provider || !id) return false;
		return resolver({
			provider,
			id,
			...input.baseUrl ? { baseUrl: input.baseUrl } : {}
		})?.suppress ?? false;
	};
}
//#endregion
export { shouldUnconditionallySuppress as i, buildSuppressedBuiltInModelError as n, shouldSuppressBuiltInModelCore as r, buildShouldSuppressBuiltInModelCore as t };
