export type RuntimeRecordType = 'role' | 'dashboard' | 'onboarding'; export type RuntimeRecordStatus = 'draft' | 'published'; const KEY = 'nxtgauge_admin_runtime_builder_v1'; type RuntimeStore = { role: Record>; dashboard: Record>; onboarding: Record>; }; export type RuntimeStoredRecord = { status: RuntimeRecordStatus; updatedAt: string; payload: T; }; export type RuntimeListItem = RuntimeStoredRecord & { key: string; }; function emptyStore(): RuntimeStore { return { role: {}, dashboard: {}, onboarding: {} }; } function readStore(): RuntimeStore { if (typeof window === 'undefined') return emptyStore(); const raw = window.localStorage.getItem(KEY); if (!raw) return emptyStore(); try { const parsed = JSON.parse(raw) as Partial; return { role: parsed.role || {}, dashboard: parsed.dashboard || {}, onboarding: parsed.onboarding || {}, }; } catch { return emptyStore(); } } function writeStore(store: RuntimeStore) { if (typeof window === 'undefined') return; window.localStorage.setItem(KEY, JSON.stringify(store)); } export function saveRuntimeConfig(type: RuntimeRecordType, key: string, payload: unknown, status: RuntimeRecordStatus) { const normalizedKey = key.trim(); if (!normalizedKey) return; const store = readStore(); store[type][normalizedKey] = { status, updatedAt: new Date().toISOString(), payload, }; writeStore(store); } export function listRuntimeConfigs(type: RuntimeRecordType): Array> { const store = readStore(); return Object.entries(store[type]) .map(([key, value]) => ({ key, ...(value as RuntimeStoredRecord) })) .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)); } export function getRuntimeConfig(type: RuntimeRecordType, key: string): RuntimeStoredRecord | null { const record = readStore()[type][key]; return (record as RuntimeStoredRecord) || null; } export function deleteRuntimeConfig(type: RuntimeRecordType, key: string): boolean { const store = readStore(); if (!store[type][key]) return false; delete store[type][key]; writeStore(store); return true; }