/**
 * Utility functions for video URL handling with HLS support and fallback to MP4
 */

export type TranscodingStatus = 'uploaded' | 'processing' | 'transcoded' | 'failed';

export interface VideoUrls {
  hlsUrl: string | null;
  mp4Url: string;
}

/**
 * Generate video URLs based on transcoding status
 * @param videoId - The video ID
 * @param mp4Url - The original MP4 URL
 * @param transcodingStatus - The current transcoding status
 * @returns Object containing HLS and MP4 URLs
 */
export function getVideoUrls(
  videoId: string,
  mp4Url: string,
  transcodingStatus: TranscodingStatus
): VideoUrls {
  // HLS is only available if transcoding is complete
  const hlsUrl = transcodingStatus === 'transcoded'
    ? `/api/videos/hls/${videoId}/master.m3u8`
    : null;

  return {
    hlsUrl,
    mp4Url,
  };
}

/**
 * Get the appropriate video source URL based on availability
 * Prefers HLS if available, falls back to MP4
 * @param videoId - The video ID
 * @param mp4Url - The original MP4 URL
 * @param transcodingStatus - The current transcoding status
 * @returns The primary URL to use
 */
export function getPrimaryVideoUrl(
  videoId: string,
  mp4Url: string,
  transcodingStatus: TranscodingStatus
): string {
  const { hlsUrl } = getVideoUrls(videoId, mp4Url, transcodingStatus);
  return hlsUrl || mp4Url;
}
