components_liveTv_LoadProgramDetailsTask.bs

import "pkg:/source/api/ApiClient.bs"
import "pkg:/source/api/apiPool.bs"
import "pkg:/source/data/JellyfinDataTransformer.bs"
import "pkg:/source/translationKeys.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.top.functionName = "loadProgramDetails"
end sub

' Orchestrator Task: submits to ApiTask pool and waits off the render thread.
sub loadProgramDetails()
  channelIndex = m.top.ChannelIndex
  programIndex = m.top.ProgramIndex

  res = fetchRes(GetApi().BuildGetLiveTvProgramRequest(m.top.programId, { UserId: m.global.user.id }), "liveTvProgram")
  if not isValid(res) or not res.ok or not isValid(res.json)
    m.top.programDetails = {}
    return
  end if

  transformer = JellyfinDataTransformer()
  node = transformer.transformBaseItem(res.json)

  ' Set hdSmallIconUrl for TimeGrid recording indicator (ContentNode built-in read by Roku TimeGrid).
  ' Must be set again here because ReplaceChild() in schedule.bs replaces the lightweight schedule
  ' item with this fully-loaded node, so the indicator must be preserved.
  if isValidAndNotEmpty(node.timerId)
    node.hdsmalliconurl = "pkg:/images/red.png"
  else
    node.hdsmalliconurl = ""
  end if

  ' Return as an AA wrapper so schedule.bs knows which grid cell to update without
  ' relying on mutable .channelIndex / .programIndex fields on the node itself
  m.top.programDetails = {
    item: node,
    channelIndex: channelIndex,
    programIndex: programIndex,
    isRecording: isValidAndNotEmpty(node.timerId)
  }
end sub