import { photos as photosApi } from '../../services/api'
import type { Photo } from '../../types/photo'

const VIDEO_EXTENSIONS = ['.mp4', '.mov', '.webm', '.mkv', '.m4v']

export function isVideo(photo: Photo): boolean {
  if (photo.media_type === 'video') return true
  const lower = photo.filepath.toLowerCase()
  return VIDEO_EXTENSIONS.some((ext) => lower.endsWith(ext))
}

/**
 * Initial display URL for a still photo in the preview view: the 1280px
 * `large` WebP thumbnail. Cheap and always pre-generated, so the preview
 * appears instantly even for RAW/HEIC photos whose full-res /proxy would
 * otherwise transcode synchronously on first hit.
 *
 * Pair with `getPreviewFullResSrc` for progressive enhancement: load this
 * first, then preload the full-res in the background and swap it in.
 *
 * Videos go through `getVideoSrc` instead and use /original directly.
 */
export function getPreviewImageSrc(photo: Photo): string {
  return photosApi.getThumbnailUrl(photo.id, 'large')
}

/**
 * Full-resolution URL fetched in the background after the thumbnail is
 * already visible. The /proxy endpoint serves the original file for
 * web-safe formats (JPEG/PNG/WebP/GIF) or a transcoded full-res WebP for
 * RAW/HEIC/TIFF (cached on first hit). Used so Z-key / wheel zoom stays
 * pixel-sharp once the upgrade lands.
 */
export function getPreviewFullResSrc(photo: Photo): string {
  return photosApi.getProxyUrl(photo.id)
}

export function getVideoSrc(photo: Photo): string {
  // /playback transcodes HEVC (iPhone .mov) to H.264 on first hit and
  // caches it. /original would just hand the browser raw HEVC, which
  // Chrome and Firefox refuse to decode.
  return photosApi.getPlaybackUrl(photo.id)
}
