import { serializeGateTimeline, serializeParallelism, serializeRuns, serializeSelectionAudit } from '../serializers/run-serializer.js';
import { collectDashboardBaseState } from './summary-selector.js';
export async function buildRunsSurface(ctx, url) {
    const state = collectDashboardBaseState(ctx, url);
    return {
        repoIdentity: state.snapshot?.repoIdentity ?? ctx.getSelectedRepoIdentity(url),
        runs: serializeRuns(state.runtime?.listRuns?.(20) ?? []),
        usage: state.usage,
        gateSummary: state.gateSummary,
        selectionAudit: serializeSelectionAudit(state.usage),
        gateTimeline: serializeGateTimeline(state.latestRun),
        parallelism: serializeParallelism(state.snapshot, state.runtime),
        tokenOptimization: state.tokenOptimization,
        sourceAwareTokenBudget: state.snapshot?.sourceAwareTokenBudget ?? state.runtime?.getUsageSnapshot?.()?.sourceAwareTokenBudget ?? {},
        agentFlow: state.snapshot?.instructionPacket?.selectionPlan?.executionPolicy?.agentFlow
            ?? state.snapshot?.executionLedger?.agentFlow
            ?? state.runtime?.getUsageSnapshot?.()?.selectionPlan?.executionPolicy?.agentFlow
            ?? {},
        orchestrationSession: state.snapshot?.orchestration ?? state.orchestrator?.getSessionState?.() ?? {},
        orchestrationLedger: state.snapshot?.executionLedger ?? state.runtime?.getExecutionLedger?.() ?? {},
        workerPlan: state.snapshot?.workerPlan ?? state.runtime?.getUsageSnapshot?.()?.workerPlan ?? {},
        artifactOutcomes: state.snapshot?.artifactOutcome ?? state.runtime?.getUsageSnapshot?.()?.artifactOutcome ?? {},
        instructionPacket: state.snapshot?.instructionPacket ?? state.runtime?.getInstructionPacket?.() ?? {},
        tokensSummary: state.snapshot?.tokens ?? state.runtime?.getTokenTelemetrySummary?.() ?? {},
        tokensTimeline: state.snapshot?.tokens?.timeline ?? state.runtime?.getTokenTelemetryTimeline?.(20) ?? [],
        tokensBySource: state.snapshot?.tokens?.bySourceClass ?? state.runtime?.getTokenTelemetrySummary?.()?.bySourceClass ?? {},
        worktreeHealth: state.snapshot?.worktreeHealth ?? state.runtime?.getUsageSnapshot?.()?.worktreeHealth ?? {},
    };
}
