import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
const VALID_STRATEGIES = ["memory", "redis"];
function readRateLimitConfig() {
  const strategy = process.env.RATE_LIMIT_STRATEGY ?? "memory";
  if (!VALID_STRATEGIES.includes(strategy)) {
    throw new Error(`Invalid RATE_LIMIT_STRATEGY "${strategy}". Must be one of: ${VALID_STRATEGIES.join(", ")}`);
  }
  const trustProxyDepth = parsePositiveInt(process.env.RATE_LIMIT_TRUST_PROXY_DEPTH) ?? 1;
  const integrationTest = parseBooleanWithDefault(process.env.OM_INTEGRATION_TEST, false);
  const enabled = integrationTest ? false : parseBooleanWithDefault(process.env.RATE_LIMIT_ENABLED, true);
  return {
    enabled,
    strategy,
    keyPrefix: process.env.RATE_LIMIT_KEY_PREFIX ?? "rl",
    redisUrl: process.env.REDIS_URL,
    trustProxyDepth
  };
}
function readEndpointRateLimitConfig(envPrefix, defaults) {
  return {
    points: parsePositiveInt(process.env[`RATE_LIMIT_${envPrefix}_POINTS`]) ?? defaults.points,
    duration: parsePositiveInt(process.env[`RATE_LIMIT_${envPrefix}_DURATION`]) ?? defaults.duration,
    blockDuration: parsePositiveInt(process.env[`RATE_LIMIT_${envPrefix}_BLOCK_DURATION`]) ?? defaults.blockDuration,
    keyPrefix: defaults.keyPrefix
  };
}
function parsePositiveInt(raw) {
  if (raw === void 0 || raw === "") return void 0;
  const parsed = Number(raw);
  return Number.isFinite(parsed) && parsed >= 0 ? parsed : void 0;
}
export {
  readEndpointRateLimitConfig,
  readRateLimitConfig
};
//# sourceMappingURL=config.js.map
