import { DISCIPLINE_LABELS, RESEARCH_MODE_LABELS } from "@autoscholar/schemas"; export function fmtDate( d: string | Date | null | undefined, opts: Intl.DateTimeFormatOptions = { year: "numeric", month: "long", day: "numeric" }, ): string { if (!d) return "—"; return new Intl.DateTimeFormat("en-CA", { timeZone: "UTC", ...opts }).format(new Date(d)); } export function fmtDateTime(d: string | Date | null | undefined): string { if (!d) return "—"; return new Intl.DateTimeFormat("en-CA", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: false, }).format(new Date(d)); } export function ago(d: string | Date | null | undefined): string { if (!d) return "never"; const s = Math.max(0, (Date.now() - new Date(d).getTime()) / 1000); if (s < 60) return `${Math.round(s)}s ago`; if (s < 3600) return `${Math.round(s / 60)}m ago`; if (s < 86400) return `${Math.round(s / 3600)}h ago`; return `${Math.round(s / 86400)}d ago`; } export function fmtBytes(n: number): string { if (n < 1024) return `${n} B`; if (n < 1024 ** 2) return `${(n / 1024).toFixed(1)} KB`; if (n < 1024 ** 3) return `${(n / 1024 ** 2).toFixed(1)} MB`; return `${(n / 1024 ** 3).toFixed(2)} GB`; } export function fmtUsd(n: number | string | null | undefined): string { return `$${Number(n ?? 0).toFixed(2)}`; } export function disciplineLabel(d: string): string { return (DISCIPLINE_LABELS as Record)[d] ?? d; } export function modeLabel(m: string): string { return (RESEARCH_MODE_LABELS as Record)[m] ?? m; } export function shortHash(h: string | null | undefined, n = 12): string { return h ? h.slice(0, n) : "—"; }