import Fuse from "fuse.js";
export default async function listServices(db, { data: { tags, limit, orderBy, orderDirection, next, query, tabs }, } = { data: {} }
// filterKeys = [
//   "name",
//   "autofill",
//   "code",
//   "costs",
//   "description",
//   "group",
//   "id",
//   "name",
//   "prices",
//   "referencePage",
//   "rental",
//   "smartScoping",
//   "tab",
//   "type",
//   "unit",
// ]
) {
  var _a, _b;
  console.log(next, tabs);
  const collectionPath = "services";
  const where = [];
  if (tabs) {
    where.push({
      key: "tab",
      conditional: "in",
      value: tabs
    });
  }
  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.find(collectionPath, next));
  console.log(startAfter);
  let services = (await db.list(collectionPath, where, `${orderBy || "name"}:${orderDirection || "asc"}`, 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[orderBy || "name"],
  }));
  if (query === null || query === void 0 ? void 0 : query.length) {
    const fuse = new Fuse(services, {
      keys: ["name", "description"],
    });
    services = await fuse.search(query).map((result) => result === null || result === void 0 ? void 0 : result.item);
  }
  if (!services)
    return null;
  return services;
}
