import isAdmin from "../../helpers/auth/isAdmin";
import cleanObjectOfReferences from "../../helpers/cleanObjectOfReferences";
import formatPhoneNumber from "../../helpers/formatPhoneNumber";
export default async function editJob(db, id, data) {
  console.log("Wee", data);
  const editedJob = { updatedAt: new Date() };
  if (data.payModelId) {
    editedJob.payModel = db.document("paymodels", data.payModelId);
  }
  if (data.cause) {
    editedJob.cause = data.cause;
  }
  if (data.address) {
    editedJob.address = await cleanObjectOfReferences(data.address);
  }
  if (data.phone) {
    editedJob.phone = await formatPhoneNumber(data.phone);
  }
  if (data.title) {
    editedJob.title = data.title;
  }
  if (data.isFranchise) {
    console.log("Is Franchise: ", data.isFranchise);
    editedJob.isFranchise = !!data.isFranchise;
  }
  if (data.locationId) {
    editedJob.location = db.document("locations", data.locationId);
  }
  if (data.tags) {
    editedJob.tags = data.tags;
  }
  if (data.matterportModelId) {
    editedJob.matterportModelId = data.matterportModelId.includes("m=")
      ? data.matterportModelId.split("m=")[1]
      : data.matterportModelId;
  }
  if (typeof data.isInsured === "boolean") {
    editedJob.isInsured = data.isInsured;
  }
  if (typeof data.isSearchable === "boolean") {
    editedJob.isSearchable = data.isSearchable;
  }
  if (typeof data.category === "number") {
    editedJob.category = data.category;
  }
  if (data.claimNumber) {
    editedJob.claimNumber = data.claimNumber;
  }
  if (data.propertyAge) {
    editedJob.propertyAge = data.propertyAge;
  }
  if (data.status) {
    editedJob.status = data.status;
  }
  if (data.serviceTypes) {
    editedJob.serviceTypes = data.serviceTypes || [];
  }
  if (data.dateOfLoss) {
    editedJob.dateOfLoss = new Date(Date.parse(data.dateOfLoss) + 360 * 60000);
  }
  if (data.amountDeducted) {
    editedJob.amountDeducted = parseFloat(data.amountDeducted);
  }
  if (typeof data.afterhours === "boolean")
    editedJob.afterhours = data.afterhours;
  if (data === null || data === void 0 ? void 0 : data.lostReason)
    editedJob.lostReason = data.lostReason;
  if (data === null || data === void 0 ? void 0 : data.moistureMap)
    editedJob.moistureMap = data.moistureMap;
  if (isAdmin() && (data === null || data === void 0 ? void 0 : data.amountBilled))
    editedJob.amountBilled = data.amountBilled;
  if (isAdmin() && (data === null || data === void 0 ? void 0 : data.amountPaid))
    editedJob.amountPaid = data.amountPaid;
  if (isAdmin() && (data === null || data === void 0 ? void 0 : data.amountDeducted))
    editedJob.amountDeducted = data.amountDeducted;
  if (isAdmin() && (data === null || data === void 0 ? void 0 : data.amountOwed))
    editedJob.amountOwed = data.amountOwed;
  return db.update("jobs", id, editedJob);
}
