import type { VMState } from "../index.js"; /** * VA-P23 · mailbox connect-settings view-model (M09 Block B/D). Pure (DoD A.3): * the loader resolves the CURRENT user's own mailboxes (read_all admins see all) * and this builds the settings list. NO tokens/credentials/clientState ever * appear here. `configuredProviders` reflects the OAuth env presence so the UI * shows a Klartext "not configured" hint instead of a dead connect button. * `initialSync` aggregates the per-folder cursor rows into one progress line. */ export type EmailMailboxProvider = "gmail" | "microsoft"; export type EmailMailboxStatus = "connecting" | "active" | "paused" | "error" | "revoked"; export type EmailInitialSyncStatus = "pending" | "running" | "completed" | "failed"; export interface EmailMailboxInitialSync { readonly status: EmailInitialSyncStatus; /** i18n key, e.g. `emailInbox.settings.initialSync.running`. */ readonly statusKey: string; /** 0–100 when derivable from the cursor progress rows, else null. */ readonly progressPercent: number | null; /** Fetched message count when known (Klartext progress line). */ readonly fetchedCount: number | null; } export interface EmailMailboxRow { readonly id: string; readonly provider: EmailMailboxProvider; /** i18n key for the provider label (brand names route through i18n, M003). */ readonly providerKey: string; readonly emailAddress: string; readonly displayName: string | null; readonly isDefaultForUser: boolean; /** True when the row belongs to the current user (owner actions enabled). */ readonly isOwn: boolean; readonly status: EmailMailboxStatus; /** i18n key for the status pill (e.g. `emailInbox.mailbox.status.active`). */ readonly statusKey: string; /** ISO timestamp of the last successful sync, or null. */ readonly lastSyncAt: string | null; /** Klartext error i18n key/text when status = error (M002), else null. */ readonly lastErrorText: string | null; readonly initialSync: EmailMailboxInitialSync; } export interface EmailMailboxListData { readonly rows: readonly EmailMailboxRow[]; /** True when the user may connect a mailbox (email.write bundles it). */ readonly canConnect: boolean; readonly configuredProviders: { readonly gmail: boolean; readonly microsoft: boolean }; readonly emptyHeadlineKey: string; readonly emptyBodyKey: string; readonly notConfiguredKey: string; } export type EmailMailboxListVM = VMState; export function emailMailboxStatusKey(status: EmailMailboxStatus): string { return `emailInbox.mailbox.status.${status}`; } export function emailMailboxProviderKey(provider: EmailMailboxProvider): string { return `emailInbox.provider.${provider}`; } export function emailInitialSyncStatusKey(status: EmailInitialSyncStatus): string { return `emailInbox.settings.initialSync.${status}`; } /** One per-folder cursor row as the loader reads it (progress is JSONB). */ export interface EmailSyncCursorInput { readonly mailboxId: string; readonly initialSyncStatus: EmailInitialSyncStatus; readonly initialSyncProgress: unknown; } /** * Aggregate the per-folder cursor rows of ONE mailbox into a single progress * line: worst status wins (failed > running > pending > completed); percent is * the mean of folder percents when every folder reports one. */ export function aggregateInitialSync(cursors: readonly EmailSyncCursorInput[]): EmailMailboxInitialSync { if (cursors.length === 0) { return { status: "pending", statusKey: emailInitialSyncStatusKey("pending"), progressPercent: null, fetchedCount: null }; } const order: Record = { failed: 3, running: 2, pending: 1, completed: 0 }; let status: EmailInitialSyncStatus = "completed"; let fetchedCount = 0; let sawFetched = false; const percents: number[] = []; for (const cursor of cursors) { if (order[cursor.initialSyncStatus] > order[status]) { status = cursor.initialSyncStatus; } const progress = cursor.initialSyncProgress as { percent?: unknown; fetchedCount?: unknown } | null; if (progress && typeof progress.percent === "number") { percents.push(Math.max(0, Math.min(100, progress.percent))); } if (progress && typeof progress.fetchedCount === "number") { fetchedCount += progress.fetchedCount; sawFetched = true; } } const progressPercent = status === "completed" ? 100 : percents.length === cursors.length && percents.length > 0 ? Math.round(percents.reduce((a, b) => a + b, 0) / percents.length) : null; return { status, statusKey: emailInitialSyncStatusKey(status), progressPercent, fetchedCount: sawFetched ? fetchedCount : null, }; } export interface BuildEmailMailboxListInput { readonly rows: readonly EmailMailboxRow[]; readonly canConnect: boolean; readonly configuredProviders: { readonly gmail: boolean; readonly microsoft: boolean }; } export function buildEmailMailboxListVM(input: BuildEmailMailboxListInput): EmailMailboxListVM { // Always "ready" (mirrors the calendar connection VM): the UI renders the // empty-state from the keys when `rows` is empty, keeping the connect // affordance + config hint available. return { status: "ready", data: { rows: input.rows, canConnect: input.canConnect, configuredProviders: input.configuredProviders, emptyHeadlineKey: "emailInbox.settings.empty.headline", emptyBodyKey: "emailInbox.settings.empty.body", notConfiguredKey: "emailInbox.settings.notConfigured", }, }; }