import type { MediaType } from '@/types/domain'

export const formatDate = (isoDate: string): string =>
  new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', year: 'numeric' }).format(
    new Date(isoDate),
  )

export const formatRating = (rating: number): string => rating.toFixed(1)

export const formatRuntime = (minutes: number, mediaType?: MediaType): string => {
  if (minutes <= 0) {
    return mediaType === 'game' ? 'TBD' : '0m'
  }

  if (mediaType === 'game') {
    const hours = minutes / 60
    return hours >= 10 ? `Est. ${Math.round(hours)}h` : `Est. ${hours.toFixed(1)}h`
  }

  const hours = Math.floor(minutes / 60)
  const remainder = minutes % 60
  return hours === 0 ? `${remainder}m` : `${hours}h ${remainder}m`
}
