import type { CalcuttaPayoutRuleProps } from '@bettoredge/types'; export const calculatePayoutPreview = ( total_pot: number, rules: CalcuttaPayoutRuleProps[] ): { description: string; payout_pct: number; payout_amount: number }[] => { return rules.map(rule => ({ description: rule.description || `${rule.payout_type === 'round' ? `Round ${rule.round_number}` : `${rule.placement}${getOrdinal(rule.placement || 0)} Place`}`, payout_pct: Number(rule.payout_pct), payout_amount: (total_pot * Number(rule.payout_pct)) / 100, })); }; export const validatePayoutRules = (rules: CalcuttaPayoutRuleProps[]): string[] => { const errors: string[] = []; if (rules.length === 0) { errors.push('At least one payout rule is required'); return errors; } const totalPct = rules.reduce((sum, r) => sum + Number(r.payout_pct), 0); if (Math.abs(totalPct - 100) > 0.01) { errors.push(`Payout percentages must sum to 100% (currently ${totalPct.toFixed(1)}%)`); } for (const rule of rules) { if (Number(rule.payout_pct) <= 0) { errors.push(`Payout percentage must be greater than 0`); } } return errors; }; const getOrdinal = (n: number): string => { const suffixes = ['th', 'st', 'nd', 'rd']; const v = n % 100; return suffixes[(v - 20) % 10] || suffixes[v] || suffixes[0]; };