export function formatCurrency( value: number, currency: { symbol: string; currency_position: 'left' | 'right' | 'leftspace' | 'rightspace'; thousand_separator: string; decimal_separator: string; decimal_number: number; }, wallet: boolean = false ): string { const parts = value.toFixed(currency.decimal_number).split('.'); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, currency.thousand_separator); const formatted = parts.join(currency.decimal_separator); if (wallet) { return formatted; } switch (currency.currency_position) { case 'left': return currency.symbol + formatted; case 'right': return formatted + currency.symbol; case 'leftspace': return `${currency.symbol} ${formatted}`; case 'rightspace': return `${formatted} ${currency.symbol}`; default: return formatted; } } export function formatRewardValue(reward: { key: string; value: number | string; currency: { symbol: string; currency_position: string; thousand_separator: string; decimal_separator: string; decimal_number: number; }; details?: any; }): string { const { key, value, currency, details } = reward; if (key === 'rewards_fixed_discount') { return formatCurrency(Number(value), currency); } if (key === 'rewards_units') { return `${formatCurrency(Number(value), currency, true)} ${currency['symbol']}`; } if ( key === 'rewards_coupon' && (details?.coupon?.type === 'fixed_cart' || details?.coupon?.type === 'fixed_product') ) { return formatCurrency(Number(value), currency); } return `${value}${currency}`; } export function shouldShowRewardDetails(reward: any): boolean { if (!reward) return false; const details = reward.details ?? {}; return Object.entries(details).some(([key, val]) => { if (val == null) return false; if (key === 'coupon' && typeof val === 'object') { const excludedKeys = ['amount', 'id', 'main_currency', 'type', 'type_label']; return Object.entries(val).some(([k, v]) => { if (excludedKeys.includes(k)) return false; if (v === null || v === '' || v === false || v === 0) return false; if (Array.isArray(v)) return v.length > 0; if (typeof v === 'object') return Object.keys(v).length > 0; return true; }); } if (Array.isArray(val)) return val.length > 0; if (typeof val === 'object') return Object.keys(val).length > 0; return true; }); } export const convertWPDateFormat = (wpFormat: string): string => { const formatMapping: Record = { 'd/m/Y': 'dd/MM/yyyy', 'm/d/Y': 'MM/dd/yyyy', 'Y-m-d': 'yyyy-MM-dd', 'F j, Y': 'LLLL d, yyyy', 'j F Y': 'd LLLL yyyy', 'Y/m/d': 'yyyy/MM/dd', 'd.m.Y': 'dd.MM.yyyy' }; return formatMapping[wpFormat] || 'yyyy-MM-dd'; };