import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function formatBytes(bytes: number): string {
  if (bytes === 0) return "0 B";
  const k = 1024;
  const sizes = ["B", "KiB", "MiB", "GiB", "TiB"];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return `${(bytes / Math.pow(k, i)).toFixed(i === 0 ? 0 : 2)} ${sizes[i]}`;
}

export function formatRate(bytesPerSec: number): string {
  return `${formatBytes(bytesPerSec)}/s`;
}

export function shortenIpv6(addr: string): string {
  if (addr.length <= 24) return addr;
  return `${addr.slice(0, 10)}…${addr.slice(-8)}`;
}

// ─── base64 helpers (UTF-8 safe) ─────────────────────────────────────────────

export function utf8ToBase64(s: string): string {
  const bytes = new TextEncoder().encode(s);
  let bin = "";
  for (const b of bytes) bin += String.fromCharCode(b);
  return btoa(bin);
}

export function base64ToUtf8(b64: string): string {
  try {
    const bin = atob(b64);
    const bytes = new Uint8Array(bin.length);
    for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
    return new TextDecoder("utf-8", { fatal: false }).decode(bytes);
  } catch {
    return b64;
  }
}

export function isPrintableUtf8(b64: string): boolean {
  try {
    const bin = atob(b64);
    for (let i = 0; i < bin.length; i++) {
      const c = bin.charCodeAt(i);
      // Reject NUL and most C0 except CR/LF/TAB.
      if (c === 0 || (c < 0x20 && c !== 9 && c !== 10 && c !== 13)) return false;
    }
    return true;
  } catch {
    return false;
  }
}
