import algoliasearch from "algoliasearch";
import state from "../../store";
export default async function searchUsers({ query, limit, page, locationId, }, { algoliaId, algoliaKey, algoliaOpts, } = {}) {
  var _a, _b;
  const client = algoliasearch(algoliaId, algoliaKey);
  const index = client.initIndex("users");
  const searchResults = await index.search(query, {
    facetFilters: [...(locationId ? [`location:locations/${locationId}`] : [])],
    ...(algoliaOpts || {}),
    hitsPerPage: limit || 12,
    page,
  });
  return (((searchResults === null || searchResults === void 0 ? void 0 : searchResults.hits) &&
    ((_b = (_a = searchResults === null || searchResults === void 0 ? void 0 : searchResults.hits) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, (result) => {
      var _a, _b;
      return ({
        ...result,
        location: (_b = (_a = state === null || state === void 0 ? void 0 : state.locations) === null || _a === void 0 ? void 0 : _a.find) === null || _b === void 0 ? void 0 : _b.call(_a, (location) => (result === null || result === void 0 ? void 0 : result.location) === `locations/${location === null || location === void 0 ? void 0 : location.id}`),
      });
    }))) ||
    null);
}
