const DEFAULT_ATTACHMENT_MAX_UPLOAD_MB = 25;
const DEFAULT_ATTACHMENT_TENANT_QUOTA_MB = 512;
const MULTIPART_CONTENT_LENGTH_OVERHEAD_BYTES = 1024 * 1024;
const ATTACHMENT_MAX_UPLOAD_MB_ENV_KEYS = [
  "OM_ATTACHMENT_MAX_UPLOAD_MB",
  "OPENMERCATO_ATTACHMENT_MAX_UPLOAD_MB"
];
const ATTACHMENT_TENANT_QUOTA_MB_ENV_KEYS = [
  "OM_ATTACHMENT_TENANT_QUOTA_MB",
  "OPENMERCATO_ATTACHMENT_TENANT_QUOTA_MB"
];
function readEnvValue(names) {
  return names.map((name) => process.env[name]).find((value) => typeof value === "string");
}
function parseMegabytesEnv(names, fallbackMb) {
  const raw = readEnvValue(names);
  const parsed = raw ? Number(raw) : NaN;
  if (Number.isFinite(parsed) && parsed > 0) {
    return parsed;
  }
  return fallbackMb;
}
function resolveDefaultAttachmentMaxUploadBytes() {
  return Math.floor(parseMegabytesEnv(ATTACHMENT_MAX_UPLOAD_MB_ENV_KEYS, DEFAULT_ATTACHMENT_MAX_UPLOAD_MB) * 1024 * 1024);
}
function resolveAttachmentTenantQuotaBytes() {
  return Math.floor(parseMegabytesEnv(ATTACHMENT_TENANT_QUOTA_MB_ENV_KEYS, DEFAULT_ATTACHMENT_TENANT_QUOTA_MB) * 1024 * 1024);
}
function resolveAttachmentMaxBytes(fieldMaxAttachmentSizeMb) {
  const defaultMaxBytes = resolveDefaultAttachmentMaxUploadBytes();
  if (typeof fieldMaxAttachmentSizeMb !== "number" || fieldMaxAttachmentSizeMb <= 0) {
    return defaultMaxBytes;
  }
  return Math.min(defaultMaxBytes, Math.floor(fieldMaxAttachmentSizeMb * 1024 * 1024));
}
function isMultipartRequestWithinUploadLimit(contentLengthHeader) {
  if (!contentLengthHeader) return true;
  const contentLength = Number(contentLengthHeader);
  if (!Number.isFinite(contentLength) || contentLength <= 0) return true;
  return contentLength <= resolveDefaultAttachmentMaxUploadBytes() + MULTIPART_CONTENT_LENGTH_OVERHEAD_BYTES;
}
function willExceedAttachmentTenantQuota(currentUsageBytes, incomingFileBytes) {
  return currentUsageBytes + incomingFileBytes > resolveAttachmentTenantQuotaBytes();
}
export {
  isMultipartRequestWithinUploadLimit,
  resolveAttachmentMaxBytes,
  resolveAttachmentTenantQuotaBytes,
  resolveDefaultAttachmentMaxUploadBytes,
  willExceedAttachmentTenantQuota
};
//# sourceMappingURL=upload-limits.js.map
