import { __awaiter } from "tslib";
import getConfig from "next/config";
import React from "react";
import useSWR from "swr";
const { publicRuntimeConfig } = getConfig();
export const useMapboxAPI = (config) => {
    const fetcher = React.useMemo(() => mapboxAPIFetcher(config), []);
    const useSearch = (query, config) => {
        return useSWR([query, config], fetcher.search);
    };
    const useRetrive = (query) => {
        return useSWR([query, { limit: 1 }], fetcher.search);
    };
    return { useSearch, useRetrive };
};
export function mapboxAPIFetcher(config) {
    const baseEndpoint = (config === null || config === void 0 ? void 0 : config.endpoint) || "https://api.mapbox.com/geocoding/v5/mapbox.places";
    const search = (query, config) => __awaiter(this, void 0, void 0, function* () {
        if (!query)
            return;
        try {
            let params = "";
            Object.entries(config).forEach(([k, v]) => {
                params += "&" + k + "=" + v;
            });
            const endpoint = baseEndpoint +
                `/${query}.json?access_token=${publicRuntimeConfig.mapboxToken}${params}`;
            const res = yield fetch(endpoint);
            const data = yield res.json();
            return data;
        }
        catch (error) { }
    });
    return { search };
}
//# sourceMappingURL=useMapboxAPI.js.map