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

const reducer = (state, action) => {
    switch (action.type) {
        case "CHANGE_ACTIVE_TAB":
            return state.map((answer, index) => action.newValue === index ? {...answer, active: true} : {
                ...answer,
                active: false
            });
        case "CHANGE_LIST":
            return [...action.newList]
        default:
            return state;
    }
}
export const AnswersContext = createContext()
export const AnswersProvider = ({children}) => {
    const [openCallDialog, setOpenCallDialog] = useState(false)
    const [activeTab, setActiveTab] = useState(0)
    const [state, dispatch] = useReducer(reducer, []);

    return <AnswersContext.Provider
        value={{
            openCallDialog,
            setOpenCallDialog,
            state,
            dispatch,
            activeTab,
            setActiveTab
        }}>{children}</AnswersContext.Provider>
}
