import "server-only"; import { callMobileApi } from "@/app/lib/server/mobileApi"; // In-memory per-locale catalog cache. This is the cache-and-serve analog of // member-portal's in-memory MessageSource; clearMessageCache() is the seam for // a future push/on-demand invalidation (and is used by tests). const TTL_MS = 15 * 60 * 1000; interface CacheEntry { data: Record; expiresAt: number; } const cache = new Map(); export function clearMessageCache(): void { cache.clear(); } /** Load the message catalog for a locale, cached for TTL_MS. `now` is injectable for tests. */ export async function loadMessageCatalog( locale: string, now: number = Date.now(), ): Promise> { const hit = cache.get(locale); if (hit && hit.expiresAt > now) return hit.data; const result = await callMobileApi>( `/api/public/messages/${encodeURIComponent(locale)}`, ); if (!result.ok || !result.data) { return hit ? hit.data : {}; } cache.set(locale, { data: result.data, expiresAt: now + TTL_MS }); return result.data; }