// orchestrator/progress-tracker.ts — Progress tracking (SPEC-079 AC-07)
// Tracks agent and task status, emits progress events via callbacks.
/**
 * Progress tracker for an orchestration session.
 * Uses callback pattern (not EventEmitter) to avoid extra dependencies.
 */
export class ProgressTracker {
    sessionId;
    agents = new Map();
    tasks = new Map();
    callbacks = [];
    timeline = [];
    constructor(sessionId) {
        this.sessionId = sessionId;
    }
    /** Register a progress callback. */
    onProgress(callback) {
        this.callbacks.push(callback);
    }
    /** Initialize tracking for agents and tasks. */
    init(agentProcesses, assignments) {
        for (const agent of agentProcesses) {
            this.agents.set(agent.agentId, agent.status);
        }
        for (const task of assignments) {
            this.tasks.set(task.taskId, {
                taskId: task.taskId,
                agentId: task.agentId,
                status: 'queued',
            });
        }
    }
    /** Update agent status and emit event. */
    updateAgentStatus(agentId, status) {
        this.agents.set(agentId, status);
        this.emit({
            sessionId: this.sessionId,
            agentId,
            type: 'agent-status',
            data: { status },
            timestamp: new Date().toISOString(),
        });
    }
    /** Update task status and emit event. */
    updateTaskStatus(taskId, status) {
        const tracked = this.tasks.get(taskId);
        if (tracked) {
            tracked.status = status;
            if (status === 'in_progress' && !tracked.startedAt) {
                tracked.startedAt = new Date().toISOString();
            }
            if (status === 'completed' || status === 'failed') {
                tracked.completedAt = new Date().toISOString();
            }
        }
        this.emit({
            sessionId: this.sessionId,
            taskId,
            type: 'task-status',
            data: { status },
            timestamp: new Date().toISOString(),
        });
    }
    /** Calculate completion percentage. */
    getCompletionPercent() {
        const total = this.tasks.size;
        if (total === 0) {
            return 100;
        }
        let completed = 0;
        for (const task of this.tasks.values()) {
            if (task.status === 'completed') {
                completed++;
            }
        }
        return Math.round((completed / total) * 100);
    }
    /** Get timeline of all events. */
    getTimeline() {
        return [...this.timeline];
    }
    /** Get current agent statuses. */
    getAgentStatuses() {
        return Object.fromEntries(this.agents);
    }
    /** Get current task statuses. */
    getTaskStatuses() {
        const result = {};
        for (const [id, tracked] of this.tasks) {
            result[id] = tracked.status;
        }
        return result;
    }
    /** Emit a progress event to all callbacks. */
    emit(event) {
        this.timeline.push(event);
        for (const cb of this.callbacks) {
            cb(event);
        }
    }
}
//# sourceMappingURL=progress-tracker.js.map