import React, { useContext, useEffect } from "react";
import { useLocalContent } from "~/sdks/LocalContent/context";
const RootElementContext = React.createContext({});
const useRootElement = () => useContext(RootElementContext);
const RootElementContextProvider = ({ children, }) => {
    const { activeScore, activeCategory, selectedPoiSubcategory, map, country, location, isCommuteCalculatorOpen, commuteData, commuteOptions, isCommuteDataLoading, distanceUnit = "metric", handleToggleIsCommuteCalculatorOpen, handleCalculateCommute, } = useLocalContent();
    const isCommuteCalculatorOpenRef = React.useRef(isCommuteCalculatorOpen);
    // Keep the ref updated with the latest value
    useEffect(() => {
        isCommuteCalculatorOpenRef.current = isCommuteCalculatorOpen;
    }, [isCommuteCalculatorOpen]);
    useEffect(() => {
        if (isCommuteCalculatorOpenRef.current) {
            handleToggleIsCommuteCalculatorOpen();
        }
    }, [activeScore, activeCategory, selectedPoiSubcategory]);
    return (React.createElement(RootElementContext.Provider, { value: {
            isOpen: isCommuteCalculatorOpen,
            commuteData,
            commuteOptions,
            isCommuteDataLoading,
            location: location,
            country: country === "CAN" ? "CAN" : "USA",
            mapProvider: map === null || map === void 0 ? void 0 : map.mapProvider,
            distanceUnit,
            onToggleIsOpen: handleToggleIsCommuteCalculatorOpen,
            handleCalculateCommute,
        } }, children));
};
export { useRootElement, RootElementContextProvider };
//# sourceMappingURL=context.js.map