components_ItemGrid_MusicArtistGridItem.bs

import "pkg:/source/constants/imageSize.bs"
import "pkg:/source/utils/itemImageUrl.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.itemPoster = m.top.findNode("itemPoster")
  m.postTextBackground = m.top.findNode("postTextBackground")
  m.posterText = m.top.findNode("posterText")
  m.placeholder = m.top.findNode("placeholder")

  m.itemPoster.observeField("loadStatus", "onPosterLoadStatusChanged")

  'Parent is MarkupGrid and it's parent is the ItemGrid
  m.topParent = m.top.GetParent().GetParent()

  'Get the imageDisplayMode for these grid items
  if isValid(m.topParent.imageDisplayMode)
    m.itemPoster.loadDisplayMode = m.topParent.imageDisplayMode
  end if

  m.posterText.visible = false
  m.postTextBackground.visible = false
end sub

sub onItemContentChanged()
  m.posterText.visible = false
  m.postTextBackground.visible = false

  if isValid(m.topParent.showItemTitles)
    if LCase(m.topParent.showItemTitles) = "showalways"
      m.posterText.visible = true
      m.postTextBackground.visible = true
    end if
  end if

  itemData = m.top.itemContent

  if not isValid(itemData) then return

  m.posterText.text = itemData.title

  posterUrl = getItemPosterUrl(itemData)
  if posterUrl = ""
    ' Known-missing image — eagerly surface the typed glyph on the backdrop.
    ' Load-status never fires "failed" for an empty URI, so we must set the
    ' placeholder state here rather than waiting for onPosterLoadStatusChanged.
    showPlaceholder(musicItemType())
  else
    ' Image expected — start in loading state. The load-status observer flips
    ' to the typed-glyph failure state on "failed", or hides on "ready".
    m.placeholder.itemType = ""
    m.placeholder.visible = true
    m.itemPoster.uri = posterUrl
  end if

  if m.top.itemHasFocus then onFocusChanged()
end sub

'Display or hide title Visibility on focus change
sub onFocusChanged()
  if m.top.itemHasFocus = true
    m.posterText.repeatCount = -1
  else
    m.posterText.repeatCount = 0
  end if

  if isValid(m.topParent.showItemTitles)
    if LCase(m.topParent.showItemTitles) = "showonhover"
      m.posterText.visible = m.top.itemHasFocus
      m.postTextBackground.visible = m.posterText.visible
    end if
  end if
end sub

' Toggle the JRPlaceholder fallback based on the real poster's load state.
' Mirrors the canonical state machine from JRRowItem.bs onPosterLoadStatusChanged.
sub onPosterLoadStatusChanged()
  if m.itemPoster.loadStatus = "ready" and m.itemPoster.uri <> ""
    m.placeholder.visible = false
  else if m.itemPoster.loadStatus = "failed" and m.itemPoster.uri <> ""
    showPlaceholder(musicItemType())
  else
    m.placeholder.visible = true
  end if
end sub

' Resolve the Jellyfin item type string for the current cell. MusicGenre items
' arrive with type="MusicGenre" (typed by the transformer) or, in pre-typed
' contexts, with folderType="MusicGenre" — handle both.
function musicItemType() as string
  itemData = m.top.itemContent
  if not isValid(itemData) then return ""
  if isValidAndNotEmpty(itemData.type) then return itemData.type
  if isValidAndNotEmpty(itemData.folderType) then return itemData.folderType
  return ""
end function

' Flip the placeholder into failure state with a type-appropriate glyph, then
' clear the poster URI so the glyph isn't covered by a stale broken image.
sub showPlaceholder(itemType as string)
  m.placeholder.itemType = itemType
  m.placeholder.visible = true
  m.itemPoster.uri = ""
end sub