/**
 * api-client/progress.ts — Polling progress display for the terminal.
 *
 * Creates a callback function that the ApiClient's `waitForCompletion()`
 * calls after each poll. Tracks elapsed time and emits single-line status
 * updates so the user sees real-time feedback during remote evaluation.
 */
/**
 * Create a progress display callback for `waitForCompletion()`.
 *
 * Usage:
 * ```ts
 * const job = await client.waitForCompletion(jobId, {
 *   onProgress: createProgressDisplay(),
 * })
 * ```
 *
 * Output example:
 * ```
 * ⏳ [queued] Waiting for runner...
 * ⏳ [running] Step 2/8: fetch-docs (1m 12s)
 * ⏳ [running] Step 5/8: run-eval (3m 45s)
 * ```
 */
export function createProgressDisplay() {
    const startMs = Date.now();
    let lastLine = "";
    return (job) => {
        const elapsed = formatElapsed(Date.now() - startMs);
        let line;
        switch (job.status) {
            case "received":
                line = `⏳ [received] Submitted, waiting for API... (${elapsed})`;
                break;
            case "queued":
                line = `⏳ [queued] Waiting for runner... (${elapsed})`;
                break;
            case "running": {
                if (job.progress?.step && job.progress.current && job.progress.total) {
                    const { step, current, total } = job.progress;
                    line = `⏳ [running] Step ${current}/${total}: ${step} (${elapsed})`;
                }
                else {
                    line = `⏳ [running] Evaluating... (${elapsed})`;
                }
                break;
            }
            default:
                // Terminal states — don't display; waitForCompletion handles them
                return;
        }
        // Only print if the line changed (avoids duplicates during long-poll)
        if (line !== lastLine) {
            console.log(line);
            lastLine = line;
        }
    };
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function formatElapsed(ms) {
    const seconds = Math.floor(ms / 1000);
    if (seconds < 60)
        return `${seconds}s`;
    const minutes = Math.floor(seconds / 60);
    const remaining = seconds % 60;
    return `${minutes}m ${remaining}s`;
}
