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, }, filterKeys = [
  "id",
  "status",
  "type",
  "category",
  "cause",
  "propertyAge",
  "title",
  "picture",
  "amountBilled",
  "amountOwed",
  "amountPaid",
  "amountDeducted",
  "customer",
  "manager",
  "referral",
  "success",
  "attributionToken",
  "address",
  "phone",
  "isInsured",
  "claimNumber",
  "sites",
  "services",
  "efiles",
  "photos",
  "payments",
  "payables",
  "location",
  "isFranchise",
  "isSearchable",
  "users",
  "userIds",
  "matterportModelId",
  "tags",
  "dateOfLoss",
  "afterhours",
]) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
  const data = await db.find("jobs", id);
  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 } });
    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 (((_a = photo === null || photo === void 0 ? void 0 : photo.site) === null || _a === void 0 ? void 0 : _a.id) !== (site === null || site === void 0 ? void 0 : site.id))
            continue;
          photos.push(photo);
        }
        site.photos = photos;
        if (((_b = state === null || state === void 0 ? void 0 : state.site) === null || _b === void 0 ? void 0 : _b.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 && ((_c = data === null || data === void 0 ? void 0 : data.userIds) === null || _c === void 0 ? void 0 : _c.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 ((_d = job === null || job === void 0 ? void 0 : job.location) === null || _d === void 0 ? void 0 : _d.id)
    job.location = (_f = (_e = state === null || state === void 0 ? void 0 : state.locations) === null || _e === void 0 ? void 0 : _e.find) === null || _f === void 0 ? void 0 : _f.call(_e, (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 ((_g = job === null || job === void 0 ? void 0 : job.referral) === null || _g === void 0 ? void 0 : _g.id)
    job.referral =
      withUsers && ((_h = data === null || data === void 0 ? void 0 : data.userIds) === null || _h === void 0 ? void 0 : _h.length)
        ? job.users.find((user) => (user === null || user === void 0 ? void 0 : user.id) === job.referral.id)
        : await findUser(db, job.referral.id);
  if ((_j = job === null || job === void 0 ? void 0 : job.customer) === null || _j === void 0 ? void 0 : _j.id)
    job.customer =
      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.customer.id)
        : await findUser(db, job.customer.id);
  if ((_l = job === null || job === void 0 ? void 0 : job.manager) === null || _l === void 0 ? void 0 : _l.id)
    job.manager =
      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.manager.id)
        : await findUser(db, job.manager.id);
  if ((_o = job === null || job === void 0 ? void 0 : job.success) === null || _o === void 0 ? void 0 : _o.id)
    job.success =
      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.success.id)
        : await findUser(db, job.success.id);
  return filterKeys ? pick(job, filterKeys) : job;
}
