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

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

const initialState = {
  intervals: [],
  failing: [],
  regions: [],
  focusJob: false,
  simplifiedControls: false,
  defaultRegion: "us", // Local Store
  defaultPage: "failing", // Local Store
};

const reducer = (state, action) => {
  const { store } = 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({ type: ACTIONS.UPDATE, store }),
  };
  const contextValue = useMemo(() => context, [state, dispatch]);

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

export default StoreContext;
