import React, { createContext, useContext, useReducer, Reducer } from 'react';

import { StateType } from '../store/initialState';

type IContextProps = [
  StateType,
  ({type}:{type:string}) => void,
]

export const StateContext = createContext({} as IContextProps);

export const StoreProvider = ({
  reducer,
  initialState,
  children,
}: {
  reducer: Reducer<any, any>;
  initialState: StateType;
  children: React.ReactNode;
}) => (
  <StateContext.Provider
    value={useReducer(reducer, initialState)}
    children={children}
  />
);

export const useStore = () => useContext(StateContext);
