/**
 * Community image rules (PDF p.26 / p.42): JPG, PNG or WEBP, 10 MB each —
 * the same as `CommunityImages` + `ImageUploadRules` on the server, which also
 * caps one request at `MAX_FILES` = 10 new files. Posts and replies share them.
 */
export const COMMUNITY_IMAGE_TYPES = ["image/jpeg", "image/png", "image/webp"] as const;
export const COMMUNITY_IMAGE_ACCEPT = COMMUNITY_IMAGE_TYPES.join(",");
export const COMMUNITY_IMAGE_MAX_BYTES = 10 * 1024 * 1024;
export const COMMUNITY_IMAGE_MAX_FILES = 10;

/** The i18n key of the first rule the picked files break, or null when they pass. */
export function postImagesError(
  files: readonly (File | null | undefined)[] | null | undefined
): string | null {
  const picked = (files ?? []).filter((file): file is File => Boolean(file));
  if (picked.length > COMMUNITY_IMAGE_MAX_FILES) return "COMMON.POST_IMAGES_MAX";
  const invalid = picked.some(
    (file) =>
      !(COMMUNITY_IMAGE_TYPES as readonly string[]).includes(file.type) ||
      file.size > COMMUNITY_IMAGE_MAX_BYTES
  );
  return invalid ? "COMMON.REVIEW_IMAGE_LIMIT" : null;
}
