import Fuse from "fuse.js";
export default async function listFeeds(db, { type, jobId, userId, next, limit, orderBy, orderDirection, query, myFeed, }) {
  var _a, _b;
  console.log(next);
  const collectionPath = "feeds";
  const where = [];
  if (jobId)
    where.push({
      key: "job",
      conditional: "==",
      value: db.document("jobs", jobId),
    });
  if (userId)
    where.push({
      key: "user",
      conditional: "==",
      value: db.document("users", userId),
    });
  if (type)
    where.push({
      key: "type",
      conditional: "==",
      value: type,
    });
  if (myFeed)
    where.push({
      key: "userIds",
      conditional: "array-contains",
      value: myFeed,
    });
  const startAfter = (next === null || next === void 0 ? void 0 : next.length) && (await db.getDocument(collectionPath, next));
  let feeds = (await db.list(collectionPath, where, `${orderBy || "createdAt"}:${orderDirection || "desc"}`, (query === null || query === void 0 ? void 0 : query.length) && 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,
  }));
  console.log(feeds);
  if (query === null || query === void 0 ? void 0 : query.length) {
    const fuse = new Fuse(feeds, {
      keys: ["html"],
    });
    feeds = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  if (!feeds)
    return null;
  return feeds;
}
