// engine/usage-tracker/rate-limiter.ts — Rate limiting logic (pure functions, no I/O).
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** Minimum milliseconds required between consecutive tool calls. */
export const COOLDOWN_MS = 2000;
// ---------------------------------------------------------------------------
// Daily limit check
// ---------------------------------------------------------------------------
/**
 * Checks whether the caller has exceeded the daily call limit.
 * Returns an allowed result when maxCalls is -1 (unlimited).
 */
export function checkDailyLimit(callCount, maxCalls) {
    if (maxCalls === -1) {
        return {
            allowed: true,
            reason: null,
            message: 'Unlimited calls available.',
            remainingCalls: -1,
        };
    }
    const remaining = maxCalls - callCount;
    if (remaining <= 0) {
        return {
            allowed: false,
            reason: 'daily_limit',
            message: `Daily call limit of ${String(maxCalls)} reached. Limit resets at midnight UTC.`,
            remainingCalls: 0,
        };
    }
    return {
        allowed: true,
        reason: null,
        message: `${String(remaining)} of ${String(maxCalls)} daily calls remaining.`,
        remainingCalls: remaining,
    };
}
// ---------------------------------------------------------------------------
// Cooldown check
// ---------------------------------------------------------------------------
/**
 * Checks whether enough time has passed since the last tool call.
 * Passes when lastCallAt is null (no previous call recorded).
 */
export function checkCooldown(lastCallAt, now) {
    if (lastCallAt === null) {
        return {
            allowed: true,
            reason: null,
            message: 'No previous call recorded.',
            remainingCalls: -1,
        };
    }
    const lastCallMs = new Date(lastCallAt).getTime();
    const elapsedMs = now.getTime() - lastCallMs;
    if (elapsedMs < COOLDOWN_MS) {
        const waitMs = COOLDOWN_MS - elapsedMs;
        return {
            allowed: false,
            reason: 'cooldown',
            message: `Rate limit: please wait ${String(waitMs)}ms before the next call.`,
            remainingCalls: -1,
        };
    }
    return {
        allowed: true,
        reason: null,
        message: 'Cooldown period elapsed.',
        remainingCalls: -1,
    };
}
//# sourceMappingURL=rate-limiter.js.map