import jfapi from '@/jfapi'
import type { Item } from '@/jfapi'

function getImageLink(item: Item): string {
  if (item.SeriesId === undefined) {
    // using movie primary
    return jfapi.PrimaryImageUrl(item.Id, item.ImageTags.Primary ? item.ImageTags.Primary : '')
  } else {
    // using series primary
    return jfapi.PrimaryImageUrl(
      item.SeriesId,
      item.SeriesPrimaryImageTag ? item.SeriesPrimaryImageTag[0] : '',
    )
  }
}

function ticksToMH(ticks: number): { hours: number; minutes: number } {
  const totalMinutes = Math.round(ticks / 600000000) || 1
  const totalHours = Math.floor(totalMinutes / 60)
  const remainderMinutes = totalMinutes % 60
  return {
    hours: totalHours,
    minutes: remainderMinutes,
  }
}

function getDisplayDuration(ticks: number) {
  const runtime = ticksToMH(ticks)
  const result = []
  if (runtime.hours > 0) {
    result.push(`${runtime.hours}h`)
  }
  result.push(`${runtime.minutes}m`)
  return result.join(' ')
}

export { getImageLink, getDisplayDuration, ticksToMH }
