/**
 * Thumbnail helpers. A media "preview" thumbnail is either the explicit image URL
 * yt-dlp handed us at probe time, or — for a YouTube video — one derived from the
 * video id with no extra network probe. Anything non-YouTube without a probed
 * thumbnail returns undefined, and the UI falls back to a kind icon.
 */
import { youtubeId } from './urlHelpers'

/**
 * Resolve a preview thumbnail URL for a media item. Prefers an explicit thumbnail
 * (from a probe), then a known videoId, then a YouTube URL it can derive an id
 * from. Returns undefined when nothing usable is available.
 */
export function thumbUrl(opts: {
  thumbnail?: string
  videoId?: string
  url?: string
}): string | undefined {
  if (opts.thumbnail) return opts.thumbnail
  const id = opts.videoId || youtubeId(opts.url)
  // mqdefault is 320×180 (16:9, no letterbox bars) and exists for every public
  // video — a better fit for our 16:9 thumb boxes than the 4:3 hqdefault.
  return id ? `https://i.ytimg.com/vi/${encodeURIComponent(id)}/mqdefault.jpg` : undefined
}
