/**
 * Cron status line — renders a one-line summary for the TUI footer.
 *
 * Reads jobs.json (cached) and PID file to build a status string like:
 *   ⏰ 3 jobs · next: "Morning Report" in 2m · scheduler: running
 *   ⏰ 0 jobs · scheduler: stopped
 */
import { getDaemonStatus } from '../../app/cron/daemon-ipc.js';
import { getCronStorePath, loadCronStore } from '../../app/cron/store.js';
const STATUS_KEY = 'cron';
const POLL_INTERVAL_MS = 15_000;
let timer = null;
// ---------------------------------------------------------------------------
// 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 async function computeCronStatus(storePath = getCronStorePath()) {
    const daemon = getDaemonStatus();
    let jobCount = 0;
    let nextJobName;
    let nextRunMs;
    try {
        const store = await loadCronStore(storePath);
        const enabled = store.jobs.filter((j) => j.enabled);
        jobCount = enabled.length;
        // Find the next job to fire
        let nearestMs = Infinity;
        for (const job of enabled) {
            const next = job.state.nextRunAtMs;
            if (typeof next === 'number' && next < nearestMs) {
                nearestMs = next;
                nextJobName = job.name;
                nextRunMs = next;
            }
        }
    }
    catch {
        // Store doesn't exist or is corrupt — show minimal status
    }
    const parts = [];
    // Job count
    const jobLabel = jobCount === 1 ? '1 job' : `${jobCount} jobs`;
    parts.push(jobLabel);
    // Next fire
    if (nextRunMs != null && nextJobName) {
        const delta = nextRunMs - Date.now();
        parts.push(`next: "${nextJobName}" in ${formatDelta(delta)}`);
    }
    // Scheduler status
    parts.push(`scheduler: ${daemon.running ? 'running' : 'stopped'}`);
    const text = `⏰ ${parts.join(' · ')}`;
    return { text, jobCount, schedulerRunning: daemon.running };
}
/**
 * Start polling cron status and updating the TUI footer.
 * Call once from main.ts after InteractiveMode is ready.
 */
export function startCronStatusPolling(setStatus, storePath = getCronStorePath()) {
    stopCronStatusPolling();
    const update = async () => {
        try {
            const { text, jobCount, schedulerRunning } = await computeCronStatus(storePath);
            // Only show status if there are jobs or the scheduler is running
            if (jobCount > 0 || schedulerRunning) {
                setStatus(STATUS_KEY, text);
            }
            else {
                setStatus(STATUS_KEY, undefined);
            }
        }
        catch {
            // Silently ignore — don't crash the TUI
        }
    };
    // Initial update
    void update();
    timer = setInterval(() => void update(), POLL_INTERVAL_MS);
    timer.unref();
}
function stopCronStatusPolling() {
    if (timer) {
        clearInterval(timer);
        timer = null;
    }
}
//# sourceMappingURL=cron-status.js.map