import React, { useContext, useMemo } from "react";
import { TrackingPipelineEvents, AnalyticsEvents, } from "./types";
import { useTracking } from "~/tracking";
import useEffectOnce from "~/hooks/useEffectOnce";
import { useStateManagerContext } from "~/context/stateManager";
import { useGeographies } from "~/features/clientLibrary/useGeographies";
import { useMarketActivity } from "~/features/clientLibrary/useMarketActivity";
const NeighborhoodMarketTrendsContext = React.createContext({});
const useNeighborhoodMarketTrends = () => useContext(NeighborhoodMarketTrendsContext);
const NeighborhoodMarketTrendsProvider = ({ 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();
    const pastTwelveMonths = useMemo(() => {
        const months = [];
        const currentDate = new Date();
        // Loop through the past 12 months
        for (let index = 0; index <= 12; index += 1) {
            const year = currentDate.getFullYear();
            const month = currentDate.getMonth(); // 0-based, January is 0, December is 11
            // Format the month as yyyy-mm
            const formattedDate = `${year}-${String(month + 1).padStart(2, "0")}`;
            months.push(formattedDate);
            // Decrement the month
            currentDate.setMonth(currentDate.getMonth() - 1);
        }
        return months.reverse(); //
    }, []);
    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 { data: marketActivity, isFetching: isMarketActivityFetching } = useMarketActivity({
        geographyId,
        propertyTypes: ["all_residential_properties"],
        intervals: ["30d", "31-60d", "12m", "13-24m", ...pastTwelveMonths],
    });
    const homesSoldPercentage = useMemo(() => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        const soldHomesCount = ((_d = (_c = (_b = (_a = marketActivity === null || marketActivity === void 0 ? void 0 : marketActivity.market_activity_by_property_type) === null || _a === void 0 ? void 0 : _a.all_residential_properties.intervals["12m"]) === null || _b === void 0 ? void 0 : _b.market_activity) === null || _c === void 0 ? void 0 : _c.stats_over_period) === null || _d === void 0 ? void 0 : _d.sold_homes_count) || 0;
        const soldHomesWithPriceCount = ((_j = (_h = (_g = (_f = (_e = marketActivity === null || marketActivity === void 0 ? void 0 : marketActivity.market_activity_by_property_type) === null || _e === void 0 ? void 0 : _e.all_residential_properties.intervals["12m"]) === null || _f === void 0 ? void 0 : _f.market_activity) === null || _g === void 0 ? void 0 : _g.stats_over_period) === null || _h === void 0 ? void 0 : _h.home_selling_prices) === null || _j === void 0 ? void 0 : _j.sold_homes_with_sold_price_count) || 0;
        return soldHomesCount > 0 ? soldHomesWithPriceCount / soldHomesCount : 0;
    }, [marketActivity]);
    const isLoading = useMemo(() => isGeographyLoading || isMarketActivityFetching, [isGeographyLoading, isMarketActivityFetching]);
    const contextValue = useMemo(() => {
        var _a;
        return ({
            isLoading,
            homesSoldPercentage,
            marketActivity: marketActivity !== null && marketActivity !== void 0 ? marketActivity : null,
            appearance: (_a = sdkState === null || sdkState === void 0 ? void 0 : sdkState.options) === null || _a === void 0 ? void 0 : _a.appearance,
            locale,
            geography,
        });
    }, [
        marketActivity,
        isLoading,
        homesSoldPercentage,
        locale,
        geography,
        geographyId,
    ]);
    return (React.createElement(NeighborhoodMarketTrendsContext.Provider, { value: contextValue }, children));
};
export { useNeighborhoodMarketTrends, NeighborhoodMarketTrendsProvider };
//# sourceMappingURL=context.js.map