import React, { useState, useEffect } from 'react'; import { StyleSheet, TouchableOpacity, LayoutAnimation, Platform, UIManager } from 'react-native'; import { View, Text, useTheme } from '@bettoredge/styles'; import { Ionicons } from '@expo/vector-icons'; import type { CalcuttaEscrowProps } from '@bettoredge/types'; import { useCalcuttaEscrow } from '../hooks/useCalcuttaEscrow'; import { formatCurrency } from '../helpers/formatting'; import { CalcuttaEscrow } from './CalcuttaEscrow'; if (Platform.OS === 'android' && UIManager.setLayoutAnimationEnabledExperimental) { UIManager.setLayoutAnimationEnabledExperimental(true); } export interface EscrowWidgetProps { calcutta_competition_id: string; market_type: string; max_escrow?: number; player_balance?: number; onDepositFunds?: (amount: number) => void; readOnly?: boolean; onUpdate?: () => void; initialExpanded?: boolean; } export const EscrowWidget: React.FC = ({ calcutta_competition_id, market_type, max_escrow, player_balance, onDepositFunds, readOnly, onUpdate, initialExpanded, }) => { const { theme } = useTheme(); const { escrow, fetchEscrow } = useCalcuttaEscrow(calcutta_competition_id); const [expanded, setExpanded] = useState(initialExpanded ?? false); useEffect(() => { if (initialExpanded) setExpanded(true); }, [initialExpanded]); useEffect(() => { fetchEscrow(); }, []); const available = Number(escrow?.escrow_balance ?? 0); const committed = Number(escrow?.committed_balance ?? 0); const hasFunds = available > 0 || committed > 0; const toggle = () => { LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); setExpanded(!expanded); }; return ( {/* Collapsed header — always visible */} {formatCurrency(available, market_type)} available {committed > 0 && ( {formatCurrency(committed, market_type)} committed )} {!readOnly && ( {!expanded && ( {hasFunds ? 'Manage' : 'Add Funds'} )} )} {/* Budget cap bar (always visible if applicable) */} {max_escrow != null && Number(max_escrow) > 0 && ( Budget: {formatCurrency(Number(max_escrow), market_type)} )} {/* Expanded: full escrow management */} {expanded && !readOnly && ( { fetchEscrow(); onUpdate?.(); }} /> )} ); }; const styles = StyleSheet.create({ container: { borderRadius: 14, borderWidth: 1, overflow: 'hidden', }, header: { flexDirection: 'row', alignItems: 'center', padding: 14, }, iconCircle: { width: 36, height: 36, borderRadius: 18, alignItems: 'center', justifyContent: 'center', }, headerText: { flex: 1, marginLeft: 12, }, addBtn: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 12, paddingVertical: 6, borderRadius: 20, }, budgetBar: { paddingHorizontal: 14, paddingBottom: 12, }, barTrack: { height: 4, borderRadius: 2, overflow: 'hidden', }, barFill: { height: 4, borderRadius: 2, }, expandedContent: { borderTopWidth: 1, borderTopColor: 'rgba(128,128,128,0.15)', }, });