export default async function addUserToJob(db, { jobId, role, userId }) {
  var _a, _b, _c, _d, _e;
  if (!jobId)
    throw new Error("A jobId is required.");
  if (!userId)
    throw new Error("A userId is required.");
  const job = await db.find("jobs", jobId);
  const userIds = (job === null || job === void 0 ? void 0 : job.userIds) || [];
  const jobData = { updatedAt: new Date() };
  for (const uid of typeof userId === "string" ? [userId] : userId) {
    if (!userIds.includes(uid))
      userIds.push(uid);
    if (role && ((_b = (_a = job === null || job === void 0 ? void 0 : job.userIds) === null || _a === void 0 ? void 0 : _a.includes) === null || _b === void 0 ? void 0 : _b.call(_a, uid))) {
      if (((_c = job === null || job === void 0 ? void 0 : job.customer) === null || _c === void 0 ? void 0 : _c.id) === uid)
        jobData.customer = null;
      if (((_d = job === null || job === void 0 ? void 0 : job.manager) === null || _d === void 0 ? void 0 : _d.id) === uid)
        jobData.manager = null;
      if (((_e = job === null || job === void 0 ? void 0 : job.referral) === null || _e === void 0 ? void 0 : _e.id) === uid)
        jobData.referral = null;
    }
    if (role)
      jobData[role] = db.document("users", uid);
  }
  return db.update("jobs", jobId, { ...jobData, userIds });
}
//# sourceMappingURL=addUserToJob.js.map