export default function totalsForJob(job) {
  const totals = {
    amountDeducted: (job === null || job === void 0 ? void 0 : job.amountDeducted) || 0,
    amountOwed: 0,
    amountBilled: 0,
    amountPaid: (job === null || job === void 0 ? void 0 : job.amountPaid) || 0,
    general: 0,
    levels: {},
    sites: {},
  };
  for (const service of (job === null || job === void 0 ? void 0 : job.services) || []) {
    totals.general = totals.general + ((service === null || service === void 0 ? void 0 : service.total) || 0);
    totals.amountOwed = totals.amountOwed + ((service === null || service === void 0 ? void 0 : service.total) || 0);
    totals.amountBilled = totals.amountBilled + ((service === null || service === void 0 ? void 0 : service.total) || 0);
  }
  for (const site of (job === null || job === void 0 ? void 0 : job.sites) || []) {
    if (!totals.sites[site === null || site === void 0 ? void 0 : site.id])
      totals.sites[site.id] = 0;
    if (!totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0])
      totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] = 0;
    for (const siteService of (site === null || site === void 0 ? void 0 : site.services) || []) {
      totals.sites[site.id] = totals.sites[site.id] + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] =
        totals.levels[(site === null || site === void 0 ? void 0 : site.level) || 0] + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.amountOwed = totals.amountOwed + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
      totals.amountBilled = totals.amountBilled + ((siteService === null || siteService === void 0 ? void 0 : siteService.total) || 0);
    }
  }
  for (const payment of (job === null || job === void 0 ? void 0 : job.payments) || []) {
    totals.amountPaid = totals.amountPaid + ((payment === null || payment === void 0 ? void 0 : payment.amount) || 0);
  }
  totals.amountOwed = ((totals === null || totals === void 0 ? void 0 : totals.amountBilled) || 0) - ((totals === null || totals === void 0 ? void 0 : totals.amountPaid) || 0);
  return totals;
}
