// Simple in-memory rate limiting
const submissions: Record<string, number> = {};
const RATE_LIMIT_WINDOW = 60 * 60 * 1000; // 1 hour
const MAX_SUBMISSIONS_PER_WINDOW = 3;

export async function rateLimit(
  identifier: string,
  windowMs = RATE_LIMIT_WINDOW,
  maxSubmissions = MAX_SUBMISSIONS_PER_WINDOW,
) {
  const now = Date.now();

  // Clean up old submissions
  Object.keys(submissions).forEach((key) => {
    if (now - submissions[key] > windowMs) {
      delete submissions[key];
    }
  });

  // Check if identifier has exceeded submission limit
  const currentSubmissions = Object.values(submissions).filter(
    (timestamp) => now - timestamp <= windowMs,
  );

  if (currentSubmissions.length >= maxSubmissions) {
    throw new Error("Too many submissions. Please try again later.");
  }

  // Record this submission
  submissions[identifier] = now;
}

export * from "./lang";

export * from "./i18n";
export * from "./env";
export * from "./directus";
