function normalizeText(value) {
  return String(value || '').trim()
}

function isProvided(value) {
  const text = normalizeText(value)
  return Boolean(text) && !['待补充', '暂无', '无', '未知'].includes(text)
}

function resolveApplicationDetailPayload(request = {}) {
  const flags = Array.isArray(request.risk_flags_json)
    ? request.risk_flags_json
    : Array.isArray(request.riskFlags)
      ? request.riskFlags
      : []
  const detailFlag = flags.find((flag) =>
    flag &&
    typeof flag === 'object' &&
    normalizeText(flag.source) === 'application_detail'
  )
  const detail = detailFlag?.application_detail || detailFlag?.applicationDetail || {}
  return detail && typeof detail === 'object' ? detail : {}
}

function pickDetailValue(detail, request, keys = [], fallback = '') {
  for (const key of keys) {
    const value = normalizeText(detail[key] ?? request[key])
    if (isProvided(value)) return value
  }
  return normalizeText(fallback)
}

export function buildApplicationDetailFactItems(request = {}) {
  const detail = resolveApplicationDetailPayload(request)
  const amountDisplay = normalizeText(request.amountDisplay || request.amount)
  const rows = [
    {
      key: 'application_type',
      label: '申请类型',
      value: pickDetailValue(detail, request, ['application_type', 'typeLabel'], request.typeLabel)
    },
    {
      key: 'time',
      label: '发生时间',
      value: pickDetailValue(detail, request, ['time', 'occurredDisplay', 'period'], request.occurredDisplay)
    },
    {
      key: 'location',
      label: '地点',
      value: pickDetailValue(detail, request, ['location', 'sceneTarget', 'city'], request.sceneTarget)
    },
    {
      key: 'reason',
      label: '事由',
      value: pickDetailValue(detail, request, ['reason'], request.reason)
    },
    {
      key: 'days',
      label: '天数',
      value: pickDetailValue(detail, request, ['days'])
    },
    {
      key: 'transport_mode',
      label: '出行方式',
      value: pickDetailValue(detail, request, ['transport_mode'])
    },
    {
      key: 'grade',
      label: '职级',
      value: pickDetailValue(detail, request, ['grade', 'profileGrade', 'employee_grade'], request.profileGrade),
      highlight: true
    },
    {
      key: 'lodging_daily_cap',
      label: '住宿上限/天',
      value: pickDetailValue(detail, request, ['lodging_daily_cap']),
      highlight: true
    },
    {
      key: 'subsidy_daily_cap',
      label: '补贴标准/天',
      value: pickDetailValue(detail, request, ['subsidy_daily_cap']),
      highlight: true
    },
    {
      key: 'transport_policy',
      label: '交通费用口径',
      value: pickDetailValue(detail, request, ['transport_policy'], '车票、机票暂无实时价格接口，按真实票据实报实销')
    },
    {
      key: 'policy_estimate',
      label: '规则测算参考',
      value: pickDetailValue(detail, request, ['policy_estimate']),
      highlight: true
    },
    {
      key: 'amount',
      label: '用户预估费用',
      value: pickDetailValue(detail, request, ['amount'], amountDisplay),
      highlight: true,
      emphasis: true
    }
  ]

  return rows.filter((row) => isProvided(row.value))
}
