/**
 * The public sponsorship intake's file rules, mirrored from
 * `SponsorController::store`:
 *
 *  - `sponsor_logo` — `ImageUploadRules::image()`: jpg/jpeg/png/webp, 10 MB;
 *  - `new_sponsor_documents` — at most 5, each pdf/jpg/jpeg/png/webp/doc/docx
 *    and 10 MB.
 *
 * Checked by extension: the browser reports no MIME type for .doc/.docx on
 * some systems, and the server re-checks the sniffed content anyway.
 */
export const SPONSOR_FILE_MAX_BYTES = 10 * 1024 * 1024;
export const SPONSOR_FILE_MAX_MB = 10;
export const SPONSOR_DOCUMENTS_MAX = 5;

export const SPONSOR_LOGO_EXTENSIONS = ["jpg", "jpeg", "png", "webp"] as const;
export const SPONSOR_DOCUMENT_EXTENSIONS = [
  "pdf", "jpg", "jpeg", "png", "webp", "doc", "docx",
] as const;

/** For the file inputs' `accept` attribute. */
export const SPONSOR_LOGO_ACCEPT = "image/jpeg,image/png,image/webp";
export const SPONSOR_DOCUMENT_ACCEPT = SPONSOR_DOCUMENT_EXTENSIONS.map((ext) => `.${ext}`).join(",");

const extensionOf = (name: string) => name.split(".").pop()?.toLowerCase() ?? "";

export function hasAllowedExtension(file: { name: string }, allowed: readonly string[]): boolean {
  return allowed.includes(extensionOf(file.name));
}

export function isWithinSponsorFileSize(file: { size: number }): boolean {
  return file.size > 0 && file.size <= SPONSOR_FILE_MAX_BYTES;
}
