import { useContext } from "react";
import { ConfigContext } from "@/providers/ConfigProvider";
export function useConfig(){
    const context = useContext(ConfigContext);

    if(!context){

        throw new Error(
            "useConfig must be used inside ConfigProvider"
        );

    }
    const {countries,currencies,store} = context;
    const getCurrentCurrencyItem = () => {
        const currentCurrencyItem = currencies.find(currency => currency.code === store.currentCurrency);
        if(!currentCurrencyItem) {
            throw new Error(
                "Can't find current currency item"
            );
        }
        return currentCurrencyItem;
    }
    
    return {
        countries,
        currencies,
        store,
        getCurrentCurrencyItem
    };
}