import { o as getActivePluginRegistryWorkspaceDir } from "./runtime-DvDGh8U9.js";
import { a as resolveDiscoveredProviderPluginIds, c as resolveOwningPluginIdsForProvider, l as withBundledProviderVitestCompat, o as resolveEnabledProviderPluginIds, r as resolveBundledProviderCompatPluginIds, s as resolveOwningPluginIdsForModelRefs } from "./plugin-auto-enable-CGAF5F6F.js";
import { n as withActivatedPluginIds, t as resolveBundledPluginCompatibleActivationInputs } from "./activation-context-CHNiRr_Q.js";
import { a as resolveRuntimePluginRegistry, r as loadKaijiBotPlugins, t as isPluginRegistryLoadInFlight } from "./loader-D74RThYg.js";
import { n as buildPluginRuntimeLoadOptionsFromValues, r as createPluginRuntimeLoaderLogger } from "./load-context-DTo2jAvt.js";
//#region src/plugins/providers.runtime.ts
function resolvePluginProviderLoadBase(params) {
	const env = params.env ?? process.env;
	const workspaceDir = params.workspaceDir ?? getActivePluginRegistryWorkspaceDir();
	const providerOwnedPluginIds = params.providerRefs?.length ? [...new Set(params.providerRefs.flatMap((provider) => resolveOwningPluginIdsForProvider({
		provider,
		config: params.config,
		workspaceDir,
		env
	}) ?? []))] : [];
	const modelOwnedPluginIds = params.modelRefs?.length ? resolveOwningPluginIdsForModelRefs({
		models: params.modelRefs,
		config: params.config,
		workspaceDir,
		env
	}) : [];
	return {
		env,
		workspaceDir,
		requestedPluginIds: params.onlyPluginIds || params.providerRefs?.length || params.modelRefs?.length || providerOwnedPluginIds.length > 0 || modelOwnedPluginIds.length > 0 ? [...new Set([
			...params.onlyPluginIds ?? [],
			...providerOwnedPluginIds,
			...modelOwnedPluginIds
		])].toSorted((left, right) => left.localeCompare(right)) : void 0,
		runtimeConfig: withActivatedPluginIds({
			config: params.config,
			pluginIds: [...providerOwnedPluginIds, ...modelOwnedPluginIds]
		})
	};
}
function resolveSetupProviderPluginLoadState(params, base) {
	const providerPluginIds = resolveDiscoveredProviderPluginIds({
		config: base.runtimeConfig,
		workspaceDir: base.workspaceDir,
		env: base.env,
		onlyPluginIds: base.requestedPluginIds,
		includeUntrustedWorkspacePlugins: params.includeUntrustedWorkspacePlugins
	});
	if (providerPluginIds.length === 0) return;
	return { loadOptions: buildPluginRuntimeLoadOptionsFromValues({
		config: withActivatedPluginIds({
			config: base.runtimeConfig,
			pluginIds: providerPluginIds
		}),
		activationSourceConfig: base.runtimeConfig,
		autoEnabledReasons: {},
		workspaceDir: base.workspaceDir,
		env: base.env,
		logger: createPluginRuntimeLoaderLogger()
	}, {
		onlyPluginIds: providerPluginIds,
		pluginSdkResolution: params.pluginSdkResolution,
		cache: params.cache ?? false,
		activate: params.activate ?? false
	}) };
}
function resolveRuntimeProviderPluginLoadState(params, base) {
	const activation = resolveBundledPluginCompatibleActivationInputs({
		rawConfig: base.runtimeConfig,
		env: base.env,
		workspaceDir: base.workspaceDir,
		onlyPluginIds: base.requestedPluginIds,
		applyAutoEnable: true,
		compatMode: {
			allowlist: params.bundledProviderAllowlistCompat,
			enablement: "allowlist",
			vitest: params.bundledProviderVitestCompat
		},
		resolveCompatPluginIds: resolveBundledProviderCompatPluginIds
	});
	const config = params.bundledProviderVitestCompat ? withBundledProviderVitestCompat({
		config: activation.config,
		pluginIds: activation.compatPluginIds,
		env: base.env
	}) : activation.config;
	const providerPluginIds = resolveEnabledProviderPluginIds({
		config,
		workspaceDir: base.workspaceDir,
		env: base.env,
		onlyPluginIds: base.requestedPluginIds
	});
	return { loadOptions: buildPluginRuntimeLoadOptionsFromValues({
		config,
		activationSourceConfig: activation.activationSourceConfig,
		autoEnabledReasons: activation.autoEnabledReasons,
		workspaceDir: base.workspaceDir,
		env: base.env,
		logger: createPluginRuntimeLoaderLogger()
	}, {
		onlyPluginIds: providerPluginIds,
		pluginSdkResolution: params.pluginSdkResolution,
		cache: params.cache ?? false,
		activate: params.activate ?? false
	}) };
}
function isPluginProvidersLoadInFlight(params) {
	const base = resolvePluginProviderLoadBase(params);
	const loadState = params.mode === "setup" ? resolveSetupProviderPluginLoadState(params, base) : resolveRuntimeProviderPluginLoadState(params, base);
	if (!loadState) return false;
	return isPluginRegistryLoadInFlight(loadState.loadOptions);
}
function resolvePluginProviders(params) {
	const base = resolvePluginProviderLoadBase(params);
	if (params.mode === "setup") {
		const loadState = resolveSetupProviderPluginLoadState(params, base);
		if (!loadState) return [];
		return loadKaijiBotPlugins(loadState.loadOptions).providers.map((entry) => ({
			...entry.provider,
			pluginId: entry.pluginId
		}));
	}
	const registry = resolveRuntimePluginRegistry(resolveRuntimeProviderPluginLoadState(params, base).loadOptions);
	if (!registry) return [];
	return registry.providers.map((entry) => ({
		...entry.provider,
		pluginId: entry.pluginId
	}));
}
//#endregion
export { resolvePluginProviders as n, isPluginProvidersLoadInFlight as t };
