import Fuse from "fuse.js";
import pick from "../../helpers/pick";
export default async function listPins(db, { data: { tags, limit, orderBy, orderDirection, next, query, userId }, } = { data: {} }, filterKeys = ["href", "icon", "id", "name", "order"]) {
  var _a, _b;
  const collectionPath = "pins";
  const where = [];
  if (userId)
    where.push({
      key: "user",
      conditional: "==",
      value: await db.document("users", userId),
    });
  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(collectionPath, next));
  let pins = (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(pins, {
      keys: ["name"],
    });
    pins = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  if (!pins)
    return null;
  return pins.map((service) => filterKeys ? pick(service, filterKeys) : service);
}
//# sourceMappingURL=listPins.js.map