/**
 * An already-stored image, as the detail payloads carry it.
 *
 * Three shapes, one per family, kept separate because the difference matters:
 *
 * - **Community** (`post_images`, `reply_images`): `{ id, image }`. The
 *   resource calls `getimg($image->image)` without its own null guard, but
 *   `getimg()` guards internally (`if (empty($filename)) return null`), so the
 *   field is nullable just like the other two — an earlier revision of this
 *   comment claimed otherwise. The payload shape lives in
 *   `features/community/communityRecord.ts`; what remains here is the
 *   *picker's* shape, below.
 * - **Opportunities** (`opportunity_images` via `opportunityImagesPayload`):
 *   adds `is_after_completed`, which is the whole reason the edit forms filter
 *   this list. A completion photo belongs to the gallery, not to the
 *   announcement picker, so an edit form that failed to filter it would offer
 *   the organizer their own after-the-fact photos as the opportunity's cover.
 * - **Events** (`event_images`): `{ id, image }` with `image` nullable, modelled
 *   as `EventImageRecord` in `features/events/eventRecord.ts`.
 *
 * Eight call sites annotated these `(img: any)`, which is how the
 * `is_after_completed` filter could have been dropped without the compiler
 * noticing.
 */

/**
 * An image a picker can actually preview: `image` is non-null **by
 * construction**, because `announcementImages()` drops the rows where it is
 * null rather than handing the picker something with no `src`.
 *
 * This is not the payload shape — `post_images[].image` and
 * `opportunity_images[].image` are both nullable. It used to be called
 * `CommunityImageRecord`, which read as "the community payload's image" and
 * said `image: string`; that conflated a filtered output with a response
 * contract. The payload types are `CommunityImageRecord` in
 * `features/community/communityRecord.ts` and `OpportunityImageRecord` below.
 */
export interface PickerImage {
  id: number;
  image: string;
}

/** `{ id, image, is_after_completed }` — volunteer and learn-&-serve. */
export interface OpportunityImageRecord {
  id: number;
  image: string | null;
  /**
   * True for a photo uploaded *after* the opportunity finished.
   *
   * The edit pickers must show only `false` rows: the completion gallery and
   * the announcement images share one array on the backend.
   */
  is_after_completed: boolean;
}

/**
 * The announcement images an edit form may offer, in picker order.
 *
 * Two filters, both deliberate:
 *
 * - `is_after_completed === false` keeps completion-gallery photos out of the
 *   announcement picker. Strict `=== false` rather than falsy, matching the two
 *   call sites this replaces: the resource casts the column with `(bool)`, so
 *   the key is always a real boolean and `undefined` would mean the payload is
 *   not this shape at all.
 * - a row whose `image` is null is dropped. `opportunityImagesPayload` emits
 *   `null` when the column is empty, and the picker has nothing to preview for
 *   it — passing it through gave an `<img>` with no `src`, which the browser
 *   resolves against the current URL and draws as a broken image. The two
 *   call sites declared this field `string` while it is nullable, so the
 *   compiler could not see it.
 */
export function announcementImages(
  images: OpportunityImageRecord[] | null | undefined
): PickerImage[] {
  const kept: PickerImage[] = [];
  for (const image of images ?? []) {
    if (image.is_after_completed !== false) continue;
    if (image.image === null) continue;
    kept.push({ id: image.id, image: image.image });
  }
  return kept;
}
