import React, { useContext, useMemo } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { includeScores } from "./constants";
import { useTracking } from "~/tracking";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useProfiles } from "~/features/clientLibrary/useProfiles";
import { useScores } from "~/features/clientLibrary/useScores";
const NeighborhoodCharacteristicsContext = React.createContext({});
const useNeighborhoodCharacteristics = () => useContext(NeighborhoodCharacteristicsContext);
const NeighborhoodCharacteristicsProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    const tracking = useTracking();
    const { sdkState } = useStateManagerContext();
    const locale = ((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en";
    useEffectOnce(() => {
        tracking.postEvent({
            eventType: TrackingPipelineEvents.SDK_VIEWED,
        });
        tracking.trackAnalyticsEvent({
            eventName: AnalyticsEvents.SDK_VIEWED,
        });
    });
    const { isLoading: isGeographyLoading, data: geographyData } = useGeographies({
        location: {
            lat: typeof ((_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.geographyId) === "undefined"
                ? (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.lat
                : undefined,
            lng: typeof ((_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.geographyId) === "undefined"
                ? (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.lng
                : undefined,
        },
        externalId: (_f = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _f === void 0 ? void 0 : _f.externalId,
    });
    const geographyId = useMemo(() => {
        var _a;
        if (typeof ((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId) !== "undefined") {
            return sdkState.options.geographyId;
        }
        return Object.keys(geographyData || {})[0];
    }, [(_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.geographyId, geographyData]);
    const { data: profilesData, isFetching: isProfilesFetching } = useProfiles({
        geogID: geographyId,
        externalId: (_h = sdkState.globalOptions) === null || _h === void 0 ? void 0 : _h.externalId,
    });
    const { data: scoresData, isFetching: isScoresFetching } = useScores({
        geographyId,
        location: {},
        include: includeScores,
    });
    const isLoading = useMemo(() => isGeographyLoading || isProfilesFetching || isScoresFetching, [isGeographyLoading, isProfilesFetching, isScoresFetching]);
    const contextValue = useMemo(() => {
        var _a, _b, _c;
        return ({
            isLoading,
            title: (_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.title,
            profilesData,
            scores: (_b = scoresData === null || scoresData === void 0 ? void 0 : scoresData.geographies[0]) === null || _b === void 0 ? void 0 : _b.scores,
            locale,
            hideAttribution: (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.hideAttribution,
        });
    }, [
        isLoading,
        (_j = sdkState.options) === null || _j === void 0 ? void 0 : _j.title,
        profilesData,
        scoresData,
        locale,
        (_k = sdkState.options) === null || _k === void 0 ? void 0 : _k.hideAttribution,
    ]);
    return (React.createElement(NeighborhoodCharacteristicsContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodCharacteristics, NeighborhoodCharacteristicsProvider };
//# sourceMappingURL=context.js.map