import { useGamesStore } from '#lib/stores' import { useLobbyGames, useCasino } from '#lib/composables' import { storeToRefs } from 'pinia' import { CASINO_LIMIT } from '#lib/constants' export const useFetchGames = () => { const { requestFetchGames } = useLobbyGames() const { requestFetchLiveCasino } = useCasino({ gameLimit: CASINO_LIMIT, }) const gamesStore = useGamesStore() const isFetchingGame = ref(false) const isFetchingCasinoGames = ref(false) const { allGames, allCasinoGames } = storeToRefs(gamesStore) const fetchAllGames = async () => { if (allGames?.value?.length == 0) { try { isFetchingGame.value = true const response = await requestFetchGames({}) gamesStore.setAllGames(response) } catch (ex) { console.error(' ~ fetchAllGames ~ ex:', ex) } finally { isFetchingGame.value = false } } } const fetchAllCasinoGames = async () => { if (allCasinoGames?.value?.length == 0) { try { isFetchingCasinoGames.value = true const response = await requestFetchLiveCasino({}) if (response) { gamesStore.setAllCasinoGames(response) } } catch (ex) { console.error(' ~ fetchAllCasinoGames ~ ex:', ex) } finally { isFetchingCasinoGames.value = false } } } return { isFetchingGame, isFetchingCasinoGames, allGames, allCasinoGames, fetchAllCasinoGames, fetchAllGames, } }