import type { EventRecord } from "./eventRecord";

/**
 * The card's picture: the event's card image (`cover_image`, PDF p.22
 * «صورة البطاقة»), then the first gallery photo for an old event saved without
 * one. Cards used to read the gallery only, so replacing the card image on an
 * edit that kept older photos changed nothing on the card.
 */
export function eventCardImage<T extends { image?: string | null }>(item: { event_images?: T[] | null; cover_image?: string | null }): string | null {
  return item.cover_image || item.event_images?.find((image) => image.image)?.image || null;
}

/**
 * The card's location line: the pin label (`map_desc`, which the resource
 * already falls back to the old text columns). An event published since the
 * text location was removed (PDF p.22) has only a location link, so this is
 * empty and the row is hidden — it used to print «Loading location...» for
 * good.
 */
export function eventCardLocation(item: Pick<EventRecord, "map_desc" | "location_en" | "location_ar">, language: string): string {
  return (item.map_desc || (language === "ar" ? item.location_ar || item.location_en : item.location_en || item.location_ar) || "").trim();
}

/** Event artwork belongs to the card, not to the documentation gallery. */
export function eventGalleryImages(item: Pick<EventRecord, "event_images" | "cover_image">) {
  const cover = eventCardImage(item);
  return (item.event_images ?? []).filter(image => image.image && image.image !== cover);
}
