import "server-only"; import { queryAdminRows } from "@/lib/admin/database"; type FeatureFlagRow = { flag_key: string; enabled: boolean; rollout_percentage: number; config: Record; }; export type RuntimeFeatureFlag = { enabled: boolean; config: Record; }; type Cache = { expiresAt: number; flags: Map }; const state = globalThis as typeof globalThis & { jyotishaFeatureFlagCache?: Cache }; const cacheTtlMs = 15_000; export async function loadRuntimeFeatureFlags(keys: readonly string[]) { const cached = state.jyotishaFeatureFlagCache; if (cached && cached.expiresAt > Date.now() && keys.every((key) => cached.flags.has(key))) return cached.flags; const rows = await queryAdminRows(` select flag_key,enabled,rollout_percentage,config from public.feature_flags where status='published' and flag_key=any($1::text[]) `, [keys]); const flags = new Map(); for (const key of keys) flags.set(key, { enabled: false, config: {} }); for (const row of rows) { flags.set(row.flag_key, { enabled: row.enabled && row.rollout_percentage === 100, config: row.config, }); } state.jyotishaFeatureFlagCache = { expiresAt: Date.now() + cacheTtlMs, flags }; return flags; }