import cloneDeep from 'lodash/cloneDeep' import maxBy from 'lodash/maxBy' import type { GameSlotJackpotResponseData } from '#lib/types' import type { JackpotBanner } from '#lib/types/socket' import { useJackpotStore } from '#lib/stores/jackpot' import { computed, ref } from 'vue' import { AppDataEnum } from '#lib/enums' import { useApiFetch } from '../service/use-api-fetch' import { APP_API } from '#lib/configs' import { useAsyncData } from 'nuxt/app' import cacheManager from '#lib/utils/cache-manager' /** * Composable for managing and retrieving jackpot data for slot games. * Provides functionality to fetch jackpots, calculate total jackpots, * and retrieve jackpot amounts by game ID. * * @returns {Object} An object containing functions and reactive variables for jackpot management. * @namespace */ export function useJackpot() { const jackpotStore = useJackpotStore() const isLoading = ref(false) const { request } = useApiFetch() /** * Fetches the current jackpot data from the API and updates the jackpot store. * Handles loading state and errors gracefully. * * @returns {Promise} A promise that resolves when the jackpot data has been fetched and stored. */ const fetchGameJackpots = async () => { await useAsyncData(AppDataEnum.JackpotGame, async () => { // check cache const cachedData = cacheManager.get(AppDataEnum.JackpotGame) if (cachedData) { return cachedData } try { isLoading.value = true const { data } = await request( APP_API.JACKPOT, ) if (data) { const jackpotNumber = cloneDeep(data) delete jackpotNumber.big_jackpot jackpotStore.setJackpot(jackpotNumber) if (data.big_jackpot) { const jackpotBanner = data.big_jackpot as unknown as JackpotBanner[] const gameItem: JackpotBanner | undefined = maxBy( jackpotBanner.filter(item => item.on), 'currentValue' ) if (gameItem) { jackpotStore.setXocDiaJackpot({ gameId: gameItem.id, gameName: gameItem.name, value: gameItem.currentValue }) return } jackpotStore.setXocDiaJackpot({ gameId: '', gameName: '', value: 0 }) } } cacheManager.set(AppDataEnum.JackpotGame, data) return data } catch { cacheManager.delete(AppDataEnum.JackpotGame) } finally { isLoading.value = false } }) } /** * A computed property that returns the current jackpot data from the store. * * @returns {GameSlotJackpotResponseData} The current jackpot data. */ const jackpots = computed((): GameSlotJackpotResponseData => { return jackpotStore.getJackpot() }) /** * A computed property that calculates the total amount of all jackpots. * * @returns {number} The total amount of all jackpots. */ const totalJackpots = computed((): number => { const numberJackpots = Object.values(jackpotStore.getJackpot() ?? {}) return numberJackpots.reduce((accumulator: number, amount: number) => { return accumulator + amount }, 0) }) /** * Retrieves the jackpot amount for a specific game by its game ID. * * @param {string} gameId The ID of the game for which to retrieve the jackpot amount. * @returns {number} The jackpot amount for the specified game ID. */ const getJackpotNumberByGameId = (gameId: string): number => { if (!jackpots.value || !gameId) { return 0 } let jackpotNumber = 0 Object.keys(jackpots.value).forEach(key => { if (gameId.replace('_', '').includes(key)) { jackpotNumber = jackpots.value[key] return } }) return jackpotNumber } return { jackpots, isLoading, totalJackpots, fetchGameJackpots, getJackpotNumberByGameId, } }