export function detectSourceType(source) {
  const extension = source.split(".").pop().toLowerCase();
  const typeMap = {
    mp4: "video/mp4",
    webm: "video/webm",
    ogv: "video/ogg",
    // Audio types
    mp3: "audio/mpeg",
    m4a: "audio/mp4",
    aac: "audio/aac",
    wav: "audio/wav",
    oga: "audio/ogg",
    ogg: "audio/ogg",
    // Encrypted Media Extensions
    m3u8: "application/x-mpegURL",
    mpd: "application/dash+xml",
  };

  return typeMap[extension] || "video/mp4";
}

export function isAudioMime(type) {
  return typeof type === "string" && type.startsWith("audio/");
}

// Helper function to format time
export function formatTime(seconds) {
  if (isNaN(seconds) || seconds < 0) {
    seconds = 0;
  }
  const date = new Date(null);
  date.setSeconds(seconds);
  const result = date.toISOString().substr(11, 8);
  return result.startsWith("00:") ? result.substr(3) : result;
}

export function formatTimeDisplay(seconds) {
  if (isNaN(seconds) || !isFinite(seconds)) {
    return '00:00';
  }
  
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const secs = Math.floor(seconds % 60);
  
  if (hours > 0) {
    return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  } else {
    return `${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
  }
}