import { newOperationID } from './operationKey' export interface UISettings { id: string state: 'validated' | 'active' | 'retired' | 'failed' document_refresh_seconds: 15 | 30 | 60 resource_refresh_seconds: 30 | 60 stalled_after_seconds: 1800 | 3600 finalizing_after_seconds: 7200 | 14400 updated_by: string created_at: string activated_at: string | null retired_at: string | null error_code: string } export interface UISettingsCandidateInput { document_refresh_seconds: UISettings['document_refresh_seconds'] resource_refresh_seconds: UISettings['resource_refresh_seconds'] stalled_after_seconds: UISettings['stalled_after_seconds'] finalizing_after_seconds: UISettings['finalizing_after_seconds'] updated_by: string } export function settingsRefreshDelays(settings: Pick) { return { documentMs: settings.document_refresh_seconds * 1000, resourceMs: settings.resource_refresh_seconds * 1000 } } const states = new Set(['validated', 'active', 'retired', 'failed']) const documentRefresh = new Set([15, 30, 60]) const resourceRefresh = new Set([30, 60]) const stalledAfter = new Set([1800, 3600]) const finalizingAfter = new Set([7200, 14400]) const isRecord = (value: unknown): value is Record => Boolean(value) && typeof value === 'object' && !Array.isArray(value) export function parseUISettingsResponse(value: unknown): UISettings { if (!isRecord(value) || !isRecord(value.data)) throw new Error('invalid UI settings response') const item = value.data if (typeof item.id !== 'string' || !/^[A-Za-z0-9._:-]{2,36}$/.test(item.id) || typeof item.state !== 'string' || !states.has(item.state) || typeof item.document_refresh_seconds !== 'number' || !documentRefresh.has(item.document_refresh_seconds) || typeof item.resource_refresh_seconds !== 'number' || !resourceRefresh.has(item.resource_refresh_seconds) || typeof item.stalled_after_seconds !== 'number' || !stalledAfter.has(item.stalled_after_seconds) || typeof item.finalizing_after_seconds !== 'number' || !finalizingAfter.has(item.finalizing_after_seconds) || typeof item.updated_by !== 'string' || typeof item.created_at !== 'string' || (item.activated_at !== null && typeof item.activated_at !== 'string') || (item.retired_at !== null && typeof item.retired_at !== 'string') || typeof item.error_code !== 'string') { throw new Error('invalid UI settings response') } return { id: item.id, state: item.state as UISettings['state'], document_refresh_seconds: item.document_refresh_seconds as UISettings['document_refresh_seconds'], resource_refresh_seconds: item.resource_refresh_seconds as UISettings['resource_refresh_seconds'], stalled_after_seconds: item.stalled_after_seconds as UISettings['stalled_after_seconds'], finalizing_after_seconds: item.finalizing_after_seconds as UISettings['finalizing_after_seconds'], updated_by: item.updated_by, created_at: item.created_at, activated_at: item.activated_at as string | null, retired_at: item.retired_at as string | null, error_code: item.error_code, } } async function readSettings(response: Response): Promise { if (!response.ok) throw new Error(`UI settings request failed (${response.status})`) return parseUISettingsResponse(await response.json()) } export async function fetchActiveUISettings(signal?: AbortSignal): Promise { return readSettings(await fetch('/api/v1/ui-settings', { signal })) } export async function createUISettingsCandidate(input: UISettingsCandidateInput): Promise { return readSettings(await fetch('/api/v1/ui-settings/candidates', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Governance-Action': 'ui-settings', 'Idempotency-Key': newOperationID() }, body: JSON.stringify(input), })) } export async function activateUISettings(revisionID: string): Promise { if (!/^[A-Za-z0-9._:-]{2,36}$/.test(revisionID)) throw new Error('invalid UI settings revision') return readSettings(await fetch(`/api/v1/ui-settings/${encodeURIComponent(revisionID)}/activate`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Governance-Action': 'ui-settings', 'Idempotency-Key': newOperationID() }, body: '{}', })) }