const UNITS = ["o", "Ko", "Mo", "Go", "To"] as const

export function formatBytes(bytes: number, decimals = 1): string {
  if (!Number.isFinite(bytes) || bytes < 0) return "—"
  if (bytes === 0) return "0 o"
  const k = 1024
  const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), UNITS.length - 1)
  const value = bytes / Math.pow(k, i)
  return `${value.toFixed(decimals)} ${UNITS[i]}`
}

export function gibToBytes(gib: number): number {
  return Math.round(gib * 1024 * 1024 * 1024)
}

export function bytesToGib(bytes: number): number {
  return bytes / (1024 * 1024 * 1024)
}
