import React, { useContext, useMemo } from "react";
import { allowedValueDrivers, allowedDriverLowQualifiers } from "./constants";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useTracking } from "~/tracking";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useStateManagerContext } from "~/context/stateManager";
import { usePropertyValueDrivers } from "~/features/clientLibrary/usePropertyValueDrivers";
import useEffectOnce from "~/hooks/useEffectOnce";
const NeighborhoodPropertyValueDriversContext = React.createContext({});
const useNeighborhoodPropertyValueDrivers = () => useContext(NeighborhoodPropertyValueDriversContext);
const NeighborhoodPropertyValueDriversProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e;
    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,
        },
        geogID: (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.geographyId,
    });
    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: propertyValueDrivers, isFetching: isPropertyValueDriversFetching, } = usePropertyValueDrivers({
        geographyId,
    });
    /**
     * Top 4, but only those allowed (translations are not provided for the others)
     */
    const filteredPropertyValueDrivers = useMemo(() => propertyValueDrivers === null || propertyValueDrivers === void 0 ? void 0 : propertyValueDrivers.filter((valueDriver) => (valueDriver.value_qualifier === "low" &&
        allowedDriverLowQualifiers.includes(valueDriver.name)) ||
        (valueDriver.value_qualifier !== "low" &&
            allowedValueDrivers.includes(valueDriver.name))).sort((a, b) => (b.value || 0) - (a.value || 0)).slice(0, 4), [propertyValueDrivers]);
    const isLoading = useMemo(() => isGeographyLoading || isPropertyValueDriversFetching, [isGeographyLoading, isPropertyValueDriversFetching]);
    const contextValue = useMemo(() => ({
        isLoading,
        geographyId,
        geography,
        propertyValueDrivers: filteredPropertyValueDrivers,
    }), [isLoading, geographyId, geography, filteredPropertyValueDrivers]);
    return (React.createElement(NeighborhoodPropertyValueDriversContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodPropertyValueDrivers, NeighborhoodPropertyValueDriversProvider, };
//# sourceMappingURL=context.js.map