function sumDebtTotalAmount(debts) {
  if (!Array.isArray(debts)) {
    return 0;
  }
  return debts.reduce((total, item) => total + Number(item.debtTotalAmount || 0), 0);
}

export function normalizeCreditDetail(data = {}, row = {}) {
  const negativeInfo = data.negativeInfo || {};
  const debts = Array.isArray(data.debtList) ? data.debtList : [];
  return {
    personId: data.personId || row.idCard || "",
    personName: data.personName || row.name || "",
    idCard: data.idCard || data.personId || row.idCard || "",
    queryDate: data.queryDate || negativeInfo.queryDate,
    debtCount: debts.length,
    debtTotalAmount: sumDebtTotalAmount(debts),
    negativeInfo,
    debts,
  };
}

export function createEmptyCreditDetail() {
  return normalizeCreditDetail();
}
