import type { Wallet } from '@/model/wallet' export function getWallets(wallet?: Wallet | Wallet[]): Wallet[] { if (!wallet) return [] return Array.isArray(wallet) ? wallet : [wallet] } /** کیف اصلی مشتری — بدون fund و بدون product */ export function getMainWallet( wallet?: Wallet | Wallet[], ownerType?: number | null ): Wallet | undefined { const wallets = getWallets(wallet) return wallets.find((w) => { if (w.fund_id != null || w.product_id != null) return false if (ownerType != null && w.owner_type != null && Number(w.owner_type) !== Number(ownerType)) { return false } return true }) } /** پارس setting `main_wallet_products` — id با کاما یا JSON */ export function parseMainWalletProductIds(raw?: string | null): number[] { const value = (raw ?? '').trim() if (!value) return [] try { const decoded = JSON.parse(value) if (Array.isArray(decoded)) { return [...new Set(decoded.map((id) => Number(id)).filter((id) => id > 0))] } } catch { // کاما‌جدا } return [ ...new Set( value .split(',') .map((part) => Number(part.trim())) .filter((id) => id > 0) ) ] } let cachedMainWalletProductIds: number[] | null = null export function setCachedMainWalletProductIds(ids: number[]): void { cachedMainWalletProductIds = ids } /** بارگذاری یک‌باره از API تنظیمات */ export async function initMainWalletProductIds(): Promise { if (cachedMainWalletProductIds !== null) { return cachedMainWalletProductIds } const { initWalletSettingsFromApi } = await import('@/utils/config') await initWalletSettingsFromApi() return cachedMainWalletProductIds ?? [] } export function getMainWalletProductIds(): number[] { return cachedMainWalletProductIds ?? [] } /** * موجودی ریالی کیف اصلی. * با main_wallet_products ممکن است کیف legacy محصول (مثلاً id=13) cash=0 داشته باشد — فقط کیف اصلی معتبر است. */ export function getCashBalance( wallet?: Wallet | Wallet[], options?: { walletCash?: number | null; ownerType?: number | null } ): number { if (options?.walletCash != null) { return Number(options.walletCash) } return getMainWallet(wallet, options?.ownerType)?.cash ?? 0 } /** * جمع طلای قابل‌نمایش — هم‌راستا با WalletBalanceService سرور. * کیف legacy محصولات main_wallet_products در جمع نمی‌آید. */ export function getGoldBalance( wallet?: Wallet | Wallet[], options?: { walletGold?: number | null; mainWalletProductIds?: number[]; ownerType?: number | null } ): string { if (options?.walletGold != null) { return Number(options.walletGold).toFixed(3) } const mainWalletProductIds = options?.mainWalletProductIds ?? getMainWalletProductIds() const ownerType = options?.ownerType ?? null const sum = getWallets(wallet).reduce((total, line) => { if (ownerType != null && line.owner_type != null && Number(line.owner_type) !== Number(ownerType)) { return total } const gold = Number(line.gold ?? 0) if (gold <= 0) return total if (line.fund_id != null) { return total + gold } if (line.product_id == null) { return total + gold } const productId = Number(line.product_id) if (mainWalletProductIds.length > 0 && mainWalletProductIds.includes(productId)) { return total } return total + gold }, 0) return sum.toFixed(3) }