export const formatDuration = (microseconds?: number): string => {
  if (!microseconds) return 'N/A'
  if (microseconds < 1000) return `${microseconds.toFixed(0)}μs`
  if (microseconds < 1000000) return `${(microseconds / 1000).toFixed(1)}ms`
  return `${(microseconds / 1000000).toFixed(2)}s`
}

export const formatSize = (bytes?: number): string => {
  if (!bytes) return 'N/A'
  if (bytes < 1024) return `${bytes}B`
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`
  return `${(bytes / 1024 / 1024).toFixed(1)}MB`
}

export const formatDataRate = (transferSize?: number, contentLength?: number, durationMicroseconds?: number): string => {
  if (!durationMicroseconds || durationMicroseconds <= 0) return 'N/A'
  
  // Prefer content-length over transfer size for more accurate data rate calculation
  const bytes = contentLength && contentLength > 0 ? contentLength : transferSize
  if (!bytes) return 'N/A'
  
  // Convert duration from microseconds to seconds
  const durationSeconds = durationMicroseconds / 1000000
  
  // Calculate bytes per second
  const bytesPerSecond = bytes / durationSeconds
  
  if (bytesPerSecond < 1024) return `${bytesPerSecond.toFixed(0)} B/s`
  if (bytesPerSecond < 1024 * 1024) return `${(bytesPerSecond / 1024).toFixed(1)} KB/s`
  return `${(bytesPerSecond / 1024 / 1024).toFixed(1)} MB/s`
}