import { HEARTBEAT_STALLED_AFTER_MS, HEARTBEAT_WARN_AFTER_MS } from './heartbeat.js';
export function deriveStuckRunStatus(inFlight, now, options = {}) {
    const warnAfterMs = options.warnAfterMs ?? HEARTBEAT_WARN_AFTER_MS;
    const stalledAfterMs = options.stalledAfterMs ?? HEARTBEAT_STALLED_AFTER_MS;
    let worst = 'ok';
    for (const op of inFlight) {
        const sinceLastTick = now - (op.startedAt + op.elapsedMs);
        if (sinceLastTick >= stalledAfterMs)
            return 'stalled';
        if (sinceLastTick >= warnAfterMs)
            worst = 'warn';
    }
    return worst;
}
/** Latest in-flight op by startedAt; UI's foreground spinner. */
export function pickLatestInFlightOp(inFlight) {
    if (inFlight.length === 0)
        return undefined;
    let latest = inFlight[0];
    for (let i = 1; i < inFlight.length; i++) {
        if (inFlight[i].startedAt > latest.startedAt)
            latest = inFlight[i];
    }
    return latest;
}
//# sourceMappingURL=stuck-run-watchdog.js.map