import { useQuery } from "@tanstack/react-query";
import { commuteCalculatorKeys } from "./index";
import errorTracker from "~/utils/errorTracker";
const DIRECTIONS_ENDPOINT = `${import.meta.env.VITE_PROVIDERS_ENDPOINT}/v1/directions`;
export function useCalculateRouteProxy({ transportMode, origin, destination, isOpen, }) {
    const options = {
        transportMode,
        origin,
        destination,
    };
    const transportTransform = transportMode === "car" ? "driving" : "walking";
    const fetchRoutes = () => fetch(`${DIRECTIONS_ENDPOINT}/${transportTransform}/${origin};${destination}?geometries=geojson`, {
        headers: {
            Accept: "application/json",
            Authorization: `${import.meta.env.VITE_API_KEY}`,
        },
        method: "GET",
    })
        .then((response) => response.json())
        .then((response) => {
        var _a, _b, _c;
        if (((_b = (_a = response.data) === null || _a === void 0 ? void 0 : _a.routes) !== null && _b !== void 0 ? _b : []).length > 0 &&
            ((_c = response.data) === null || _c === void 0 ? void 0 : _c.code) === "Ok") {
            return response.data.routes[0];
        }
        return [];
    })
        .catch((error) => {
        errorTracker.captureException(error);
    });
    return useQuery({
        queryKey: commuteCalculatorKeys.list(options),
        queryFn: fetchRoutes,
        enabled: !!options &&
            destination.indexOf("undefined") === -1 &&
            origin.indexOf("undefined") === -1 &&
            isOpen,
    });
}
//# sourceMappingURL=useCommute.js.map