"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRestartDelay = getRestartDelay;
exports.recordRestart = recordRestart;
exports.resetRestartState = resetRestartState;
exports.shouldAutoRestart = shouldAutoRestart;
exports.getRestartStatus = getRestartStatus;
const BACKOFF_DELAYS = [1000, 2000, 4000, 8000, 16000, 30000];
const MAX_RESTARTS = 10;
const RESTART_WINDOW_MS = 120000;
let state = { count: 0, lastRestartAt: [], backoffIndex: 0 };
function getRestartDelay() {
    const now = Date.now();
    state.lastRestartAt = state.lastRestartAt.filter(t => now - t < RESTART_WINDOW_MS);
    state.count = state.lastRestartAt.length;
    if (state.count >= MAX_RESTARTS) {
        return -1;
    }
    const delay = BACKOFF_DELAYS[Math.min(state.backoffIndex, BACKOFF_DELAYS.length - 1)];
    state.backoffIndex++;
    return delay;
}
function recordRestart() {
    state.lastRestartAt.push(Date.now());
    state.count++;
}
function resetRestartState() {
    state = { count: 0, lastRestartAt: [], backoffIndex: 0 };
}
function shouldAutoRestart() {
    const now = Date.now();
    state.lastRestartAt = state.lastRestartAt.filter(t => now - t < RESTART_WINDOW_MS);
    return state.lastRestartAt.length < MAX_RESTARTS;
}
function getRestartStatus() {
    const now = Date.now();
    state.lastRestartAt = state.lastRestartAt.filter(t => now - t < RESTART_WINDOW_MS);
    return {
        recentRestarts: state.lastRestartAt.length,
        maxRestarts: MAX_RESTARTS,
        windowMs: RESTART_WINDOW_MS,
        backoffIndex: state.backoffIndex,
        currentDelay: BACKOFF_DELAYS[Math.min(state.backoffIndex, BACKOFF_DELAYS.length - 1)],
        canRestart: state.lastRestartAt.length < MAX_RESTARTS,
    };
}
//# sourceMappingURL=restart.js.map