/**
 * GET /state/sync-status — sanitized diagnostics for the bridge sync surface.
 *
 * Body shape:
 *   {
 *     enabled: boolean,
 *     endpointConfigured: boolean,
 *     retryBudget: number,
 *     intervalMs: number,
 *     enabledDomains: SeedDomainId[],
 *     domains: {
 *       [domain]: {
 *         lastAttemptAt: string | null,
 *         lastSuccessAt: string | null,
 *         pending: number,
 *         failed: number,
 *         lastFailureReason: string | null,
 *       }
 *     }
 *   }
 *
 * No raw URLs, no provider IDs, no paths.
 */
import { SEED_DOMAIN_IDS, summarizeSyncConfig, } from "../config/bridgeConfig.js";
import { writeJson } from "./publicResponse.js";
export async function handleSyncStatusRoute(context) {
    const { request, response, url, app } = context;
    if (request.method !== "GET" || url.pathname !== "/state/sync-status") {
        return false;
    }
    const payload = buildSyncStatusPayload(app.localStore, app.syncConfig);
    writeJson(response, 200, payload, { surface: "state.sync-status" });
    return true;
}
export function buildSyncStatusPayload(store, sync) {
    const summary = summarizeSyncConfig(sync);
    const domains = {};
    for (const domain of SEED_DOMAIN_IDS) {
        const states = store.listSyncStateForDomain(domain);
        let lastAttemptAt = null;
        let lastSuccessAt = null;
        let pending = 0;
        let failed = 0;
        let lastFailureReason = null;
        for (const s of states) {
            if (s.lastAttemptAt) {
                if (!lastAttemptAt || s.lastAttemptAt > lastAttemptAt)
                    lastAttemptAt = s.lastAttemptAt;
            }
            if (s.lastSuccessAt) {
                if (!lastSuccessAt || s.lastSuccessAt > lastSuccessAt)
                    lastSuccessAt = s.lastSuccessAt;
            }
            else {
                pending += 1;
            }
            if (s.retryCount > 0 && !s.lastSuccessAt)
                failed += 1;
            if (s.lastFailureReason && !lastFailureReason)
                lastFailureReason = s.lastFailureReason;
        }
        domains[domain] = {
            lastAttemptAt,
            lastSuccessAt,
            pending,
            failed,
            lastFailureReason,
        };
    }
    return {
        enabled: summary.enabled,
        endpointConfigured: summary.endpointConfigured,
        retryBudget: summary.retryBudget,
        intervalMs: summary.intervalMs,
        enabledDomains: summary.enabledDomains,
        domainDepths: summary.domainDepths,
        domains,
    };
}
void null;
