import React, { useContext, useMemo } from "react";
import { useSimilarNeighborhoods } from "./hooks/useSimilarNeighborhoods";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useTracking } from "~/tracking";
import { TrackingPipelineEvents, AnalyticsEvents, } from "~/sdks/NeighborhoodMatch/types";
const NeighborhoodMatchContext = React.createContext({});
const useNeighborhoodMatch = () => useContext(NeighborhoodMatchContext);
const NeighborhoodMatchProvider = ({ children, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
    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({
        location: {
            lat: typeof ((_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.geographyId) === "undefined"
                ? (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.lat
                : undefined,
            lng: typeof ((_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.geographyId) === "undefined"
                ? (_e = sdkState.options) === null || _e === void 0 ? void 0 : _e.lng
                : undefined,
        },
        geogID: ((_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.geographyId) || undefined,
        externalId: (_g = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _g === void 0 ? void 0 : _g.externalId,
    });
    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 { isLoading: isSimilarNeighborhoodsLoading, isFetched: isSimilarNeighborhoodsFetched, similarNeighborhoods, hasAffordabilityIndex, neighborhoodName, limit, minimumLimit, hasMore, viewMore, viewLess, } = useSimilarNeighborhoods({
        geogID: geographyId,
        externalId: (_h = sdkState.globalOptions) === null || _h === void 0 ? void 0 : _h.externalId,
    });
    const isLoading = useMemo(() => isGeographyLoading || isSimilarNeighborhoodsLoading, [isGeographyLoading, isSimilarNeighborhoodsLoading]);
    const contextValue = useMemo(() => {
        var _a, _b, _c, _d;
        return ({
            title: (_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.title,
            limit,
            minimumLimit,
            hasMore,
            viewMore,
            viewLess,
            isLoading,
            isFetched: isSimilarNeighborhoodsFetched,
            neighborhoodName,
            similarNeighborhoods,
            hasAffordabilityIndex,
            hideAffordabilityTags: (_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.hideAffordabilityTags,
            hideAttribution: (_c = sdkState.options) === null || _c === void 0 ? void 0 : _c.hideAttribution,
            hideThumbnails: (_d = sdkState.options) === null || _d === void 0 ? void 0 : _d.hideThumbnails,
            locale,
            level: (geography === null || geography === void 0 ? void 0 : geography.level) || 10,
        });
    }, [
        (_j = sdkState.options) === null || _j === void 0 ? void 0 : _j.title,
        (_k = sdkState.options) === null || _k === void 0 ? void 0 : _k.hideAttribution,
        limit,
        hasMore,
        isLoading,
        isSimilarNeighborhoodsFetched,
        similarNeighborhoods,
        hasAffordabilityIndex,
        neighborhoodName,
        locale,
        geography,
    ]);
    return (React.createElement(NeighborhoodMatchContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodMatch, NeighborhoodMatchProvider };
//# sourceMappingURL=context.js.map