import { createContext, useReducer } from "react";

import closeModals from "../reducers/closeModal";
import handleModal from "../reducers/handleModal";

const GlobalContext = createContext();

export const GlobalContextProvider = (props) => {
  const openModalWithUrl = () => {
    return null;
  };

  let initialState = {
    modal: openModalWithUrl(),
  };

  const reducer = (state, action) => {
    switch (action.type) {
      case "HANDLE_MODAL":
        return handleModal(state, action);
      case "CLOSE_MODALS":
        return closeModals(state);

      default:
        return state;
    }
  };

  const [state, dispatch] = useReducer(reducer, initialState);

  return (
    <GlobalContext.Provider value={{ state, dispatch }}>
      {props.children}
    </GlobalContext.Provider>
  );
};

export default GlobalContext;
