import { getSharedGovernor } from '../../engines/machine-efficiency/index.js';
import { serializeMemoryAudit, serializeMemoryQuarantine } from '../serializers/memory-serializer.js';
import { collectDashboardBaseState } from './summary-selector.js';
export async function buildTrustSurface(ctx, url) {
    const state = collectDashboardBaseState(ctx, url);
    const governor = getSharedGovernor();
    const governorState = governor.getState();
    const synapseTeams = ctx.getSynapse()?.getStrikeTeamStatus?.();
    const synapseHealth = ctx.getSynapse()?.getOperativeHealth?.();
    return {
        repoIdentity: state.snapshot?.repoIdentity ?? ctx.getSelectedRepoIdentity(url),
        usage: state.usage,
        interpretationIssues: state.interpretationIssues,
        preCompactionBackup: state.memory?.getLastPreCompactionBackup?.() ?? null,
        memoryContainers: state.memoryContainers,
        health: await ctx.getCachedValue('health', 15_000, () => ctx.collectHealth()),
        worktreeHealth: state.snapshot?.worktreeHealth ?? state.runtime?.getUsageSnapshot?.()?.worktreeHealth ?? {},
        federation: state.runtime?.getNetworkStatus?.() ?? {},
        memoryAudit: serializeMemoryAudit(state.runtime?.auditMemory?.(80)),
        memoryQuarantine: serializeMemoryQuarantine(state.runtime?.listMemoryQuarantine?.(40)),
        synapseTeams: Array.isArray(synapseTeams) ? synapseTeams : synapseTeams ? [synapseTeams] : [],
        synapseHealth: Array.isArray(synapseHealth) ? synapseHealth : synapseHealth ? [synapseHealth] : [],
        synapseApprovals: ctx.getSynapse()?.getPendingApprovals?.() ?? [],
        architectsDispatch: ctx.getArchitects()?.getDispatchStatus?.() ?? {},
        architectsEscalations: ctx.getArchitects()?.getWardEscalations?.() ?? [],
        architectsWorklist: ctx.getPreferredArchitectsWorklist(url),
        clients: state.snapshot?.clients?.detected?.length
            ? state.snapshot.clients.detected
            : ctx.getClientRegistry()?.listClients(ctx.getAdapters()) ?? [],
        primaryClient: state.snapshot?.clients?.primary ?? ctx.getClientRegistry()?.getPrimaryClient(ctx.getAdapters()) ?? null,
        nexusLayer: state.layer?.getSummary(state.snapshot?.orchestration?.sessionId) ?? null,
        machinePressure: governorState.pressureLevel,
        activeWorktrees: governorState.activeWorktrees,
        governor: governorState,
    };
}
