var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { useQuery } from "@tanstack/react-query";
import { transformSuggestionsResponse } from "./transformSuggestionsResponse";
import useDebounce from "~/hooks/useDebounce";
const MAPBOX_ENDPOINT = "https://api.mapbox.com/search/searchbox/v1/forward?";
export function useAddressSearch({ location, country, locale = "en", query, }) {
    const debouncedQuery = useDebounce(query, 1250);
    const regionCode = country === "CAN" ? "CA" : "US";
    const options = {
        q: debouncedQuery,
        language: locale,
        proximity: `${location.lng},${location.lat}`,
        origin: `${location.lng},${location.lat}`,
        country: regionCode,
        types: "poi,address,street,neighborhood,locality,city,place",
        limit: "5",
        access_token: import.meta.env.VITE_MAPBOX_API_KEY,
    };
    /**
     * Fetches address suggestions from Mapbox Search API.
     *
     * @returns SuggestionsResponse
     */
    const fetchSuggestions = () => __awaiter(this, void 0, void 0, function* () {
        const response = yield fetch(MAPBOX_ENDPOINT + new URLSearchParams(options));
        return transformSuggestionsResponse(yield response.json());
    });
    return useQuery({
        queryKey: [useAddressSearch.name, debouncedQuery],
        queryFn: fetchSuggestions,
        enabled: !!debouncedQuery.length && debouncedQuery.length >= 4,
    });
}
//# sourceMappingURL=index.js.map