import React, { useContext, useMemo } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { includeScores } from "./constants";
import { useTracking } from "~/tracking";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useScores } from "~/features/clientLibrary/useScores";
import useEffectOnce from "~/hooks/useEffectOnce";
const NeighborhoodHighlightsContext = React.createContext({});
const useNeighborhoodHighlights = () => useContext(NeighborhoodHighlightsContext);
const NeighborhoodHighlightsProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const { sdkState } = useStateManagerContext();
    const tracking = useTracking();
    useEffectOnce(() => {
        tracking.postEvent({
            eventType: TrackingPipelineEvents.SDK_VIEWED,
        });
        tracking.trackAnalyticsEvent({
            eventName: AnalyticsEvents.SDK_VIEWED,
        });
    });
    const { isLoading: isGeographyLoading, data: geographyData } = useGeographies({
        location: {
            lat: typeof ((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId) === "undefined"
                ? (_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.lat
                : undefined,
            lng: typeof ((_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.geographyId) === "undefined"
                ? (_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.lng
                : undefined,
        },
        externalId: (_e = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _e === void 0 ? void 0 : _e.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];
    }, [(_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.geographyId, geographyData]);
    const { data: scoresData, isFetching: isScoresFetching } = useScores({
        geographyId,
        location: {},
        include: includeScores,
    });
    const isLoading = useMemo(() => isGeographyLoading || isScoresFetching, [isGeographyLoading, isScoresFetching]);
    const contextValue = useMemo(() => {
        var _a, _b;
        return ({
            isLoading,
            title: (_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.title,
            scores: (_b = scoresData === null || scoresData === void 0 ? void 0 : scoresData.geographies[0]) === null || _b === void 0 ? void 0 : _b.scores,
        });
    }, [isLoading, scoresData, (_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.title]);
    return (React.createElement(NeighborhoodHighlightsContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodHighlights, NeighborhoodHighlightsProvider };
//# sourceMappingURL=context.js.map