import Fuse from "fuse.js";
export default async function listPayments(db, { locationId, method, next, limit, orderBy, orderDirection, query, jobId, }) {
  var _a, _b;
  const collectionPath = "payments";
  const where = [];
  if (jobId)
    where.push({
      key: "job",
      conditional: "==",
      value: await db.document("jobs", jobId),
    });
  if (locationId)
    where.push({
      key: "location",
      conditional: "==",
      value: await db.document("locations", locationId),
    });
  if (method)
    where.push({
      key: "method",
      conditional: "==",
      value: method,
    });
  const startAfter = (next === null || next === void 0 ? void 0 : next.length) && (await db.getDocument(collectionPath, next));
  let payments = (await db.list(collectionPath, where, `${orderBy || "createdAt"}:${orderDirection || "desc"}`, limit, 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 (query === null || query === void 0 ? void 0 : query.length) {
    const fuse = new Fuse(payments, {
      keys: ["name"],
    });
    payments = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  if (!payments)
    return null;
  return payments;
}
//# sourceMappingURL=listPayment.js.map