"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkMessageRateLimit = exports.messageRateLimiter = void 0;
class RateLimiter {
    limits = new Map();
    maxMessages;
    windowMs;
    constructor(maxMessages = 10, windowMs = 60000) {
        this.maxMessages = maxMessages;
        this.windowMs = windowMs;
    }
    checkLimit(userId) {
        const now = Date.now();
        const userLimit = this.limits.get(userId) ?? { count: 0, resetTime: now + this.windowMs };
        if (now > userLimit.resetTime) {
            userLimit.count = 0;
            userLimit.resetTime = now + this.windowMs;
        }
        userLimit.count++;
        this.limits.set(userId, userLimit);
        return userLimit.count <= this.maxMessages;
    }
    getRemainingTime(userId) {
        const userLimit = this.limits.get(userId);
        if (!userLimit)
            return 0;
        return Math.max(0, userLimit.resetTime - Date.now());
    }
}
exports.messageRateLimiter = new RateLimiter(10, 60000); // 10 messages per minute
const checkMessageRateLimit = (socket) => {
    const canSend = exports.messageRateLimiter.checkLimit(socket.userId);
    if (!canSend) {
        const remainingTime = exports.messageRateLimiter.getRemainingTime(socket.userId);
        socket.emit("rate_limit_exceeded", {
            message: `Too many messages. Please wait ${Math.ceil(remainingTime / 1000)} seconds before sending another message.`,
            remainingTime,
        });
    }
    return canSend;
};
exports.checkMessageRateLimit = checkMessageRateLimit;
