/**
 * Formatea un número en formato compacto (1K, 1.2M, etc.)
 * Soporta sufijos personalizados.
 */
export function formatCompact(n: number, decimals: number = 1): string {
  if (Math.abs(n) >= 1_000_000_000) return (n / 1_000_000_000).toFixed(decimals) + 'B'
  if (Math.abs(n) >= 1_000_000) return (n / 1_000_000).toFixed(decimals) + 'M'
  if (Math.abs(n) >= 1_000) return (n / 1_000).toFixed(decimals) + 'K'
  return n.toString()
}

/**
 * Formatea frecuencia en Hz/KHz/MHz/GHz.
 */
export function formatHz(hz: number, decimals: number = 1): string {
  if (hz >= 1_000_000_000) return (hz / 1_000_000_000).toFixed(decimals) + ' GHz'
  if (hz >= 1_000_000) return (hz / 1_000_000).toFixed(decimals) + ' MHz'
  if (hz >= 1_000) return (hz / 1_000).toFixed(decimals) + ' KHz'
  return hz + ' Hz'
}

/**
 * Formatea bytes en KB/MB/GB/TB.
 */
export function formatBytes(bytes: number, decimals: number = 1): string {
  if (bytes >= 1_099_511_627_776) return (bytes / 1_099_511_627_776).toFixed(decimals) + ' TB'
  if (bytes >= 1_073_741_824) return (bytes / 1_073_741_824).toFixed(decimals) + ' GB'
  if (bytes >= 1_048_576) return (bytes / 1_048_576).toFixed(decimals) + ' MB'
  if (bytes >= 1_024) return (bytes / 1_024).toFixed(decimals) + ' KB'
  return bytes + ' B'
}

/**
 * Formatea duración en ms/s/min/h.
 */
export function formatDuration(ms: number): string {
  if (ms >= 3_600_000) return (ms / 3_600_000).toFixed(1) + 'h'
  if (ms >= 60_000) return (ms / 60_000).toFixed(1) + 'min'
  if (ms >= 1_000) return (ms / 1_000).toFixed(1) + 's'
  if (ms >= 1) return ms.toFixed(0) + 'ms'
  return (ms * 1000).toFixed(0) + 'µs'
}
