import formatDate from "../../helpers/formatDate";
import pick from "../../helpers/pick";
import state from "../../store";
import listEfiles from "../efile/listEfiles";
import listPayments from "../payment/listPayment";
import listPhotos from "../photo/listPhotos";
import listScopeServices from "../service/listScopeServices";
import listSites from "../site/listSites";
import findUser from "../user/findUser";
import listUsers from "../user/listUsers";
export default async function findJob(db, { id, withSites, withEfiles, withUsers, withPhotos, withPayments, withReadings, }, filterKeys = [
  "id",
  "status",
  "serviceTypes",
  "category",
  "cause",
  "propertyAge",
  "title",
  "picture",
  "amountBilled",
  "amountOwed",
  "amountPaid",
  "amountDeducted",
  "customer",
  "cosigner",
  "manager",
  "referral",
  "success",
  "attributionToken",
  "address",
  "phone",
  "isInsured",
  "claimNumber",
  "sites",
  "services",
  "efiles",
  "photos",
  "payments",
  "payables",
  "location",
  "isFranchise",
  "isSearchable",
  "users",
  "userIds",
  "matterportModelId",
  "tags",
  "dateOfLoss",
  "afterhours",
  "lostReason",
  "createdAt",
  "notionId",
  "moistureMap"
]) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
  const res = await db.getDocument("jobs", id);
  const data = res.data();
  // Convert Firestore Timestamps to date strings
  try {
    data.createdAt = formatDate(data.createdAt);
    data.updatedAt = formatDate(data.updatedAt);
    data.dateOfLoss = (_b = (_a = data === null || data === void 0 ? void 0 : data.dateOfLoss) === null || _a === void 0 ? void 0 : _a.toDate) === null || _b === void 0 ? void 0 : _b.call(_a);
  }
  catch (e) {
    console.log("Error formatting timestamps for job.", e);
  }
  if (!data)
    return null;
  const job = {
    ...data,
    id,
  };
  job.services = await listScopeServices(db, { data: { jobId: id } });
  if (withPhotos)
    job.photos = await listPhotos(db, { data: { jobId: id } });
  if (withSites) {
    job.sites = await listSites(db, { data: { jobId: id, withReadings } });
    if (withPhotos) {
      for (const site of (job === null || job === void 0 ? void 0 : job.sites) || []) {
        const photos = [];
        for (const photo of (job === null || job === void 0 ? void 0 : job.photos) || []) {
          if (((_c = photo === null || photo === void 0 ? void 0 : photo.site) === null || _c === void 0 ? void 0 : _c.id) !== (site === null || site === void 0 ? void 0 : site.id))
            continue;
          photos.push(photo);
        }
        site.photos = photos;
        if (((_d = state === null || state === void 0 ? void 0 : state.site) === null || _d === void 0 ? void 0 : _d.id) === (site === null || site === void 0 ? void 0 : site.id))
          state.site = site;
      }
    }
  }
  if (withEfiles)
    job.efiles = await listEfiles(db, { data: { jobId: id } });
  if (withUsers && ((_e = data === null || data === void 0 ? void 0 : data.userIds) === null || _e === void 0 ? void 0 : _e.length))
    job.users = await listUsers(db, { data: { ids: data === null || data === void 0 ? void 0 : data.userIds } });
  if (withPayments)
    job.payments = await listPayments(db, { jobId: id });
  if ((_f = job === null || job === void 0 ? void 0 : job.location) === null || _f === void 0 ? void 0 : _f.id)
    job.location = (_h = (_g = state === null || state === void 0 ? void 0 : state.locations) === null || _g === void 0 ? void 0 : _g.find) === null || _h === void 0 ? void 0 : _h.call(_g, (location) => { var _a; return (location === null || location === void 0 ? void 0 : location.id) === ((_a = job === null || job === void 0 ? void 0 : job.location) === null || _a === void 0 ? void 0 : _a.id); });
  if ((_j = job === null || job === void 0 ? void 0 : job.referral) === null || _j === void 0 ? void 0 : _j.id)
    job.referral =
      withUsers && ((_k = data === null || data === void 0 ? void 0 : data.userIds) === null || _k === void 0 ? void 0 : _k.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.referral.id)
        : await findUser(db, job.referral.id);
  if ((_l = job === null || job === void 0 ? void 0 : job.customer) === null || _l === void 0 ? void 0 : _l.id)
    job.customer =
      withUsers && ((_m = data === null || data === void 0 ? void 0 : data.userIds) === null || _m === void 0 ? void 0 : _m.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.customer.id)
        : await findUser(db, job.customer.id);
  if ((_o = job === null || job === void 0 ? void 0 : job.cosigner) === null || _o === void 0 ? void 0 : _o.id)
    job.cosigner =
      withUsers && ((_p = data === null || data === void 0 ? void 0 : data.userIds) === null || _p === void 0 ? void 0 : _p.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.cosigner.id)
        : await findUser(db, job.cosigner.id);
  if ((_q = job === null || job === void 0 ? void 0 : job.manager) === null || _q === void 0 ? void 0 : _q.id)
    job.manager =
      withUsers && ((_r = data === null || data === void 0 ? void 0 : data.userIds) === null || _r === void 0 ? void 0 : _r.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.manager.id)
        : await findUser(db, job.manager.id);
  if ((_s = job === null || job === void 0 ? void 0 : job.success) === null || _s === void 0 ? void 0 : _s.id)
    job.success =
      withUsers && ((_t = data === null || data === void 0 ? void 0 : data.userIds) === null || _t === void 0 ? void 0 : _t.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.success.id)
        : await findUser(db, job.success.id);
  return filterKeys ? pick(job, filterKeys) : job;
}
