import { s as normalizeOptionalLowercaseString } from "./string-coerce-mnp54Vah.js";
import { g as sortUniqueStrings } from "./string-normalization-WNUDCpXX.js";
import { n as defaultSlotIdForKey } from "./slots-DT22cOei.js";
import { s as normalizePluginsConfig } from "./config-state-Bo3NFpc4.js";
import { i as passesManifestOwnerBasePolicy } from "./manifest-owner-policy-oq4uytXg.js";
import { a as listExplicitlyDisabledChannelIdsForConfig, o as listPotentialConfiguredChannelIds } from "./configured-model-refs-BbPeX06R.js";
import { T as resolveConfiguredChannelPluginIds, i as loadGatewayStartupPluginPlan, w as listExplicitConfiguredChannelIdsForConfig } from "./channel-plugin-ids-BH1H30QQ.js";
import { s as loadManifestMetadataSnapshot } from "./manifest-contract-eligibility-4s7nqe8A.js";
import { t as applyPluginAutoEnable } from "./plugin-auto-enable-LnLPkXha.js";
//#region src/plugins/effective-plugin-ids.ts
function collectConfiguredChannelIds(config, activationSourceConfig, env) {
	const disabled = new Set([...listExplicitlyDisabledChannelIdsForConfig(config), ...listExplicitlyDisabledChannelIdsForConfig(activationSourceConfig)]);
	return [...new Set([...listPotentialConfiguredChannelIds(config, env, { includePersistedAuthState: false }), ...listExplicitConfiguredChannelIdsForConfig(activationSourceConfig)])].map((channelId) => normalizeOptionalLowercaseString(channelId)).filter((channelId) => {
		if (!channelId) return false;
		return !disabled.has(channelId);
	}).toSorted((left, right) => left.localeCompare(right));
}
function collectBundledChannelOwnerPluginIds(params) {
	const plugins = normalizePluginsConfig(params.config.plugins);
	const channelIds = new Set(params.channelIds.map((channelId) => normalizeOptionalLowercaseString(channelId)).filter((channelId) => Boolean(channelId)));
	if (channelIds.size === 0) return [];
	const env = params.bundledPluginsDir ? {
		...params.env,
		DEX_BUNDLED_PLUGINS_DIR: params.bundledPluginsDir,
		...params.env.VITEST || process.env.VITEST ? { DEX_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1" } : {}
	} : params.env;
	const snapshot = loadManifestMetadataSnapshot({
		config: params.config,
		env,
		workspaceDir: params.workspaceDir
	});
	const pluginIds = /* @__PURE__ */ new Set();
	for (const plugin of snapshot.plugins) {
		if (plugin.origin !== "bundled") continue;
		if (plugin.channels.some((channelId) => channelIds.has(normalizeOptionalLowercaseString(channelId) ?? ""))) {
			const pluginId = normalizeOptionalLowercaseString(plugin.id);
			if (pluginId && passesManifestOwnerBasePolicy({
				plugin: { id: pluginId },
				normalizedConfig: plugins,
				allowRestrictiveAllowlistBypass: true
			})) pluginIds.add(pluginId);
		}
	}
	return sortUniqueStrings(pluginIds);
}
function collectExplicitEffectivePluginIds(config) {
	const plugins = normalizePluginsConfig(config.plugins);
	if (!plugins.enabled) return [];
	const ids = new Set(plugins.allow);
	for (const [pluginId, entry] of Object.entries(plugins.entries)) if (entry?.enabled === true && (plugins.allow.length === 0 || plugins.allow.includes(pluginId))) ids.add(pluginId);
	for (const pluginId of plugins.deny) ids.delete(pluginId);
	for (const [pluginId, entry] of Object.entries(plugins.entries)) if (entry?.enabled === false) ids.delete(pluginId);
	return sortUniqueStrings(ids);
}
function collectSelectedContextEnginePluginIds(config) {
	const plugins = normalizePluginsConfig(config.plugins);
	if (!plugins.enabled) return [];
	const pluginId = plugins.slots.contextEngine;
	if (!pluginId || pluginId === defaultSlotIdForKey("contextEngine")) return [];
	if (plugins.deny.includes(pluginId)) return [];
	if (plugins.entries[pluginId]?.enabled === false) return [];
	return [pluginId];
}
function resolveEffectivePluginIds(params) {
	const effectiveConfig = applyPluginAutoEnable({
		config: params.config,
		env: params.env
	}).config;
	const ids = new Set(collectExplicitEffectivePluginIds(effectiveConfig));
	for (const pluginId of collectSelectedContextEnginePluginIds(effectiveConfig)) ids.add(pluginId);
	const configuredChannelIds = collectConfiguredChannelIds(effectiveConfig, params.config, params.env);
	for (const pluginId of resolveConfiguredChannelPluginIds({
		config: effectiveConfig,
		activationSourceConfig: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env
	})) ids.add(pluginId);
	for (const pluginId of collectBundledChannelOwnerPluginIds({
		config: effectiveConfig,
		channelIds: configuredChannelIds,
		env: params.env,
		workspaceDir: params.workspaceDir,
		...params.bundledPluginsDir ? { bundledPluginsDir: params.bundledPluginsDir } : {}
	})) ids.add(pluginId);
	for (const pluginId of loadGatewayStartupPluginPlan({
		config: effectiveConfig,
		activationSourceConfig: params.config,
		workspaceDir: params.workspaceDir,
		env: params.env
	}).pluginIds) ids.add(pluginId);
	return sortUniqueStrings(ids);
}
//#endregion
export { resolveEffectivePluginIds as t };
