/**
 * Formats a byte count using the closest binary unit (B / KB / MB / GB / TB).
 * Returns "0 B" for zero or negative inputs so the UI never renders "NaN".
 */
export function formatBytes(bytes: number): string {
	if (!Number.isFinite(bytes) || bytes <= 0) return '0 B';
	if (bytes < 1024) return `${bytes} B`;
	const kb = bytes / 1024;
	if (kb < 1024) return `${kb.toFixed(0)} KB`;
	const mb = kb / 1024;
	if (mb < 1024) return `${mb.toFixed(1)} MB`;
	const gb = mb / 1024;
	if (gb < 1024) return `${gb.toFixed(2)} GB`;
	const tb = gb / 1024;
	return `${tb.toFixed(2)} TB`;
}
