import { createContext, useContext, useMemo } from 'react';
import { createSelectedProductStore, SelectedProductType } from '../../../store/simulation/useSimulationStore';

type ProductContextValue = {
    selectedProductStore: SelectedProductType,
};

const ProductContext = createContext<ProductContextValue | null>(null);

export function ProductProvider({
    children,
}: {
    readonly children: React.ReactNode;
}) {
    const selectedProductStore = useMemo(() => createSelectedProductStore(), []);

    const contextValue = useMemo(() => (
        {
            selectedProductStore
        }
    ), [selectedProductStore]);

    return (
        <ProductContext.Provider value={contextValue}>
            {children}
        </ProductContext.Provider>
    );
}

// Base hook to get the context
export function useProductContext() {
    const context = useContext(ProductContext);
    if (!context) {
        throw new Error('useProductContext must be used within a ProductProvider');
    }
    return context;
}