import { formatMoney } from "@vmall/shared"; import type { Currency } from "@vmall/shared"; /** Currency-aware money formatter: exponents come from the API, cached app-wide. */ export function useMoney() { const { $api } = useNuxtApp(); const { locale } = useI18n(); const currencies = useState("vmall.currencies", () => []); async function load(): Promise { if (currencies.value.length > 0) return; try { currencies.value = await $api.listCurrencies(); } catch { /* keep empty; fmt falls back to exponent 2 */ } } function fmt(amountMinor: number, currency: string): string { const exponent = currencies.value.find((c) => c.code === currency)?.exponent ?? 2; return formatMoney(amountMinor, currency, exponent, locale.value); } return { currencies, load, fmt }; }