import type { Product } from '@/models/products' import type { WalletInventoryCard, WalletMetalTotal } from '@/models/walletAssets' import { resolveProductImageSrc } from '@/utils/productImage' export function fundPageInventoryCards(cards: T[]): T[] { return cards.filter((card) => Number(card.quantity ?? 0) > 0) } export function resolveSummaryMetalWeight( metal: { total_weight: number }, _cards?: Array<{ metal_id?: number; quantity?: number }> ): number { return Number(metal.total_weight ?? 0) } export function enrichInventoryCardsWithCatalog( cards: T[], catalog: Product[] ): T[] { return cards.map((card) => { const fromCatalog = catalog.find((product) => product.id === card.product_id) if (!fromCatalog) { return card } return { ...card, file: card.file ?? fromCatalog.file ?? null, } }) } export function getProductDisplayImage(product?: { group_type?: string | null title?: string | null file?: { src?: string | null } | null img?: string | null } | null): string { return resolveProductImageSrc(product) } import { formatGoldAmount, usePriceWithoutType } from '@/utils/usePrice' export function formatMetalTotal( metal: WalletMetalTotal, inventoryCards: WalletInventoryCard[] ): string { return `${formatGoldAmount(resolveSummaryMetalWeight(metal, inventoryCards))}${metal.unit}` } export function resolveAssetsRialValue( summary: { total_rial_value?: number assets_rial_value?: number cash_wallet?: number } | null | undefined, liveGoldPrice = 0 ): number { const total = Number(summary?.total_rial_value ?? 0) if (total > 0) { return total } const cash = Number(summary?.cash_wallet ?? 0) const assetsRial = Number(summary?.assets_rial_value ?? 0) if (assetsRial > 0) { return cash + assetsRial } const metalWeight = (summary as { metal_totals?: Array<{ total_weight?: number }> } | null) ?.metal_totals?.[0]?.total_weight return cash + Number(metalWeight ?? 0) * liveGoldPrice } export function getProductInventoryQuantity( cards: WalletInventoryCard[], productId?: number | null, mainWalletGold?: number | null, ): number { const card = cards.find((item) => Number(item.product_id) === Number(productId)) return card ? Number(card.quantity ?? 0) : Number(mainWalletGold ?? 0) } export function formatProductWalletBalanceText( product: | { id?: number; title?: string | null; unit?: string | null; group_type?: string | null } | null, cards: WalletInventoryCard[] = [], mainWalletGold?: number | null, ): string { const card = cards.find((item) => Number(item.product_id) === Number(product?.id)) const quantity = card ? Number(card.quantity ?? 0) : Number(mainWalletGold ?? 0) const unit = card?.unit ?? product?.unit?.trim() ?? 'گرم' const title = product?.title?.trim() || 'محصول' const formatted = unit === 'عدد' ? usePriceWithoutType(quantity, false) : formatGoldAmount(quantity) return `موجودی کیف ${title}: ${formatted} ${unit}` } export function getCashWalletBalance( walletInfo?: Record | null, wallets?: unknown, ): number { const direct = Number(walletInfo?.cash ?? walletInfo?.count_cash_wallets ?? 0) if (direct) return direct if (Array.isArray(wallets)) { return wallets.reduce((sum, wallet) => sum + Number(wallet?.cash ?? 0), 0) } return Number((wallets as { cash?: number } | null)?.cash ?? 0) }