package com.ponzischeme89.memby.data

import com.ponzischeme89.memby.data.model.BaseItem
import com.ponzischeme89.memby.data.model.EmbyPerson

/**
 * Loads the cast Emby associates with an item.
 *
 * Episodes commonly omit `People`, while their parent series holds the canonical cast.
 * Keep episode-specific credits when Emby supplies them, and only pay for the series
 * request when the episode has no cast of its own.
 */
internal suspend fun resolveCast(
    itemId: String,
    loadItem: suspend (String) -> BaseItem,
): List<EmbyPerson> {
    val item = loadItem(itemId)
    if (item.cast.isNotEmpty()) return item.cast

    val seriesId = item.seriesId
        ?.takeIf(String::isNotBlank)
        ?.takeUnless { it == item.id }
    return if (item.isEpisode && seriesId != null) loadItem(seriesId).cast else emptyList()
}
