import { validationError } from "../errors.js"; import { CorpConfig, GlobalConfig } from "./types.js"; export function resolveCorpConfig( corps: CorpConfig[], options: Pick & { corpId?: string }, candidates: string[] = [] ): CorpConfig { const requestedCorpId = options.corpId?.trim() || undefined; const configuredCorpId = options.stateful ? options.defaultCorpId?.trim() || undefined : undefined; const selectedCorpId = requestedCorpId ?? configuredCorpId; if (selectedCorpId) { const exact = corps.find((item) => item.id === selectedCorpId); if (exact) return exact; const caseInsensitive = corps.find((item) => item.id.toLowerCase() === selectedCorpId.toLowerCase()); if (caseInsensitive) return caseInsensitive; const byName = corps.find((item) => item.name.toLowerCase() === selectedCorpId.toLowerCase()); if (byName) return byName; throw validationError(`No enterprise config found for ${selectedCorpId}`); } if (!options.stateful) { throw validationError("Stateless mode is enabled. Every enterprise command requires --corp . " + "Use `seal-use config mode stateful` to enable a default enterprise."); } const firstCorp = corps[0]; if (firstCorp) return firstCorp; throw validationError("No enterprise config found. Add a non-example JSON file under one of: " + (candidates.length ? candidates.join(", ") : "the configured enterprise directories")); } export type CorpContextOptions = Pick; export class CorpContext { private current: CorpConfig | null = null; private corps = new Map(); private stateful = false; private defaultCorpId: string | undefined; constructor(corps: CorpConfig[], options: CorpContextOptions = { stateful: false }) { this.stateful = options.stateful; this.defaultCorpId = options.defaultCorpId; this.replaceCorps(corps); this.selectCurrent(corps[0]); } refresh(corps: CorpConfig[], options: CorpContextOptions = { stateful: this.stateful, defaultCorpId: this.defaultCorpId }): void { const currentId = this.current?.id; this.stateful = options.stateful; this.defaultCorpId = options.defaultCorpId; this.replaceCorps(corps); this.selectCurrent(currentId ? this.corps.get(currentId) ?? corps[0] : corps[0]); } private replaceCorps(corps: CorpConfig[]): void { this.corps = new Map(); for (const corp of corps) { this.corps.set(corp.id, corp); } } getCurrent(): CorpConfig | null { return this.current; } getCorp(corpId: string): CorpConfig | null { return this.corps.get(corpId) ?? null; } resolve(corpId?: string): CorpConfig | null { if (corpId) return this.getCorp(corpId); return this.current; } switchCorp(corpId: string): boolean { if (!this.stateful) return false; const corp = this.corps.get(corpId); if (!corp) return false; this.current = corp; this.defaultCorpId = corp.id; return true; } listCorps(): Array<{ id: string; name: string; sourceType: string; current: boolean }> { return Array.from(this.corps.values()).map((corp) => ({ id: corp.id, name: corp.name, sourceType: corp.source.type, current: corp.id === this.current?.id })); } private selectCurrent(fallback?: CorpConfig): void { if (!this.stateful) { this.current = null; return; } this.current = this.defaultCorpId !== undefined ? this.corps.get(this.defaultCorpId) ?? null : fallback ?? null; } }