import { clsx, type ClassValue } from 'clsx'; import { twMerge } from 'tailwind-merge'; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } export function debounce any>(fn: T, delay: number) { let timeoutId: NodeJS.Timeout; return (...args: Parameters) => { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn(...args), delay); }; } export function throttle any>(fn: T, limit: number) { let inThrottle: boolean; return (...args: Parameters) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => { inThrottle = false; }, limit); } }; } export function formatRelativeTime(dateStr: string): string { const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000); if (seconds < 60) return 'just now'; if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`; return `${Math.floor(seconds / 86400)}d ago`; } export function getFileIcon(filename: string): string { const ext = filename.split('.').pop()?.toLowerCase(); const icons: Record = { js: '📄', ts: '📘', py: '🐍', cpp: '⚙️', java: '☕', html: '🌐', css: '🎨', json: '📋', md: '📝' }; return icons[ext || ''] || '📄'; } export async function copyToClipboard(text: string): Promise { try { await navigator.clipboard.writeText(text); return true; } catch { return false; } } export function generateInviteLink(inviteCode: string): string { const baseUrl = typeof window !== 'undefined' ? window.location.origin : ''; return `${baseUrl}/join/${inviteCode}`; } export function formatDuration(ms: number): string { if (ms < 1000) return `${ms}ms`; if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`; return `${Math.floor(ms / 60000)}m ${Math.floor((ms % 60000) / 1000)}s`; }