import type { PolyModel } from "@/lib/ai/core/types"; /** "$0.25", "$10", "—" — USD per 1M tokens. Server- and client-safe. */ export function formatPrice(v: number | null | undefined): string { if (v === undefined || v === null || Number.isNaN(v)) return "—"; if (v === 0) return "$0"; if (v < 0.01) return `$${v.toFixed(4).replace(/0+$/, "")}`; if (v < 1) return `$${v.toFixed(2)}`; return `$${Number.isInteger(v) ? v : v.toFixed(2).replace(/\.?0+$/, "")}`; } /** "$2 / $10" input / output per 1M. */ export function formatPricePair(m: PolyModel): string { const p = m.pricing; if (!p || (p.inputPerMillion === undefined && p.outputPerMillion === undefined)) return "—"; return `${formatPrice(p.inputPerMillion)} / ${formatPrice(p.outputPerMillion)}`; } export function formatContext(n: number | null | undefined): string { if (!n) return "—"; if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2).replace(/\.?0+$/, "")}M`; if (n >= 1_000) return `${(n / 1_000).toFixed(1).replace(/\.?0+$/, "")}K`; return String(n); } /** ISO date → "Sep 8, 2026" (UTC, deterministic for SSR). */ export function formatIsoDate(v: unknown): string { if (typeof v !== "string" || !v) return "—"; const d = new Date(v); if (Number.isNaN(d.getTime())) return v; return d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", timeZone: "UTC" }); }