import { ensureRtkSessionBaseline, formatRtkSavingsLabel, getRtkSessionSavings, } from "../shared/rtk-session-stats.js";
import { loadEffectiveNerfPreferences } from "./preferences.js";
const STATUS_KEY = "nerf-rtk";
const REFRESH_INTERVAL_MS = 30_000;
let refreshTimer = null;
function clearTimer() {
    if (refreshTimer) {
        clearInterval(refreshTimer);
        refreshTimer = null;
    }
}
function isRtkEnabledInPrefs() {
    return loadEffectiveNerfPreferences()?.preferences.experimental?.rtk === true;
}
function updateStatus(ctx) {
    if (!ctx.hasUI)
        return;
    if (!isRtkEnabledInPrefs())
        return;
    const basePath = ctx.cwd;
    const sessionId = ctx.sessionManager.getSessionId();
    ensureRtkSessionBaseline(basePath, sessionId);
    const savings = getRtkSessionSavings(basePath, sessionId);
    ctx.ui.setStatus(STATUS_KEY, formatRtkSavingsLabel(savings) ?? undefined);
}
export function startRtkStatusUpdates(ctx) {
    clearTimer();
    if (!isRtkEnabledInPrefs()) {
        // Ensure any previously set status is cleared (e.g. preference was toggled off)
        ctx.ui.setStatus(STATUS_KEY, undefined);
        return;
    }
    updateStatus(ctx);
    if (!ctx.hasUI)
        return;
    refreshTimer = setInterval(() => {
        updateStatus(ctx);
    }, REFRESH_INTERVAL_MS);
}
export function stopRtkStatusUpdates(ctx) {
    clearTimer();
    ctx?.ui.setStatus(STATUS_KEY, undefined);
}
