/**
 * Loop status line — renders a one-line summary for the TUI footer.
 *
 * Subscribes to LoopManager.onChange for immediate updates,
 * plus polls every 15s to keep the countdown fresh.
 *
 *   🔁 2 loops · next: "check staging" in 3m
 *   (nothing when no loops active)
 */
const STATUS_KEY = 'loop';
const POLL_INTERVAL_MS = 15_000;
// ---------------------------------------------------------------------------
// Time formatting
// ---------------------------------------------------------------------------
function formatDelta(ms) {
    if (ms < 0)
        return 'now';
    const secs = Math.round(ms / 1000);
    if (secs < 60)
        return `${secs}s`;
    const mins = Math.round(secs / 60);
    if (mins < 60)
        return `${mins}m`;
    const hours = Math.floor(mins / 60);
    const remainMins = mins % 60;
    return remainMins > 0 ? `${hours}h ${remainMins}m` : `${hours}h`;
}
export function computeLoopStatus(manager) {
    const loops = manager.list();
    const loopCount = loops.length;
    if (loopCount === 0) {
        return { text: '', loopCount: 0 };
    }
    const parts = [];
    parts.push(loopCount === 1 ? '1 loop' : `${loopCount} loops`);
    const next = manager.nextTick();
    if (next) {
        parts.push(`next: "${next.name}" in ${formatDelta(next.deltaMs)}`);
    }
    return { text: `🔁 ${parts.join(' · ')}`, loopCount };
}
/** Format the loop status as a turquoise badge (ANSI: black text on #008080 background). */
function formatBadge(text) {
    return `\x1b[48;2;0;128;128m\x1b[30m ${text} \x1b[39m\x1b[49m`;
}
/**
 * Start status polling and change-driven updates. Returns a cleanup function.
 */
export function startLoopStatusPolling(manager, setStatus, setEditorBadge) {
    const update = () => {
        try {
            const { text, loopCount } = computeLoopStatus(manager);
            setStatus(STATUS_KEY, loopCount > 0 ? text : undefined);
            setEditorBadge?.(loopCount > 0 ? formatBadge(text) : undefined);
        }
        catch {
            // Silently ignore — don't crash the TUI
        }
    };
    // Immediate first render
    update();
    // Subscribe to loop changes for immediate updates
    const unsubChange = manager.onChange(update);
    // Poll for countdown freshness
    const timer = setInterval(update, POLL_INTERVAL_MS);
    timer.unref();
    return () => {
        clearInterval(timer);
        unsubChange();
    };
}
//# sourceMappingURL=loop-status.js.map