import {OrderProduct} from "../../../types/order";
import {createContext, useContext, useState} from "react";

type ScanCrptContextState = {
    currentOrderProduct?: OrderProduct;
}

const ScanCrptContext = createContext<ScanCrptContextState>({});

const useScanCrptContextState = () => {
    const [currentOrderProduct, setCurrentOrderProduct] = useState<OrderProduct | undefined>(undefined);

    return {
        currentOrderProduct,
        setCurrentOrderProduct
    }
}
type ScanCrptContextProviderProps = {
    children: React.ReactNode;
}

export const ScanCrptContextProvider = ({children}: ScanCrptContextProviderProps) => {
    const state = useScanCrptContextState();
    return (
        <ScanCrptContext.Provider value={state}>
            {children}
        </ScanCrptContext.Provider>
    )
}

export const useScanCrptContext = () => {
    const context = useContext(ScanCrptContext);
    if (!context) {
        throw new Error(
            "useScanCrptContext must be used within a ScanCrptContextProvider"
        );
    }
    return context;
}