"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimiter = void 0;
const logger_1 = require("./logger");
class RateLimiter {
    tokens;
    maxTokens;
    refillRate;
    lastRefill;
    now;
    constructor(maxTokens = 10, refillPerSecond = 2, now) {
        this.tokens = maxTokens;
        this.maxTokens = maxTokens;
        this.refillRate = refillPerSecond;
        this.now = now ?? Date.now;
        this.lastRefill = this.now();
    }
    tryConsume() {
        this.refill();
        if (this.tokens >= 1) {
            this.tokens -= 1;
            return true;
        }
        logger_1.logger.warn('RateLimiter', 'Rate limit exceeded', {
            tokens: this.tokens,
            maxTokens: this.maxTokens,
        });
        return false;
    }
    refill() {
        const now = this.now();
        const elapsed = (now - this.lastRefill) / 1000;
        this.tokens = Math.min(this.maxTokens, this.tokens + elapsed * this.refillRate);
        this.lastRefill = now;
    }
}
exports.RateLimiter = RateLimiter;
//# sourceMappingURL=rateLimiter.js.map