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 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 useMyNeighborhoods() {
    var _a, _b, _c;
    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 = () => __awaiter(this, void 0, void 0, function* () {
        var _a;
        return fetch(`${baseURL}/sdk/my-neighborhoods/${(_a = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.externalId}?include=configurations&page_size=100`, {
            headers: {
                Accept: "application/json",
                Authorization: sdkState.apiKey,
                "x-ll-sdk-name": "neighborhood-showcase",
            },
            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;
            const neighborhoods = ((_a = response.neighborhoods) === null || _a === void 0 ? void 0 : _a.value) || [];
            return neighborhoods
                .map((neighborhood) => {
                var _a;
                return transformNeighborhood(((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en", neighborhood, response.configurations[neighborhood.geog_id]);
            })
                .sort((a, b) => a.name.localeCompare(b.name));
        })
            .catch((error) => {
            errorTracker.captureException(error);
        });
    });
    return useQuery({
        queryKey: ["myNeighborhoods"],
        queryFn: fetchMyNeighborhoods,
        enabled: !!((_c = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _c === void 0 ? void 0 : _c.externalId) && !!sdkState.apiKey,
    });
}
//# sourceMappingURL=useMyNeighborhoods.js.map