/**
 * 养殖资讯详情展示辅助（对接 GET /app/farmingNews/{id}?type=）
 */

function pickText(val) {
  if (val == null) return ''
  return String(val).trim()
}

function isEmpty(val) {
  if (val == null) return true
  if (typeof val === 'string') return val.trim() === ''
  if (Array.isArray(val)) return !val.length
  return false
}

export function formatPublishTime(val) {
  const s = pickText(val)
  if (!s) return ''
  return s.length >= 10 ? s.slice(0, 10) : s
}

export function resolveDetailTitle(d) {
  if (!d) return ''
  return (
    pickText(d.standardName) ||
    pickText(d.projectName) ||
    pickText(d.policyName) ||
    pickText(d.subsidyName) ||
    pickText(d.title) ||
    ''
  )
}

export function resolveDetailIntro(d) {
  if (!d) return ''
  return pickText(d.introduction) || pickText(d.projectIntro) || ''
}

export function resolveDetailCover(d) {
  if (!d) return ''
  return pickText(d.coverFileUrl)
}

/** 正文附件 / 政策文件 / 视频等 */
export function resolveDetailContent(d) {
  if (!d) return ''
  return (
    pickText(d.contentFileUrl) ||
    pickText(d.attachmentFileUrl) ||
    pickText(d.policyFileUrl) ||
    pickText(d.detailFileUrl) ||
    pickText(d.videoFileUrl) ||
    ''
  )
}

export function resolveOpImageUrls(d, resolveUrl) {
  const list = d && Array.isArray(d.operationImages) ? d.operationImages : []
  return list
    .map((img) => {
      const raw = img && (img.fileUrl || img.url)
      return raw ? resolveUrl(raw) : ''
    })
    .filter(Boolean)
}

function pushRow(rows, label, value) {
  const v = pickText(value)
  if (!v) return
  rows.push({ label, value: v })
}

function formatMoney(val) {
  if (val == null || val === '') return ''
  const n = Number(val)
  if (Number.isNaN(n)) return pickText(val)
  return n.toLocaleString('zh-CN', { minimumFractionDigits: 0, maximumFractionDigits: 2 })
}

const CP_TYPE_KEYS = {
  1: 'cpType1',
  2: 'cpType2',
  3: 'cpType3'
}

/**
 * @param {object} d 详情 data（源表全字段）
 * @param {string} type 资讯类别编码
 * @param {(key:string)=>string} t i18n：$t 包装
 */
export function buildFarmingDetailRows(d, type, t) {
  if (!d) return []
  const rows = []
  const p = 'newsDetailPage'
  const typeCode = pickText(type)

  if (typeCode === '003004') {
    const typeKey = CP_TYPE_KEYS[d.projectType] || CP_TYPE_KEYS[String(d.projectType)]
    pushRow(rows, t(`${p}.cpProjectType`), typeKey ? t(`${p}.${typeKey}`) : d.projectType)
    pushRow(rows, t(`${p}.cpContent`), d.projectContent)
    if (!isEmpty(d.investmentAmount)) {
      pushRow(rows, t(`${p}.cpInvestment`), `${formatMoney(d.investmentAmount)}${t(`${p}.cpUnitWanYuan`)}`)
    }
    pushRow(rows, t(`${p}.cpImplYear`), d.implYear != null ? String(d.implYear) : '')
    pushRow(rows, t(`${p}.cpImplStatus`), d.implStatus)
    return rows
  }

  // 标准类 / 科技 / 政策 / 补贴：补充常用业务字段
  pushRow(rows, t(`${p}.fieldProjectContent`), d.projectContent)
  pushRow(rows, t(`${p}.fieldRemark`), d.remark)
  if (d.implYear != null) {
    pushRow(rows, t(`${p}.cpImplYear`), String(d.implYear))
  }
  if (!isEmpty(d.investmentAmount)) {
    pushRow(rows, t(`${p}.cpInvestment`), `${formatMoney(d.investmentAmount)}${t(`${p}.cpUnitWanYuan`)}`)
  }
  pushRow(rows, t(`${p}.cpImplStatus`), d.implStatus)
  return rows
}
