import { useState, useCallback } from 'react'; import { depositToEscrow, withdrawFromEscrow, getEscrowBalance } from '@bettoredge/api'; import type { CalcuttaEscrowProps } from '@bettoredge/types'; export interface CalcuttaEscrowState { loading: boolean; error?: string; escrow?: CalcuttaEscrowProps; } export const useCalcuttaEscrow = (calcutta_competition_id?: string) => { const [state, setState] = useState({ loading: false }); const fetchEscrow = useCallback(async () => { if (!calcutta_competition_id) return; setState(prev => ({ ...prev, loading: true })); try { const resp = await getEscrowBalance(calcutta_competition_id); setState({ loading: false, escrow: resp.escrow }); } catch { setState(prev => ({ ...prev, loading: false })); } }, [calcutta_competition_id]); const deposit = useCallback(async (amount: number) => { if (!calcutta_competition_id) return; setState(prev => ({ ...prev, loading: true, error: undefined })); try { const resp = await depositToEscrow(calcutta_competition_id, amount); setState({ loading: false, escrow: resp.escrow }); return resp.escrow; } catch (e: any) { setState(prev => ({ ...prev, loading: false, error: e.message })); throw e; } }, [calcutta_competition_id]); const withdraw = useCallback(async (amount: number) => { if (!calcutta_competition_id) return; setState(prev => ({ ...prev, loading: true, error: undefined })); try { const resp = await withdrawFromEscrow(calcutta_competition_id, amount); setState({ loading: false, escrow: resp.escrow }); return resp.escrow; } catch (e: any) { setState(prev => ({ ...prev, loading: false, error: e.message })); throw e; } }, [calcutta_competition_id]); const updateEscrowLocally = useCallback((escrow: CalcuttaEscrowProps) => { setState({ loading: false, escrow }); }, []); return { ...state, fetchEscrow, deposit, withdraw, updateEscrowLocally }; };