import React, { useReducer } from 'react';
import { IAllRules, initialRulesState, RulesDispatchType, rulesReducer } from '../reducers/RulesReducer';

export interface IRulesStateContext {
    rulesState: IAllRules;
}
const initialStateContext: IRulesStateContext = {
    rulesState: initialRulesState
};
const initalDispatchContext: RulesDispatchType = null;

export const RulesStateContext = React.createContext(initialStateContext);
export const RulesDispatchContext = React.createContext(initalDispatchContext);

export function RulesContextProvider({ children }) {
  const [rulesState, dispatch] = useReducer(rulesReducer, initialRulesState);

  return (
    <RulesStateContext.Provider value={{rulesState}}>
      <RulesDispatchContext.Provider value={dispatch}>{children}</RulesDispatchContext.Provider>
    </RulesStateContext.Provider>
  );
}
