import type { AssetPackage } from '@/api/assets'

export function getPackageUsedMb(pkg?: AssetPackage | null) {
  if (!pkg)
    return 0

  return Number(pkg.enable_virtual_data ? pkg.virtual_used_mb : pkg.real_used_mb) || 0
}

export function getPackageTotalMb(pkg?: AssetPackage | null) {
  if (!pkg)
    return 0

  return Number(pkg.real_total_mb) || 0
}

export function getPackageRemainMb(pkg?: AssetPackage | null) {
  return Math.max(0, getPackageTotalMb(pkg) - getPackageUsedMb(pkg))
}

export function getPackageUsagePercent(pkg?: AssetPackage | null) {
  const total = getPackageTotalMb(pkg)
  if (total <= 0)
    return 0

  return Math.min(100, (getPackageUsedMb(pkg) / total) * 100)
}

export function formatPackageDataSize(mb?: number | null) {
  const value = Number(mb) || 0

  if (value >= 1024)
    return `${(value / 1024).toFixed(2)}GB`

  return `${value.toFixed(2)}MB`
}

export function formatPackagePrice(amount?: number | null) {
  if (amount === undefined || amount === null)
    return '-'

  const yuan = amount / 100
  return Number.isInteger(yuan) ? `¥${yuan}` : `¥${yuan.toFixed(2)}`
}
