const platformLabelMap = {
  bilibili: 'B站账号',
  douyin: '抖音账号',
  kuaishou: '快手账号',
  qita: '其他账号',
}

export const formatAmount = (amount) => {
  if (!amount && amount !== 0) return '-'
  return `¥${Number(amount).toLocaleString('zh-CN', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  })}`
}

export const formatNumberValue = (value, decimals = 0) => {
  if (value === null || value === undefined || value === '') return '-'
  if (typeof value === 'number') {
    return Number(value).toLocaleString('zh-CN', {
      minimumFractionDigits: decimals,
      maximumFractionDigits: decimals,
    })
  }
  return value
}

export const formatPercent = (value) => {
  if (value === null || value === undefined || Number.isNaN(Number(value))) return '-'
  return `${(Number(value) * 100).toFixed(2)}%`
}

export const formatThreeYearIncome = (list = []) => {
  if (!Array.isArray(list) || !list.length) return ['暂无数据']
  return list.map((item, index) => `第${index + 1}年：${formatNumberValue(item)}`)
}

export const formatAgeDistribution = (list = []) => {
  return [
    { label: '≤50岁', value: list?.[0] },
    { label: '50-70岁', value: list?.[1] },
    { label: '≥70岁', value: list?.[2] },
  ].map((bucket) => `${bucket.label}：${formatNumberValue(bucket.value)}`)
}

export const formatHistoricalEvidence = (evidence = {}) => {
  const mapping = [
    { key: 'artifacts', label: '出土实物' },
    { key: 'ancient_literature', label: '古代文献' },
    { key: 'inheritor_testimony', label: '传承人佐证' },
    { key: 'modern_research', label: '现代研究' },
  ]
  return mapping.map(({ key, label }) => `${label}：${formatNumberValue(evidence?.[key])}`)
}

export const formatPlatformAccounts = (accounts = {}) => {
  const list = Object.entries(accounts || {}).map(([platform, info]) => {
    const label = platformLabelMap[platform] || platform
    if (!info) return `${label}：-`
    return `${label}：${info.account || '-'}（赞${formatNumberValue(info.likes)} / 评${formatNumberValue(
      info.comments
    )} / 转${formatNumberValue(info.shares)}）`
  })
  return list.length ? list : ['暂无账号信息']
}

export const formatPriceRange = (range = []) => {
  if (!Array.isArray(range) || range.length < 2) return '-'
  return `${formatAmount(range[0])} - ${formatAmount(range[1])}`
}
