import { config } from '../../config/app.config'

type Entry = { count: number; resetAt: number }
const store = new Map<string, Entry>()

export function isRateLimited(ip: string): boolean {
  const now = Date.now()
  const entry = store.get(ip)

  if (!entry || now > entry.resetAt) {
    store.set(ip, { count: 1, resetAt: now + config.RATE_LIMIT_WINDOW_MS })
    return false
  }

  if (entry.count >= config.RATE_LIMIT_MAX) return true

  entry.count++
  return false
}
