export type ProductCashFeeSource = { cash_fee_buy?: number | null cash_fee_sell?: number | null cash_fee_buy_is_percent?: boolean | null cash_fee_sell_is_percent?: boolean | null } /** 0.5 => 0.5% ، 5 => 5% — همیشه عدد درصد */ export function resolveCashFeePercentRatio(percentValue?: number | null) { const offset = Number(percentValue ?? 0) if (!offset || !Number.isFinite(offset)) { return 0 } return offset / 100 } /** @deprecated از resolveCashFeePercentRatio استفاده کنید */ export function resolveFeePercentRatio(percentValue?: number | null) { return resolveCashFeePercentRatio(percentValue) } export function resolveProductCashFeeRial( feeValue: number | null | undefined, isPercent: boolean | null | undefined, basePriceRial: number, ) { const raw = Number(feeValue ?? 0) if (!raw || !Number.isFinite(raw)) { return 0 } if (!isPercent) { return Math.round(raw) } const base = Number(basePriceRial) if (!base || base <= 0) { return 0 } return Math.round(Math.abs(base * resolveCashFeePercentRatio(raw))) } export function resolveProductCashFeeBuyRial(product: ProductCashFeeSource, basePriceRial: number) { const baseFee = resolveProductCashFeeRial( product.cash_fee_buy, product.cash_fee_buy_is_percent, basePriceRial, ) const special = Number((product as any).special_profit_buy ?? 0) return baseFee + (Number.isFinite(special) ? Math.round(special) : 0) } export function resolveProductCashFeeSellRial(product: ProductCashFeeSource, basePriceRial: number) { const baseFee = resolveProductCashFeeRial( product.cash_fee_sell, product.cash_fee_sell_is_percent, basePriceRial, ) const special = Number((product as any).special_profit_sell ?? 0) return baseFee + (Number.isFinite(special) ? Math.round(special) : 0) } export function formatProductCashFeeLabel( feeValue: number | null | undefined, isPercent?: boolean | null, ) { const raw = Number(feeValue ?? 0) if (!raw) { return '۰' } if (isPercent) { const text = Number.isInteger(raw) ? String(raw) : String(parseFloat(raw.toFixed(4))) return `${text}%` } return `${raw} ریال` }