import Fuse from "fuse.js";
import pick from "../../helpers/pick";
import state from "../../store";
import findUser from "../user/findUser";
export default async function listJobs(db, { data: { locationId, userId, status, tags, limit, orderBy, orderDirection, next, query, }, }, filterKeys = [
  "id",
  "status",
  "type",
  "category",
  "cause",
  "propertyAge",
  "title",
  "picture",
  "amountBilled",
  "amountOwed",
  "amountPaid",
  "amountDeducted",
  "customer",
  "manager",
  "referral",
  "attributionToken",
  "address",
  "phone",
  "isInsured",
  "claimNumber",
  "sites",
  "services",
  "efiles",
  "photos",
  "payments",
  "payables",
  "location",
  "isFranchise",
  "isSearchable",
  "users",
  "userIds",
  "matterportModelId",
  "tags",
  "dateOfLoss",
  "afterhours",
  "createdAt",
]) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
  const where = [];
  if (locationId)
    where.push({
      key: "location",
      conditional: "==",
      value: await db.document("locations", locationId),
    });
  if (userId)
    where.push({
      key: "userIds",
      conditional: "array-contains",
      value: userId,
    });
  if (status === null || status === void 0 ? void 0 : status.length)
    where.push({
      key: "status",
      conditional: "in",
      value: status,
    });
  if (tags === null || tags === void 0 ? void 0 : tags.length)
    where.push({
      key: "tags",
      conditional: "array-contains-any",
      value: tags,
    });
  const startAfter = (next === null || next === void 0 ? void 0 : next.length) && (await db.getDocument("jobs", next));
  let jobs = (await db.list("jobs", where, `${orderBy || "createdAt"}:${orderDirection || "desc"}`, (query === null || query === void 0 ? void 0 : query.length) ? null : limit || 20, startAfter && {
    startAfter: (_b = (_a = startAfter === null || startAfter === void 0 ? void 0 : startAfter.data) === null || _a === void 0 ? void 0 : _a.call(startAfter)) === null || _b === void 0 ? void 0 : _b.createdAt,
  }));
  if (!jobs)
    return null;
  for (const [index, job] of Object.entries(jobs)) {
    if ((_c = job === null || job === void 0 ? void 0 : job.location) === null || _c === void 0 ? void 0 : _c.id)
      job.location = (_e = (_d = state === null || state === void 0 ? void 0 : state.locations) === null || _d === void 0 ? void 0 : _d.find) === null || _e === void 0 ? void 0 : _e.call(_d, (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 ((_f = job === null || job === void 0 ? void 0 : job.referral) === null || _f === void 0 ? void 0 : _f.id)
      job.referral =
        ((_g = state === null || state === void 0 ? void 0 : state.users) === null || _g === void 0 ? void 0 : _g[(_h = job === null || job === void 0 ? void 0 : job.referral) === null || _h === void 0 ? void 0 : _h.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 =
        ((_k = state === null || state === void 0 ? void 0 : state.users) === null || _k === void 0 ? void 0 : _k[(_l = job === null || job === void 0 ? void 0 : job.customer) === null || _l === void 0 ? void 0 : _l.id]) ||
          (await findUser(db, job.customer.id));
    if ((_m = job === null || job === void 0 ? void 0 : job.manager) === null || _m === void 0 ? void 0 : _m.id)
      job.manager =
        ((_o = state === null || state === void 0 ? void 0 : state.users) === null || _o === void 0 ? void 0 : _o[(_p = job === null || job === void 0 ? void 0 : job.manager) === null || _p === void 0 ? void 0 : _p.id]) ||
          (await findUser(db, job.manager.id));
    if ((_q = job === null || job === void 0 ? void 0 : job.success) === null || _q === void 0 ? void 0 : _q.id)
      job.success =
        ((_r = state === null || state === void 0 ? void 0 : state.users) === null || _r === void 0 ? void 0 : _r[(_s = job === null || job === void 0 ? void 0 : job.success) === null || _s === void 0 ? void 0 : _s.id]) ||
          (await findUser(db, job.success.id));
    jobs[index] = filterKeys ? pick(job, filterKeys) : job;
  }
  state.jobs = {
    ...((state === null || state === void 0 ? void 0 : state.jobs) || {}),
    ...jobs.reduce((acc, job) => ({ ...acc, [job.id]: job }), {}),
  };
  if (query === null || query === void 0 ? void 0 : query.length) {
    const fuse = new Fuse(jobs, {
      keys: [
        "title",
        "customer.firstName",
        "customer.lastName",
        "customer.email",
        "address.full",
        "tags",
        "referral.firstName",
        "referral.lastName",
        "referral.email",
        "manager.firstName",
        "manager.lastName",
        "manager.email",
        "location.name",
      ],
    });
    jobs = await fuse.search(query, { limit }).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  return jobs;
}
