import React, { useReducer, useMemo } from "react";
import PropTypes from "prop-types";
import { SensitiveInfoContext } from "./contexts";

const initialState = {
  enabled: false
};

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "onSensitiveInfoEnabled": {
      return {
        ...state,
        enabled: action.payload.enabled
      };
    }
    default: {
      return state;
    }
  }
};

const dispatchActions = dispatch => ({
  onSensitiveInfoEnabled: enabled => dispatch({ type: "onSensitiveInfoEnabled", payload: { enabled } })
});

const SensitiveInfoProvider = ({ children }) => {
  const [state, dispatch] = useReducer(reducer, initialState);
  const actions = useMemo(() => dispatchActions(dispatch), [dispatch]);

  return (
    <SensitiveInfoContext.Provider
      value={{
        state,
        actions
      }}
    >
      {children}
    </SensitiveInfoContext.Provider>
  );
};

SensitiveInfoProvider.propTypes = {
  children: PropTypes.node.isRequired
};

export { SensitiveInfoProvider };
export default SensitiveInfoProvider;
