import {createContext, useContext, useReducer} from "react";

const PubAffContext = createContext({
    next: [],
    c1: undefined,
    c2: undefined,
    showScore: true,
    timeCb: undefined,
    timeCb2: undefined,
    scoreRouge: 0,
    scoreBleu: 0
});
const PubAffDispatchContext = createContext(() => {
});

function reducer(state, action) {
    switch (action.type) {
        case 'SET_DATA':
            return {...state, ...action.payload}
        case 'CALL_TIME':
            if (state.timeCb)
                state.timeCb(action.payload)
            if (state.timeCb2)
                state.timeCb2(action.payload)
            return state
        case 'CLEAR_CB_TIME':
            return {...state, timeCb: undefined}
        default:
            return state
    }
}

export function PubAffProvider({children}) {
    const [state, dispatch] = useReducer(reducer, {})

    return <PubAffContext.Provider value={state}>
        <PubAffDispatchContext.Provider value={dispatch}>
            {children}
        </PubAffDispatchContext.Provider>
    </PubAffContext.Provider>
}

export function usePubAffState() {
    return useContext(PubAffContext)
}

export function usePubAffDispatch() {
    return useContext(PubAffDispatchContext)
}
