import { useMemo, useReducer } from 'react';
import { AppContext } from './AppContext';
import { AppContextProviderProps } from './AppContextProviderProps.interface';
import { AppContextProps } from './AppContextProps.interface';
import { AppContextState } from './AppContextState.interface';
import { reducer } from './reducer';

const AppContextProvider = (
  props: AppContextProviderProps
) => {
  const intialState: AppContextState = {
    featureFlags: [],
    userProfile: {}
  };
  const [state, dispatch] = useReducer(reducer, intialState);
  const contextValue: AppContextProps = useMemo(() => {
    return {
      state,
      dispatch
    };
  }, [state, dispatch]);

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

export default AppContextProvider;
