components_liveTv_LoadProgramDetailsTask.bs

import "pkg:/source/api/baserequest.bs"
import "pkg:/source/data/JellyfinDataTransformer.bs"
import "pkg:/source/utils/config.bs"
import "pkg:/source/utils/misc.bs"

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

end sub

sub loadProgramDetails()

  channelIndex = m.top.ChannelIndex
  programIndex = m.top.ProgramIndex

  params = {
    UserId: m.global.user.id
  }

  url = Substitute("LiveTv/Programs/{0}", m.top.programId)

  resp = APIRequest(url, params)
  data = getJson(resp)

  if not isValid(data)
    m.top.programDetails = {}
    return
  end if

  transformer = JellyfinDataTransformer()
  node = transformer.transformBaseItem(data)
  ' 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