import { serializeMemoryAudit, serializeMemoryQualitySummary, serializeMemoryQuarantine, serializeMemoryRows } from '../serializers/memory-serializer.js';
import { collectDashboardBaseState } from './summary-selector.js';
export async function buildMemorySurface(ctx, url) {
    const state = collectDashboardBaseState(ctx, url);
    const workspaceMemories = ctx.listDashboardMemories({ limit: 40, lane: 'workspace' });
    return {
        repoIdentity: state.snapshot?.repoIdentity ?? ctx.getSelectedRepoIdentity(url),
        usage: state.usage,
        memory: serializeMemoryRows(workspaceMemories),
        workspace: serializeMemoryRows(workspaceMemories.slice(0, 28)),
        profile: serializeMemoryRows(ctx.listDashboardMemories({ limit: 18, lane: 'profile' })),
        shared: serializeMemoryRows(ctx.listDashboardMemories({ limit: 18, lane: 'shared' })),
        inbox: serializeMemoryRows(ctx.listDashboardMemories({ limit: 18, lane: 'inbox', includeHidden: true })),
        memoryHealth: state.memoryHealth,
        memoryContainers: state.memoryContainers,
        qualitySummary: serializeMemoryQualitySummary(ctx.collectMemoryQualitySummary(state.memoryHealth, state.memoryContainers)),
        memoryAudit: serializeMemoryAudit(state.runtime?.auditMemory?.(80)),
        memoryQuarantine: serializeMemoryQuarantine(state.runtime?.listMemoryQuarantine?.(40)),
        memoryShared: state.snapshot?.runtimeId && state.runtime?.getRuntimeId?.() === state.snapshot.runtimeId
            ? state.runtime.getSharedMemorySnapshot?.(12) ?? []
            : [],
        knowledgeFabricSession: state.snapshot?.knowledgeFabric ?? state.orchestrator?.getKnowledgeFabricSnapshot?.() ?? {},
        knowledgeProvenance: state.snapshot?.knowledgeFabric?.provenance ?? state.orchestrator?.getKnowledgeFabricProvenance?.() ?? { entries: [] },
        ragCollections: state.orchestrator?.listRagCollections?.() ?? [],
        patterns: state.orchestrator?.listPatterns?.()?.slice(0, 8) ?? [],
        modelTiers: state.snapshot?.knowledgeFabric
            ? {
                policy: state.snapshot.knowledgeFabric.modelTierPolicy,
                trace: state.snapshot.knowledgeFabric.modelTierTrace,
            }
            : state.orchestrator?.getModelTierTrace?.() ?? { trace: [] },
        nexusLayer: state.layer?.getSummary(state.snapshot?.orchestration?.sessionId) ?? null,
    };
}
