import React, { useContext, useMemo } from "react";
import DesktopFallback from "./assets/images/desktop_fallback.jpg";
import MobileFallback from "./assets/images/mobile_fallback.jpg";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useTracking } from "~/tracking";
import useEffectOnce from "~/hooks/useEffectOnce";
import useIsMobile from "~/hooks/useIsMobile";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useProfiles } from "~/features/clientLibrary/useProfiles";
const NeighborhoodHeroContext = React.createContext({});
const useNeighborhoodHero = () => useContext(NeighborhoodHeroContext);
const NeighborhoodHeroProvider = ({ 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 isMobile = useIsMobile();
    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,
        },
        externalId: (_e = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _e === void 0 ? void 0 : _e.externalId,
    });
    const { data: profilesData, isFetching: isProfilesFetching } = useProfiles({
        geogID: (_f = sdkState.options) === null || _f === void 0 ? void 0 : _f.geographyId,
        location: {
            lat: (_g = sdkState.options) === null || _g === void 0 ? void 0 : _g.lat,
            lng: (_h = sdkState.options) === null || _h === void 0 ? void 0 : _h.lng,
        },
        externalId: (_j = sdkState === null || sdkState === void 0 ? void 0 : sdkState.globalOptions) === null || _j === void 0 ? void 0 : _j.externalId,
    });
    const geography = useMemo(() => {
        const geographiesArray = Object.values(geographyData || {});
        return geographiesArray[0];
    }, [isGeographyLoading, geographyData]);
    const isLoading = useMemo(() => isGeographyLoading || isProfilesFetching, [isGeographyLoading, isProfilesFetching]);
    const neighborhoodImage = useMemo(() => {
        var _a, _b;
        const lowercaseOptionsImageValue = (((_a = sdkState.options) === null || _a === void 0 ? void 0 : _a.image) || "").toLocaleLowerCase();
        // Always hide the image if the option is set to "hide"
        if (lowercaseOptionsImageValue === "hide") {
            return null;
        }
        // If the geography has an image, use it by default
        if (typeof (geography === null || geography === void 0 ? void 0 : geography.image_url) === "string" &&
            (geography === null || geography === void 0 ? void 0 : geography.image_url.length) > 0) {
            return geography.image_url;
        }
        // If the image option is set to a string, use it as the image (unless the string is "noFallback" which is obviuosly not an image)
        if (typeof ((_b = sdkState.options) === null || _b === void 0 ? void 0 : _b.image) === "string" &&
            lowercaseOptionsImageValue !== "nofallback" &&
            lowercaseOptionsImageValue.substring(0, 4) === "http") {
            return sdkState.options.image;
        }
        // No fallback image
        if (lowercaseOptionsImageValue === "nofallback") {
            return null;
        }
        // Fallback image based on the device
        return isMobile ? MobileFallback : DesktopFallback;
    }, [geography === null || geography === void 0 ? void 0 : geography.image_url, (_k = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _k === void 0 ? void 0 : _k.image, isMobile]);
    const contextValue = useMemo(() => ({
        isLoading,
        locale,
        geography,
        isFallbackImage: neighborhoodImage !== null &&
            [MobileFallback, DesktopFallback].includes(neighborhoodImage),
        neighborhoodImage,
        neighborhoodSummary: profilesData === null || profilesData === void 0 ? void 0 : profilesData.profiles.summary,
    }), [
        isLoading,
        locale,
        geography,
        neighborhoodImage,
        (_l = profilesData === null || profilesData === void 0 ? void 0 : profilesData.profiles) === null || _l === void 0 ? void 0 : _l.summary,
        sdkState,
    ]);
    return (React.createElement(NeighborhoodHeroContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodHero, NeighborhoodHeroProvider };
//# sourceMappingURL=context.js.map