import { n as loadPluginManifestRegistry } from "../manifest-registry-C9qOP1PN.js";
import { g as shouldPreferNativeJiti, n as buildPluginLoaderJitiOptions, t as buildPluginLoaderAliasMap } from "../sdk-alias-Czt6OgXf.js";
import { a as resolveDiscoveredProviderPluginIds } from "../plugin-auto-enable-CGAF5F6F.js";
import { n as resolvePluginProviders } from "../providers.runtime-BszPbZ7V.js";
import { createJiti } from "jiti";
//#region src/plugins/source-loader.ts
function shouldProfilePluginSourceLoader() {
	return process.env.KAIJIBOT_PLUGIN_LOAD_PROFILE === "1";
}
function createPluginSourceLoader() {
	const loaders = /* @__PURE__ */ new Map();
	return (modulePath) => {
		const tryNative = shouldPreferNativeJiti(modulePath);
		const aliasMap = buildPluginLoaderAliasMap(modulePath, process.argv[1], import.meta.url);
		const cacheKey = JSON.stringify({
			tryNative,
			aliasMap: Object.entries(aliasMap).toSorted(([left], [right]) => left.localeCompare(right))
		});
		let jiti = loaders.get(cacheKey);
		if (!jiti) {
			jiti = createJiti(import.meta.url, {
				...buildPluginLoaderJitiOptions(aliasMap),
				tryNative
			});
			loaders.set(cacheKey, jiti);
		}
		if (!shouldProfilePluginSourceLoader()) return jiti(modulePath);
		const startMs = performance.now();
		try {
			return jiti(modulePath);
		} finally {
			console.error(`[plugin-load-profile] phase=source-loader plugin=(direct) elapsedMs=${(performance.now() - startMs).toFixed(1)} source=${modulePath}`);
		}
	};
}
//#endregion
//#region src/plugins/provider-discovery.runtime.ts
function normalizeDiscoveryModule(value) {
	const resolved = value && typeof value === "object" && "default" in value && value.default !== void 0 ? value.default : value;
	if (Array.isArray(resolved)) return resolved;
	if (resolved && typeof resolved === "object" && "id" in resolved) return [resolved];
	if (value && typeof value === "object" && !Array.isArray(value)) {
		const record = value;
		if (Array.isArray(record.providers)) return record.providers;
		if (record.provider) return [record.provider];
	}
	return [];
}
function resolveProviderDiscoveryEntryPlugins(params) {
	const pluginIds = resolveDiscoveredProviderPluginIds(params);
	const pluginIdSet = new Set(pluginIds);
	const records = loadPluginManifestRegistry(params).plugins.filter((plugin) => plugin.providerDiscoverySource && pluginIdSet.has(plugin.id));
	if (records.length === 0) return [];
	const loadSource = createPluginSourceLoader();
	const providers = [];
	for (const manifest of records) try {
		const moduleExport = loadSource(manifest.providerDiscoverySource);
		providers.push(...normalizeDiscoveryModule(moduleExport).map((provider) => ({
			...provider,
			pluginId: manifest.id
		})));
	} catch {
		return [];
	}
	return providers;
}
function resolvePluginDiscoveryProvidersRuntime(params) {
	const entryProviders = resolveProviderDiscoveryEntryPlugins(params);
	if (entryProviders.length > 0) return entryProviders;
	return resolvePluginProviders({
		...params,
		bundledProviderAllowlistCompat: true
	});
}
//#endregion
export { resolvePluginDiscoveryProvidersRuntime };
