import {
  clampText,
  isOnlineFormat,
  type OpportunityCardItem,
} from "./opportunityCardTypes";

/**
 * What the card prints on its location row.
 *
 * Three cases, in order:
 *
 *  1. an **online** opportunity has no place to show, so it prints the format;
 *  2. a **development** opportunity prints "In person" rather than its venue —
 *    the list cards deliberately do not expose where a course is held;
 *  3. otherwise the venue (or the map pin's label), **clamped to 12 characters**, because a long name
 *    pushes the rest of the row off a phone-width card instead of wrapping.
 */
export function opportunityCardLocation({
  item,
  isArabic,
  isLearnServe,
  t,
}: {
  item: OpportunityCardItem;
  isArabic: boolean;
  isLearnServe?: boolean;
  t: (key: string) => string;
}): string {
  if (isOnlineFormat(item)) return t("COMMON.ONLINE");
  if (isLearnServe) return t("COMMON.IN_PERSON");
  /* The forms send only `map_desc` now, so `location_en/ar` are empty on every
     new opportunity — the card used to read «Loading location...» for good.
     Nothing to show leaves the row empty rather than claiming it is loading. */
  const raw =
    (isArabic ? item.location_ar : item.location_en) ||
    (isArabic ? item.location_en : item.location_ar) ||
    item.map_desc ||
    "";
  return raw ? clampText(String(raw), 12) : "";
}
