import { ErrorHandler } from "./errors/ErrorHandler.mjs";
import rateLimit, { ipKeyGenerator } from "express-rate-limit";

//#region src/utils/rateLimiter.ts
const ipLimiterOptions = {
	windowMs: 60 * 1e3,
	limit: 500,
	standardHeaders: "draft-8",
	legacyHeaders: false,
	keyGenerator: (req) => {
		return ipKeyGenerator(req.ip ?? req.socket?.remoteAddress ?? "unknown");
	},
	handler: (req, res) => {
		const { limit, remaining, resetTime } = req.rateLimit;
		ErrorHandler.handleGenericErrorResponse(res, "RATE_LIMIT_EXCEEDED", {
			limit: `${limit} per minute`,
			retryAfter: Math.ceil((resetTime?.getTime() - Date.now()) / 1e3),
			remaining
		});
	}
};
const ipLimiter = rateLimit(ipLimiterOptions);
const unauthenticatedChatBotLimiterOptions = {
	windowMs: 3600 * 1e3,
	limit: 3,
	standardHeaders: "draft-8",
	skip: (_req, res) => Boolean(res.locals.user),
	legacyHeaders: false,
	keyGenerator: (req) => {
		return ipKeyGenerator(req.ip ?? req.socket?.remoteAddress ?? "unknown");
	},
	handler: (req, res) => {
		const { limit, remaining, resetTime } = req.rateLimit;
		ErrorHandler.handleGenericErrorResponse(res, "RATE_LIMIT_EXCEEDED_UNAUTHENTICATED", {
			limit: `${limit} per hour`,
			retryAfter: Math.ceil((resetTime?.getTime() - Date.now()) / 1e3),
			remaining
		});
	}
};
const unauthenticatedChatBotLimiter = rateLimit(unauthenticatedChatBotLimiterOptions);

//#endregion
export { ipLimiter, unauthenticatedChatBotLimiter };
//# sourceMappingURL=rateLimiter.mjs.map