import React, { useContext, useMemo, useEffect, useState } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useFilteredSchools } from "./hooks/useFilteredSchools";
import { useTracking } from "~/tracking";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useLocation } from "~/hooks/useLocation";
const NeighborhoodSchoolsContext = React.createContext({});
const useNeighborhoodSchools = () => useContext(NeighborhoodSchoolsContext);
const NeighborhoodSchoolsProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const { sdkState } = useStateManagerContext();
    const locale = ((_a = sdkState.globalOptions) === null || _a === void 0 ? void 0 : _a.locale) || "en";
    const tracking = useTracking();
    useEffectOnce(() => {
        tracking.postEvent({
            eventType: TrackingPipelineEvents.SDK_VIEWED,
        });
        tracking.trackAnalyticsEvent({
            eventName: AnalyticsEvents.SDK_VIEWED,
        });
    });
    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,
        },
    });
    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 center = useMemo(() => {
        var _a, _b, _c;
        if (!((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.geographyId)) {
            return {
                lat: ((_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.lat) || 0,
                lng: ((_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.lng) || 0,
            };
        }
        return (geography === null || geography === void 0 ? void 0 : geography.centroid) || { lat: 0, lng: 0 };
    }, [
        (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.geographyId,
        (_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.lat,
        (_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.lng,
        geography,
    ]);
    const { isLoading: isLocationLoading, data: location } = useLocation({
        lat: center.lat,
        lng: center.lng,
    });
    const [filterLevel, setFilterLevel] = useState();
    const [filterType, setFilterType] = useState();
    const { isLoading: isSchoolsLoading, filteredSchools, schoolTypes, schoolLevels, hasCategories, } = useFilteredSchools({
        geogId: geographyId,
        country: location.country,
        filterLevel,
        filterType,
    });
    const isLoading = useMemo(() => isSchoolsLoading || isLocationLoading, [isSchoolsLoading, isLocationLoading]);
    useEffect(() => {
        setFilterType((currentFilterType) => currentFilterType === undefined || schoolTypes.includes(currentFilterType)
            ? currentFilterType
            : undefined);
    }, [schoolTypes]);
    useEffect(() => {
        setFilterLevel((currentFilterLevel) => currentFilterLevel === undefined ||
            schoolLevels.includes(currentFilterLevel)
            ? currentFilterLevel
            : undefined);
    }, [schoolLevels]);
    const contextValue = useMemo(() => ({
        isLoading,
        filteredSchools,
        schoolTypes,
        schoolLevels,
        filterType,
        filterLevel,
        setFilterType,
        setFilterLevel,
        hasCategories,
        locale,
        location,
    }), [
        isLoading,
        filteredSchools,
        schoolTypes,
        schoolLevels,
        filterType,
        filterLevel,
        locale,
        hasCategories,
        location,
    ]);
    return (React.createElement(NeighborhoodSchoolsContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodSchools, NeighborhoodSchoolsProvider };
//# sourceMappingURL=context.js.map