import { ref } from 'vue' import { loading } from '@/utils' import { getLastPrices, startLastPricePolling, stopLastPricePolling } from '@/utils/price' import { useWalletAssets } from '@/composables/useWalletAssets' import { clearProductsCatalog } from '@/composables/useProductsCatalog' import { getHomeLayoutId } from '../layouts/registry' import { fetchHomeChartPrices, loadHomeMaftolOffset, resetHomePriceChart } from './usePriceChart' import { loadHomeBanners, resetHomeBanners } from './useBanners' import { loadHomeFunds, resetHomeFunds } from './useUserFunds' /** * Single entry point for all homepage API calls. * Mobile and desktop layouts must not fetch independently — they consume shared module state. */ let initPromise: Promise | null = null const initialized = ref(false) export function isHomePageDataInitialized() { return initialized.value } export async function initHomePageData(force = false) { if (!force && initialized.value) { // Resume live price updates when returning to home without refetching datasets startLastPricePolling() return } if (!force && initPromise) { return initPromise } initPromise = (async () => { try { loading.value = true const { loadWalletAssets } = useWalletAssets() await Promise.all([ loadHomeFunds(force), loadWalletAssets(force), getLastPrices(), fetchHomeChartPrices(force), startLastPricePolling(), loadHomeBanners(force), ]) if (getHomeLayoutId() === 'boursecu') { await loadHomeMaftolOffset(force) } initialized.value = true } catch (error) { console.error('Home page initialization error:', error) } finally { loading.value = false initPromise = null } })() return initPromise } /** Clears homepage caches — call on logout or when a full refresh is required. */ export function resetHomePageData() { initialized.value = false initPromise = null stopLastPricePolling() resetHomeBanners() resetHomeFunds() resetHomePriceChart() clearProductsCatalog() } export function useHomePageInitState() { return { initialized } }