/** * BoostMedia AI Content Generator Admin - Utility Functions * * @package BoostMedia_AI * @license GPL-2.0-or-later */ import { t, tf, getDateLocale } from '../lib/i18n' /** * Format a number with locale-specific formatting */ export function formatNumber(num: number, locale?: string): string { return num.toLocaleString(locale || getDateLocale()) } /** * Format a date to a localized string */ export function formatDate( dateString: string | null, options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'short', year: 'numeric', } ): string { if (!dateString) return t('Not available') const date = new Date(dateString) return date.toLocaleDateString(getDateLocale(), options) } /** * Format a date to a relative time string (e.g., "2 hours ago") */ export function formatTimeAgo(dateString: string): string { const date = new Date(dateString) const now = new Date() const diffMs = now.getTime() - date.getTime() const diffSecs = Math.floor(diffMs / 1000) const diffMins = Math.floor(diffMs / 60000) const diffHours = Math.floor(diffMs / 3600000) const diffDays = Math.floor(diffMs / 86400000) if (diffSecs < 60) return t('just now') if (diffMins < 60) return tf('%d minutes ago', diffMins) if (diffHours < 24) return tf('%d hours ago', diffHours) if (diffDays < 7) return tf('%d days ago', diffDays) return formatDate(dateString) } /** * Truncate a string to a specified length */ export function truncate(str: string, maxLength: number): string { if (str.length <= maxLength) return str return str.slice(0, maxLength - 3) + '...' } /** * Capitalize the first letter of a string */ export function capitalize(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1) } /** * Generate a unique ID */ export function generateId(): string { return Math.random().toString(36).substring(2, 11) } /** * Sleep for a specified duration */ export function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)) } /** * Debounce a function */ export function debounce unknown>( func: T, wait: number ): (...args: Parameters) => void { let timeout: ReturnType | null = null return function (...args: Parameters) { if (timeout) clearTimeout(timeout) timeout = setTimeout(() => func(...args), wait) } } /** * Copy text to clipboard */ export async function copyToClipboard(text: string): Promise { try { await navigator.clipboard.writeText(text) return true } catch { return false } } /** * Check if we're in development mode */ export function isDev(): boolean { return typeof import.meta !== 'undefined' && import.meta.env?.DEV === true } /** * Get the API base URL */ export function getApiBaseUrl(): string { return window.bmaiSettings?.apiUrl || '/wp-json/bmai/v1' } /** * Get the plugin URL */ export function getPluginUrl(): string { return window.bmaiSettings?.pluginUrl || '' }