import state from "../../store";
export default async function editSite(db, id, data) {
  const editedSite = { ...data, updatedAt: new Date() };
  if (data === null || data === void 0 ? void 0 : data.areas) {
    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;
    }
    editedSite.areas = areas;
    editedSite.areaTotalSqft = areaTotalSqft;
    editedSite.areaTotalCuft = areaTotalCuft;
  }
  if (data === null || data === void 0 ? void 0 : data.affectedAreas) {
    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;
    }
    editedSite.affectedAreas = affectedAreas;
    editedSite.affectedAreaTotalSqft = affectedAreaTotalSqft;
    editedSite.affectedAreaTotalCuft = affectedAreaTotalCuft;
  }
  state.site = { ...state.site, ...editedSite };
  return db.update("sites", id, editedSite);
}
//# sourceMappingURL=editSite.js.map