import { type ClassValue, clsx } from "clsx" import { twMerge } from "tailwind-merge" export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } const COMPACT_SUFFIXES = ['', 'K', 'M', 'B']; /** * Short number for dense cards and chart axes: 1K, 1.5K, 12K, 1.2M. * * One decimal below 10 and none above, so a metric tile never wraps. Values * under 1000 are printed in full. */ export function formatCompactNumber(value: number): string { if (!Number.isFinite(value)) return '0'; const abs = Math.abs(value); if (abs < 1000) return String(Math.round(value)); const tier = Math.min(COMPACT_SUFFIXES.length - 1, Math.floor(Math.log10(abs) / 3)); const scaled = value / 10 ** (tier * 3); const rounded = Math.abs(scaled) < 10 ? Math.round(scaled * 10) / 10 : Math.round(scaled); // Rounding can push a value up a tier (999,950 → 1000K), so re-check. if (Math.abs(rounded) >= 1000 && tier < COMPACT_SUFFIXES.length - 1) { return `${Math.round(rounded / 1000)}${COMPACT_SUFFIXES[tier + 1]}`; } return `${rounded}${COMPACT_SUFFIXES[tier]}`; } /** Reading result of `llm-analytics/v1/page-stats`. */ export interface PageStatsResponse { /** Rows the server returned. */ pages: T[]; /** Pages with AI traffic the server found. */ total: number; /** False when `total` hit the query ceiling and is really "at least". */ totalIsExact: boolean; } /** * Read `page-stats`, which returns an envelope but historically returned a * bare array. Accepting both keeps older cached bundles working. */ export function readPageStatsResponse(data: unknown): PageStatsResponse { if (Array.isArray(data)) { return { pages: data as T[], total: data.length, totalIsExact: true }; } if (!data || typeof data !== 'object') { return { pages: [], total: 0, totalIsExact: true }; } const envelope = data as Record; const pages = Array.isArray(envelope.pages) ? (envelope.pages as T[]) : []; const total = typeof envelope.total === 'number' ? envelope.total : pages.length; return { pages, total, totalIsExact: envelope.total_is_exact !== false, }; }