export function createRateLimiter(max = 120, windowMs = 60_000) {
	const buckets = new Map<string, { count: number; resetAt: number }>();

	return {
		allow(key: string): boolean {
			const now = Date.now();
			const current = buckets.get(key);
			if (!current || now >= current.resetAt) {
				buckets.set(key, { count: 1, resetAt: now + windowMs });
				return true;
			}
			current.count += 1;
			return current.count <= max;
		},
	};
}

export type RateLimiter = ReturnType<typeof createRateLimiter>;
