/** Formatting shared by the server detail panels. One copy, because the rail
 *  and the storage panel must round the same bytes the same way. */

export function formatBytes(n: number): string {
    if (!n) return "0 B";
    const u = ["B", "KB", "MB", "GB", "TB"];
    const i = Math.floor(Math.log(n) / Math.log(1024));
    return `${(n / Math.pow(1024, i)).toFixed(1)} ${u[i]}`;
}

export function formatDate(dateStr: string) {
    return new Date(dateStr).toLocaleString();
}

export function relativeAge(iso?: string): string {
    if (!iso) return "never";
    const secs = Math.max(0, (Date.now() - new Date(iso).getTime()) / 1000);
    if (secs < 60) return `${Math.round(secs)}s ago`;
    if (secs < 3600) return `${Math.round(secs / 60)}m ago`;
    if (secs < 86_400) return `${Math.round(secs / 3600)}h ago`;
    return `${Math.round(secs / 86_400)}d ago`;
}
