import React, { useContext, useState, useMemo, useCallback } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { MAX_POI_LIMIT } from "./constants";
import { useTracking } from "~/tracking";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useStateManagerContext } from "~/context/stateManager";
import errorTracker from "~/utils/errorTracker";
import { getIdFromMarker } from "~/utils/map/getIdFromMarker";
import { useMarkers } from "~/hooks/useMarkers";
const NeighborhoodMapContext = React.createContext({});
const useNeighborhoodMap = () => useContext(NeighborhoodMapContext);
const NeighborhoodMapProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
    const { sdkState } = useStateManagerContext();
    const locale = ((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en";
    const tracking = useTracking();
    const [activeMarker, setActiveMarker] = useState();
    const [activePoiCategory, setActivePoiCategory] = useState();
    useEffectOnce(() => {
        tracking.postEvent({
            eventType: TrackingPipelineEvents.SDK_VIEWED,
            eventAttributes: [
                {
                    label: "default_category",
                    data: activePoiCategory,
                },
            ],
        });
        tracking.trackAnalyticsEvent({
            eventName: AnalyticsEvents.SDK_VIEWED,
            eventAttributes: {
                ll_default_category: activePoiCategory,
            },
        });
    });
    const { isLoading: isGeographyLoading, data: geographyData } = useGeographies({
        geogID: (_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.geographyId,
        location: {
            lat: (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.lat,
            lng: (_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.lng,
        },
        include: "geometry",
    });
    const { geographyId, geography } = useMemo(() => {
        var _a, _b;
        const geographiesArray = Object.entries(geographyData || {});
        return {
            geographyId: (_a = geographiesArray[0]) === null || _a === void 0 ? void 0 : _a[0],
            geography: (_b = geographiesArray[0]) === null || _b === void 0 ? void 0 : _b[1],
        };
    }, [isGeographyLoading, geographyData]);
    const { data: markersDataRaw, isLoading: isMarkersLoading } = useMarkers({
        geogId: geographyId,
        limit: MAX_POI_LIMIT,
        categories: activePoiCategory ? [activePoiCategory] : undefined,
    });
    // Flatten the markers data into a single array
    const markersData = useMemo(() => [].concat(...Object.values(markersDataRaw || {})), [markersDataRaw]);
    const handleSelectPoiCategory = (category) => {
        errorTracker.addBreadCrumb("NeighborhoodMapProvider", "handleSelectPoiCategory", {
            category,
        });
        setActivePoiCategory(activePoiCategory === category ? undefined : category);
    };
    const handleMarkerClick = useCallback((marker) => {
        if ((activeMarker === null || activeMarker === void 0 ? void 0 : activeMarker.latitude) === (marker === null || marker === void 0 ? void 0 : marker.latitude) &&
            (activeMarker === null || activeMarker === void 0 ? void 0 : activeMarker.longitude) === (marker === null || marker === void 0 ? void 0 : marker.longitude)) {
            setActiveMarker(undefined);
            return;
        }
        setActiveMarker(marker);
    }, [activeMarker]);
    const activeMarkerDetails = useMemo(() => {
        if (!markersData || !activeMarker || activeMarker.markers.length !== 1) {
            return null;
        }
        return (markersData.find((marker) => activeMarker.markers[0].id ===
            getIdFromMarker(marker.lat, marker.lng, marker.name)) || null);
    }, [activeMarker, markersData]);
    const isLoading = useMemo(() => isGeographyLoading || isMarkersLoading, [isGeographyLoading, isMarkersLoading]);
    return (React.createElement(NeighborhoodMapContext.Provider, { value: {
            locale,
            isLoading,
            map: {
                mapProvider: (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.mapProvider,
                zoom: (_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.zoom,
                pitch: (_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.pitch,
                bearing: (_h = sdkState.options) === null || _h === void 0 ? void 0 : _h.bearing,
                cooperativeGestures: (_j = sdkState.options) === null || _j === void 0 ? void 0 : _j.cooperativeGestures,
            },
            geography,
            activeMarker,
            activeMarkerDetails,
            handleMarkerClick,
            activePoiCategory,
            onSelectPoiCategory: handleSelectPoiCategory,
            markersData,
            hideAttribution: (_k = sdkState.options) === null || _k === void 0 ? void 0 : _k.hideAttribution,
            showSchoolRatings: (_l = sdkState.options) === null || _l === void 0 ? void 0 : _l.showSchoolRatings,
        } }, children));
};
export { useNeighborhoodMap, NeighborhoodMapProvider };
//# sourceMappingURL=context.js.map