source_utils_backdropUtils.bs

import "pkg:/source/api/Image.bs"
import "pkg:/source/utils/misc.bs"

' /**
'  * @function getBackdropUrl
'  * @description Centralized function to determine the appropriate backdrop URL for any Jellyfin item type.
'  *              Handles all media types: Movie, Series, Episode, MusicAlbum, Video, CollectionFolder, etc.
'  *              Uses device resolution for proper image sizing.
'  * @param {object} datum - The Jellyfin item data (BaseItemDto) from API
'  * @param {array} deviceResolution - Device UI resolution as [width, height] array
'  * @return {string} Backdrop URL or empty string if no backdrop available
'  */
function getBackdropUrl(datum as object, deviceResolution as object) as string
  if not isValid(datum) or not isValid(deviceResolution)
    return ""
  end if

  ' Validate datum.type exists before using it
  if not isValidAndNotEmpty(datum.type) then return ""

  backdropWidth = deviceResolution[0]
  backdropHeight = deviceResolution[1]
  itemType = LCase(datum.type)

  ' CollectionFolder or UserView
  if itemType = "collectionfolder" or itemType = "userview"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    end if

    ' Episode, Recording, MusicVideo, or Program - Use parent series backdrop or own backdrop
  else if itemType = "episode" or itemType = "recording" or itemType = "musicvideo" or itemType = "program"
    ' Try parent backdrop first (for series-based content)
    if isValid(datum.ParentBackdropImageTags) and isValidAndNotEmpty(datum.ParentBackdropImageTags[0]) and isValid(datum.ParentBackdropItemId)
      backdropParams = { "tag": datum.ParentBackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.ParentBackdropItemId, "Backdrop", backdropParams)
    end if
    ' Fallback to own backdrop (rare for LiveTV, but possible)
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.Id, "Backdrop", backdropParams)
    end if

    ' Series
  else if itemType = "series"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.Id, "Backdrop", backdropParams)
    end if

    ' Movie
  else if itemType = "movie"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    end if

    ' Video
  else if itemType = "video"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    end if

    ' MusicAlbum - Try album backdrop first, fallback to parent artist backdrop
  else if itemType = "musicalbum"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    else if isValid(datum.ParentBackdropImageTags) and isValidAndNotEmpty(datum.ParentBackdropImageTags[0]) and isValid(datum.ParentBackdropItemId)
      backdropParams = { "tag": datum.ParentBackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.ParentBackdropItemId, "Backdrop", backdropParams)
    end if

    ' MusicArtist - Use artist's own backdrop
  else if itemType = "musicartist"
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    end if

    ' Audio (Song) - Try song backdrop -> album backdrop -> artist backdrop
  else if itemType = "audio"
    ' Priority 1: Song's own backdrop (rare)
    if isValid(datum.BackdropImageTags) and isValidAndNotEmpty(datum.BackdropImageTags[0])
      backdropParams = { "tag": datum.BackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.id, "Backdrop", backdropParams)
    end if
    ' Priority 2: Album backdrop (via ParentBackdropImageTags - this is the album's backdrop for songs)
    if isValid(datum.ParentBackdropImageTags) and isValidAndNotEmpty(datum.ParentBackdropImageTags[0]) and isValid(datum.ParentBackdropItemId)
      backdropParams = { "tag": datum.ParentBackdropImageTags[0], "maxHeight": backdropHeight, "maxWidth": backdropWidth }
      return ImageURL(datum.ParentBackdropItemId, "Backdrop", backdropParams)
    end if

  end if

  ' Default: no backdrop available
  return ""
end function