import { m as hashJson } from "./installed-plugin-index-BVZVYQO0.js";
import { a as resolvePluginMetadataSnapshot, n as isPluginMetadataSnapshotCompatible } from "./plugin-metadata-snapshot-30GVjW7k.js";
import { c as resolveGatewayStartupPluginPlanFromRegistry } from "./channel-plugin-ids-A6opBWW0.js";
//#region src/plugins/plugin-lookup-table.ts
let lookupTableMemoBySnapshot = /* @__PURE__ */ new WeakMap();
function clearPluginLookUpTableMemoForTest() {
	lookupTableMemoBySnapshot = /* @__PURE__ */ new WeakMap();
}
function createPluginLookUpTableMemoKey(params) {
	return hashJson({
		activationSourceConfig: params.activationSourceConfig ?? null,
		config: params.config,
		env: params.env,
		indexPolicyHash: params.index?.policyHash ?? null,
		indexGeneratedAtMs: params.index?.generatedAtMs ?? null,
		indexPlugins: params.index?.plugins.map((plugin) => [
			plugin.pluginId,
			plugin.manifestHash,
			plugin.installRecordHash
		]) ?? null,
		workspaceDir: params.workspaceDir ?? null
	});
}
function loadPluginLookUpTable(params) {
	const requestedSnapshotConfig = params.activationSourceConfig ?? params.config;
	const metadataSnapshot = params.metadataSnapshot && isPluginMetadataSnapshotCompatible({
		snapshot: params.metadataSnapshot,
		config: requestedSnapshotConfig,
		env: params.env,
		workspaceDir: params.workspaceDir,
		index: params.index
	}) ? params.metadataSnapshot : resolvePluginMetadataSnapshot({
		config: requestedSnapshotConfig,
		workspaceDir: params.workspaceDir,
		env: params.env,
		allowWorkspaceScopedCurrent: params.workspaceDir === void 0,
		...params.index ? { index: params.index } : {}
	});
	const memoKey = createPluginLookUpTableMemoKey({
		config: params.config,
		...params.activationSourceConfig !== void 0 ? { activationSourceConfig: params.activationSourceConfig } : {},
		env: params.env,
		...params.workspaceDir !== void 0 ? { workspaceDir: params.workspaceDir } : {},
		...params.index !== void 0 ? { index: params.index } : {}
	});
	const memo = lookupTableMemoBySnapshot.get(metadataSnapshot)?.get(memoKey);
	if (memo) return memo;
	const { index, manifestRegistry } = metadataSnapshot;
	const startupPlanStartedAt = performance.now();
	const startup = resolveGatewayStartupPluginPlanFromRegistry({
		config: params.config,
		...params.activationSourceConfig !== void 0 ? { activationSourceConfig: params.activationSourceConfig } : {},
		env: params.env,
		index,
		manifestRegistry
	});
	const startupPlanMs = performance.now() - startupPlanStartedAt;
	const table = {
		...metadataSnapshot,
		key: hashJson({
			policyHash: index.policyHash,
			generatedAtMs: index.generatedAtMs,
			plugins: index.plugins.map((plugin) => [
				plugin.pluginId,
				plugin.manifestHash,
				plugin.installRecordHash
			]),
			startup
		}),
		startup,
		metrics: {
			...metadataSnapshot.metrics,
			startupPlanMs,
			totalMs: metadataSnapshot.metrics.totalMs + startupPlanMs,
			startupPluginCount: startup.pluginIds.length,
			deferredChannelPluginCount: startup.configuredDeferredChannelPluginIds.length
		}
	};
	let memoByKey = lookupTableMemoBySnapshot.get(metadataSnapshot);
	if (!memoByKey) {
		memoByKey = /* @__PURE__ */ new Map();
		lookupTableMemoBySnapshot.set(metadataSnapshot, memoByKey);
	}
	memoByKey.set(memoKey, table);
	return table;
}
//#endregion
export { loadPluginLookUpTable as n, clearPluginLookUpTableMemoForTest as t };
