import { useState, useCallback } from 'react'; import { SearchResponse, ClubInfo, Competition, Match, } from '../services/facr/types'; import facrApi from '../services/facr/facrApi'; interface UseFacrApiReturn { // Search for clubs by query searchClubs: (query: string) => Promise; searchResults: SearchResponse['results'] | []; searchLoading: boolean; searchError: Error | null; // Get club details by ID and type getClub: (clubId: string, clubType?: 'football' | 'futsal') => Promise; // Get club table/standings by ID and type getClubTable: (clubId: string, clubType?: 'football' | 'futsal') => Promise; // Get all competitions for a club getClubCompetitions: (clubId: string, clubType?: 'football' | 'futsal') => Promise; // Get matches for a specific competition getCompetitionMatches: (competitionId: string) => Promise; // Clear the API cache clearCache: () => void; // Loading state loading: boolean; // Error state error: Error | null; } export const useFacrApi = (): UseFacrApiReturn => { const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [searchResults, setSearchResults] = useState([]); const [searchLoading, setSearchLoading] = useState(false); const [searchError, setSearchError] = useState(null); const handleApiCall = useCallback( async (apiCall: () => Promise): Promise => { setLoading(true); setError(null); try { return await apiCall(); } catch (err) { const error = err instanceof Error ? err : new Error('An unknown error occurred'); setError(error); throw error; } finally { setLoading(false); } }, [] ); const searchClubs = useCallback( async (query: string): Promise => { setSearchLoading(true); setSearchError(null); try { const response = await handleApiCall(() => facrApi.searchClubs(query)); setSearchResults(response.results || []); return response; } catch (err) { const error = err instanceof Error ? err : new Error('Failed to search clubs'); setSearchError(error); throw error; } finally { setSearchLoading(false); } }, [handleApiCall] ); const getClub = useCallback( (clubId: string, clubType: 'football' | 'futsal' = 'football'): Promise => handleApiCall(() => facrApi.getClub(clubId, clubType)), [handleApiCall] ); const getClubTable = useCallback( (clubId: string, clubType: 'football' | 'futsal' = 'football'): Promise => handleApiCall(() => facrApi.getClubTable(clubId, clubType)), [handleApiCall] ); const getClubCompetitions = useCallback( (clubId: string, clubType: 'football' | 'futsal' = 'football'): Promise => handleApiCall(() => facrApi.getClubCompetitions(clubId, clubType)), [handleApiCall] ); const getCompetitionMatches = useCallback( (competitionId: string): Promise => handleApiCall(() => facrApi.getCompetitionMatches(competitionId)), [handleApiCall] ); const clearCache = useCallback(() => { facrApi.clearCache(); }, []); return { searchClubs, searchResults, searchLoading, searchError, getClub, getClubTable, getClubCompetitions, getCompetitionMatches, clearCache, loading, error, }; }; export default useFacrApi;