Files
ghatreh_user/src/views/base/piggy/piggyIcons.ts
T
2026-08-03 22:36:23 +03:30

74 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Fund } from '@/models/fund'
export const PIGGY_PRESETS = [
'پس انداز',
'هدیه',
'ازدواج',
'تعمیرات',
'گردش',
'خرید لوازم منزل',
] as const
/** آیکون vuesax برای عنوان قلک — بدون تصویر سه‌بعدی گیج‌کننده */
export function piggyIconName(title?: string | null): string {
switch ((title ?? '').trim()) {
case 'خرید لوازم منزل':
return 'vuesax-linear:house-2'
case 'هدیه':
return 'vuesax-linear:gift'
case 'گردش':
return 'vuesax-linear:airplane'
case 'ازدواج':
return 'vuesax-linear:heart'
case 'تعمیرات':
return 'vuesax-linear:setting-2'
case 'پس انداز':
default:
return 'vuesax-linear:safe-home'
}
}
export function fundWalletGold(item?: Fund | null): number {
if (!item) return 0
const wallets = Array.isArray(item.wallet) ? item.wallet : item.wallet ? [item.wallet] : []
return wallets.reduce((acc, w) => acc + Number((w as { gold?: number })?.gold ?? 0), 0)
}
export function fundProductLabel(item?: Fund | null): string {
if (!item) return '—'
return item.product?.title || (item as { product_title?: string }).product_title || '—'
}
export function fundProductId(item?: Fund | null): number {
return Number(item?.product_id ?? item?.product?.id ?? 0)
}
export function formatPiggyAmount(value: number): string {
const n = Number(value) || 0
if (Math.abs(n) < 0.0005) return '۰'
return n.toLocaleString('fa-IR', { maximumFractionDigits: 3 })
}
export type PiggyProductGroup = {
productId: number
productTitle: string
totalGold: number
funds: Fund[]
}
export function groupFundsByProduct(funds: Fund[]): PiggyProductGroup[] {
const map = new Map<number, PiggyProductGroup>()
for (const fund of funds) {
const productId = fundProductId(fund) || -1
const productTitle = fundProductLabel(fund)
let group = map.get(productId)
if (!group) {
group = { productId, productTitle, totalGold: 0, funds: [] }
map.set(productId, group)
}
group.funds.push(fund)
group.totalGold += fundWalletGold(fund)
}
return [...map.values()].sort((a, b) => a.productTitle.localeCompare(b.productTitle, 'fa'))
}