import ModalsActionTypes from "./modals.types";

const baseModal = {
  visible: false,
  context: {},
  actions: {
    refetch: null
  }
};

const INITIAL_STATE = {
  jobLineEdit: { ...baseModal },
  invoiceEnter: { ...baseModal }
};

const modalsReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case ModalsActionTypes.TOGGLE_MODAL_VISIBLE:
      return {
        ...state,
        [action.payload]: {
          ...state[action.payload],
          visible: !state[action.payload].visible
        }
      };
    case ModalsActionTypes.SET_MODAL_CONTEXT:
      return {
        ...state,
        [action.payload.modal]: {
          ...state[action.payload.modal],
          ...action.payload.context,
          visible: true
        }
      };
    default:
      return state;
  }
};

export default modalsReducer;
