import type { Video } from "./api"; // What the player does with its queue after the item it is PLAYING is removed from the list it was // opened from. The player's launch queue is frozen at mount (see PlayerModal), so nothing external // can take the item out for it: leaving it in would let prev/next step back onto a video that no // longer exists, and unmounting the player instead would drop you out of the list you are triaging. // // Pure, because the decision is the whole of the behaviour: which item plays next, and when there is // nothing left to play. export interface AfterRemoval { /** The queue with the removed item gone. */ queue: Video[]; /** The item to play next. */ playingId: string; } /** `null` means nothing is left — the caller closes the player. * * The next item is the one that FOLLOWED the removed one (that is what "delete and keep going" * means when you are working down a list), clamped to the end when the last item goes. */ export function afterRemoval( queue: Video[] | undefined, index: number, victimId: string, ): AfterRemoval | null { const rest = (queue ?? []).filter((v) => v.id !== victimId); if (rest.length === 0) return null; const next = rest[Math.min(Math.max(index, 0), rest.length - 1)]!; return { queue: rest, playingId: next.id }; }