import { v as validateAuth } from './auth-DeCdZ83n.js';
import { randomBytes } from 'crypto';
import { j as json } from './shared-server-sSGG17Df.js';

const GLOBAL_KEY = "__shooter_ws_tickets";
const tickets = globalThis[GLOBAL_KEY] || /* @__PURE__ */ new Map();
globalThis[GLOBAL_KEY] = tickets;
function generateTicket() {
  const ticket = randomBytes(32).toString("hex");
  tickets.set(ticket, { createdAt: Date.now(), used: false });
  return ticket;
}
setInterval(() => {
  const now = Date.now();
  for (const [key, val] of tickets) {
    if (now - val.createdAt > 3e4) {
      tickets.delete(key);
    }
  }
}, 3e4).unref();
const RATE_LIMIT_WINDOW_MS = 6e4;
const RATE_LIMIT_MAX = 10;
const rateLimitMap = /* @__PURE__ */ new Map();
function checkRateLimit(apiKey) {
  const now = Date.now();
  const cutoff = now - RATE_LIMIT_WINDOW_MS;
  let timestamps = rateLimitMap.get(apiKey);
  if (!timestamps) {
    timestamps = [];
    rateLimitMap.set(apiKey, timestamps);
  }
  const recent = timestamps.filter((t) => t > cutoff);
  rateLimitMap.set(apiKey, recent);
  if (recent.length >= RATE_LIMIT_MAX) {
    return false;
  }
  recent.push(now);
  return true;
}
setInterval(() => {
  const cutoff = Date.now() - RATE_LIMIT_WINDOW_MS;
  for (const [key, timestamps] of rateLimitMap) {
    const recent = timestamps.filter((t) => t > cutoff);
    if (recent.length === 0) {
      rateLimitMap.delete(key);
    } else {
      rateLimitMap.set(key, recent);
    }
  }
}, 3e5).unref();
const POST = ({ request }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  const apiKey = request.headers.get("authorization").substring(7);
  if (!checkRateLimit(apiKey)) {
    return json(
      { error: "Rate limit exceeded. Maximum 10 ticket requests per minute." },
      { status: 429 }
    );
  }
  const ticket = generateTicket();
  return json({
    expiresIn: 30,
    ticket
  });
};

export { POST };
//# sourceMappingURL=_server.ts-COu0vNpd.js.map
