import { primaryConfigToken, projectLocaleToken, resolveLocale, type PluginId, type PrimaryConfig, type RuntimeConfigDocument, type SnapshotState, type TokenId } from '@zhin.js/plugin-runtime'; import type { EnvStore } from './environment-store.js'; export { primaryConfigToken, type PrimaryConfig } from '@zhin.js/plugin-runtime'; export function createPrimaryConfig( document: RuntimeConfigDocument, environment: EnvStore, compositionMode: NonNullable = 'unknown', ): PrimaryConfig { const safeDocument = freezeConfig(structuredClone(document)); const expanded = freezeConfig(environment.expandMissingAsEmpty(safeDocument)); return Object.freeze({ compositionMode, document: safeDocument, expanded, get(key: string): T | undefined { return expanded[key] as T | undefined; }, }); } /** Copies bindings so retained Scopes never mutate a leased generation's view. */ export function projectPrimaryConfigResources( resources: SnapshotState['resources'], config: PrimaryConfig, ): Map> { return new Map([...resources].map(([owner, bindings]) => [ owner, new Map([...bindings, [primaryConfigToken.id, config], [projectLocaleToken.id, resolveLocale(config.get('locale'))]]), ])); } function freezeConfig(value: T): T { if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value; for (const child of Object.values(value)) freezeConfig(child); return Object.freeze(value); }