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

type VersionContextValue = {
    selectedVersionStore: SelectedVersionType,
};

const VersionContext = createContext<VersionContextValue | null>(null);

export function VersionProvider({
    children,
}: {
    readonly children: React.ReactNode;
}) {
    const selectedVersionStore = useMemo(() => createSelectedVersionStore(), []);

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

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

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