"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOGIN_RATE_LIMIT_MESSAGE = void 0;
exports.createLoginRateLimiter = createLoginRateLimiter;
exports.LOGIN_RATE_LIMIT_MESSAGE = 'Too many login attempts from this IP, please try again later';
function createLoginRateLimiter(windowMs, max) {
    const entries = new Map();
    const cleanup = setInterval(() => {
        const now = Date.now();
        for (const [ip, entry] of entries) {
            if (now >= entry.resetTime) {
                entries.delete(ip);
            }
        }
    }, windowMs);
    cleanup.unref();
    return {
        check(ip) {
            const now = Date.now();
            let entry = entries.get(ip);
            if (!entry || now >= entry.resetTime) {
                entry = { count: 0, resetTime: now + windowMs };
                entries.set(ip, entry);
            }
            entry.count++;
            if (entry.count > max) {
                return { allowed: false, retryAfterMs: entry.resetTime - now };
            }
            return { allowed: true, retryAfterMs: 0 };
        },
        dispose() {
            clearInterval(cleanup);
            entries.clear();
        }
    };
}
//# sourceMappingURL=login-rate-limiter.js.map