import { useQuery } from "@tanstack/react-query";
import errorTracker from "~/utils/errorTracker";
import { useStateManagerContext } from "~/context/stateManager";
import { transformNeighborhood } from "~/utils/transformNeighborhood";
const ENVIRONMENTS = {
    dev: "https://api-qa.locallogic.co/neighborhood-management-v1",
    qa: "https://api-qa.locallogic.co/neighborhood-management-v1",
    prod: "https://api.locallogic.co/neighborhood-management-v1",
};
export function useFavoriteNeighborhoods({ externalId, }) {
    var _a, _b;
    const { sdkState } = useStateManagerContext();
    const baseURL = ENVIRONMENTS[((_b = (_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.environment) === null || _b === void 0 ? void 0 : _b.api) || "prod"];
    const fetchMyNeighborhoods = () => fetch(`${baseURL}/sdk/favorite-neighborhoods/${externalId}?include=configurations`, {
        headers: {
            Accept: "application/json",
            Authorization: sdkState.apiKey,
            "x-ll-sdk-name": "neighborhood-featured",
        },
        method: "GET",
    })
        .then((response) => {
        if ([204, 404].includes(response.status)) {
            return null;
        }
        if (response.status >= 300) {
            throw new Error(`Request failed with status ${response.status}`);
        }
        return response.json();
    })
        .then((response) => {
        var _a, _b, _c;
        return ({
            featured: transformNeighborhood(((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en", response.featured, response.configurations[(_b = response.featured) === null || _b === void 0 ? void 0 : _b.geog_id]),
            favorites: (_c = response.favorites) === null || _c === void 0 ? void 0 : _c.map((neighborhood) => {
                var _a;
                return transformNeighborhood(((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en", neighborhood, response.configurations[neighborhood === null || neighborhood === void 0 ? void 0 : neighborhood.geog_id]);
            }),
        });
    })
        .catch((error) => {
        errorTracker.captureException(error);
        throw error;
    });
    return useQuery({
        queryKey: ["myNeighborhoods"],
        queryFn: fetchMyNeighborhoods,
        enabled: !!externalId && !!sdkState.apiKey,
    });
}
//# sourceMappingURL=useFavoriteNeighborhoods.js.map