import React, { useReducer, createContext, useMemo } from "react";
const StoreContext = createContext();

const ACTIONS = {
  UPDATE: "u",
};

const initialState = {};

const reducer = (state, action) => {
  // Actions
  switch (action.type) {
    case ACTIONS.UPDATE:
      return { ...state, ...store };
    default:
      return state;
  }
};

export const StoreProvider = ({ children }) => {
  const [state, dispatch] = useReducer(reducer, initialState);

  const context = {
    state,
    dispatch,
    updateStore: (store) => dispatch(state, { type: ACTIONS.UPDATE, store }),
  };
  const contextValue = useMemo(() => context, [state, dispatch]);

  return (
    <StoreContext.Provider value={contextValue}>
      {children}
    </StoreContext.Provider>
  );
};

export default StoreContext;
