import {createContext, FC, useContext, useState} from "react";
import {DealSchema} from "../../../client";

type DealPageContextState = {
    selectedDeal?: DealSchema;
    setSelectedDeal: (deal: DealSchema | undefined) => void;
}

const DealPageContext = createContext<DealPageContextState | undefined>(undefined);
const useDealPageContextState = () => {
    const [selectedDeal, setSelectedDeal] = useState<DealSchema | undefined>(undefined);
    return {selectedDeal, setSelectedDeal};
}

type DealPageContextProviderProps = {
    children: React.ReactNode;
}

export const DealPageContextProvider: FC<DealPageContextProviderProps> = ({children}) => {
    const state = useDealPageContextState();
    return (
        <DealPageContext.Provider value={state}>
            {children}
        </DealPageContext.Provider>
    );
}

export const useDealPageContext = () => {
    const context = useContext(DealPageContext);
    if (!context) {
        throw new Error('useDealPageContext must be used within a DealPageContextProvider');
    }
    return context;
}