/**
 * 摄像头流媒体工具函数
 */

/**
 * 构建完整的流媒体播放 URL
 * @param {string} relativePath - 后端返回的相对路径，如 "/live/CAM-001/hls.m3u8"
 * @returns {string} 完整的播放 URL
 */
export function buildStreamUrl(relativePath) {
  if (!relativePath) return ''
  
  const protocol = window.location.protocol // "http:" or "https:"
  const host = window.location.host         // "example.com:8080"
  
  // 确保相对路径以 / 开头
  const path = relativePath.startsWith('/') ? relativePath : '/' + relativePath
  
  return `${protocol}//${host}${path}`
}

/**
 * 从后端返回的 playUrls 对象构建完整的播放 URL 映射
 * @param {Object} playUrls - 后端返回的 { hls: "/live/...", flv: "/live/...", wsFlv: "/live/..." }
 * @returns {Object} 完整的 URL 映射
 */
export function buildFullPlayUrls(playUrls) {
  if (!playUrls) return {}
  
  const protocol = window.location.protocol
  const host = window.location.host
  
  const result = {}
  
  // HLS 和 HTTP-FLV
  if (playUrls.hls) {
    result.hls = buildStreamUrl(playUrls.hls)
  }
  if (playUrls.flv) {
    result.flv = buildStreamUrl(playUrls.flv)
  }
  
  // WebSocket-FLV（需要转换协议）
  if (playUrls.wsFlv) {
    const wsProtocol = protocol === 'https:' ? 'wss:' : 'ws:'
    const path = playUrls.wsFlv.startsWith('/') ? playUrls.wsFlv : '/' + playUrls.wsFlv
    result.wsFlv = `${wsProtocol}//${host}${path}`
  }
  
  return result
}