export default async function addSite(db, data) {
  const job = (data === null || data === void 0 ? void 0 : data.jobId) ? db.document("jobs", data.jobId) : null;
  const docData = {
    ...data,
    job,
    createdAt: new Date(),
  };
  const areas = (data === null || data === void 0 ? void 0 : data.areas) || [];
  let areaTotalSqft = 0;
  let areaTotalCuft = 0;
  for (const [i, area] of areas.entries()) {
    areas[i].height = area.height ? parseFloat(area.height) : 0;
    areas[i].width = area.width ? parseFloat(area.width) : 0;
    areas[i].length = area.length ? parseFloat(area.length) : 0;
    areas[i].sqft = areas[i].width * areas[i].length;
    areaTotalSqft = areaTotalSqft + areas[i].sqft;
    areas[i].cuft = areas[i].sqft * areas[i].height;
    areaTotalCuft = areaTotalCuft + areas[i].cuft;
  }
  docData.areas = areas;
  docData.areaTotalSqft = areaTotalSqft;
  docData.areaTotalCuft = areaTotalCuft;
  const affectedAreas = (data === null || data === void 0 ? void 0 : data.affectedAreas) || [];
  let affectedAreaTotalSqft = 0;
  let affectedAreaTotalCuft = 0;
  for (const [i, affectedArea] of affectedAreas.entries()) {
    affectedAreas[i].height = affectedArea.height
      ? parseFloat(affectedArea.height)
      : 0;
    affectedAreas[i].width = affectedArea.width
      ? parseFloat(affectedArea.width)
      : 0;
    affectedAreas[i].length = affectedArea.length
      ? parseFloat(affectedArea.length)
      : 0;
    affectedAreas[i].sqft = affectedAreas[i].width * affectedAreas[i].length;
    affectedAreaTotalSqft = affectedAreaTotalSqft + affectedAreas[i].sqft;
    affectedAreas[i].cuft = affectedAreas[i].sqft * affectedAreas[i].height;
    affectedAreaTotalCuft = affectedAreaTotalCuft + affectedAreas[i].cuft;
  }
  docData.affectedAreas = affectedAreas;
  docData.affectedAreaTotalSqft = affectedAreaTotalSqft;
  docData.affectedAreaTotalCuft = affectedAreaTotalCuft;
  const addDoc = await db.add("sites", docData);
  docData.id = addDoc.id;
  return docData;
}
