import React, { useContext, useMemo } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useTracking } from "~/tracking";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useLocation } from "~/hooks/useLocation";
import { useStateManagerContext } from "~/context/stateManager";
import { useDemographicsData } from "~/hooks/useDemographicsData";
const NeighborhoodDemographicsContext = React.createContext({});
const useNeighborhoodDemographics = () => useContext(NeighborhoodDemographicsContext);
const NeighborhoodDemographicsProvider = ({ 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,
        },
    });
    const geography = useMemo(() => {
        const geographiesArray = Object.values(geographyData || {});
        return geographiesArray[0];
    }, [isGeographyLoading, geographyData]);
    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];
    }, [(_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.geographyId, geographyData]);
    const { data: { country }, } = useLocation({
        lat: geography === null || geography === void 0 ? void 0 : geography.centroid.lat,
        lng: geography === null || geography === void 0 ? void 0 : geography.centroid.lng,
    });
    const { data: demographicsData, isFetching: isDemographicsFetching } = useDemographicsData({
        geographyId,
    });
    return (React.createElement(NeighborhoodDemographicsContext.Provider, { value: {
            isLoading: isGeographyLoading || isDemographicsFetching,
            demographicsData: demographicsData || {},
            country: country || "CA",
            locale: ((_f = sdkState.globalOptions) === null || _f === void 0 ? void 0 : _f.locale) || "en",
            hideAttribution: ((_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.hideAttribution) || false,
        } }, children));
};
export { useNeighborhoodDemographics, NeighborhoodDemographicsProvider };
//# sourceMappingURL=context.js.map