package link.roadio.riff.ui import link.roadio.riff.indexer.core.media.MediaId import link.roadio.riff.player.storage.PlaybackQueueKind import link.roadio.riff.player.storage.PlaybackQueueSnapshot /** Immutable worker result: main only installs already prepared queue identity and current metadata. */ internal data class PreparedQueuePresentation( val mediaIds: List, val index: Int, val track: BrowserItem?, ) // AI-INTENT: A READY callback must not repeat a recursive source projection on the main looper. // Keep the captured cohort alive and reuse the manual command's ordered references when available. // https://wiki.271.pics/Riff/1-1-0/catalog-consumption#player-browse-representation internal fun prepareQueuePresentation( root: BrowserItem, availableTracks: List, queue: PlaybackQueueSnapshot, contextTracks: List? = null, ): PreparedQueuePresentation { val order = queue.orderedOccurrences() val index = order.indexOfFirst { it.occurrenceId == queue.checkpoint.occurrenceId }.coerceAtLeast(0) val occurrence = order.getOrNull(index) val contextual = occurrence?.takeIf { queue.kind != PlaybackQueueKind.PERMANENT_PLAYLIST }?.let { item -> if (contextTracks != null) { contextTracks.getOrNull(item.ordinal)?.takeIf { it.mediaId == item.mediaId } } else { queueTrackForOccurrence(root, queue.contextKey, item.ordinal, item.mediaId) } } val track = contextual ?: occurrence?.mediaId?.let { media -> root.catalog?.let { it.track(media) } ?: availableTracks.firstOrNull { it.mediaId == media } } return PreparedQueuePresentation(order.map { it.mediaId }, index, track) }