"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RedisRateLimitStore = void 0;
class RedisRateLimitStore {
    client;
    windowMs;
    prefix;
    constructor(client, windowMs, prefix = 'rl:') {
        this.client = client;
        this.windowMs = windowMs;
        this.prefix = prefix;
    }
    key(key) {
        return `${this.prefix}${key}`;
    }
    async increment(key) {
        const redisKey = this.key(key);
        const [hits] = (await this.client
            .multi()
            .incr(redisKey)
            .pexpire(redisKey, this.windowMs)
            .exec());
        const totalHits = hits[1];
        const ttl = await this.client.pttl(redisKey);
        const resetTime = new Date(Date.now() + Math.max(ttl, 0));
        return { totalHits, resetTime };
    }
    async decrement(key) {
        const redisKey = this.key(key);
        const current = await this.client.decr(redisKey);
        if (current < 0) {
            await this.client.del(redisKey);
        }
    }
    async resetKey(key) {
        await this.client.del(this.key(key));
    }
}
exports.RedisRateLimitStore = RedisRateLimitStore;
//# sourceMappingURL=redis-rate-limit-store.js.map