import ActionTypes from '../constants/actionTypes';
import Immutable, { Map } from 'immutable';
import { getAuthObject } from '../utils/helpers.jsx';

/**
 * Reducer voor gebruikerdetails
 */
export const authenticateReducer = (state = Map({}), action) => {
  switch (action.type) {
    case ActionTypes.LOGIN_USER_PINCODE:
      return Map(action.auth);
    case ActionTypes.LOGIN_USER_ERROR:
      return Map(action.auth);
    case ActionTypes.LOGIN_USER_SUCCESS:
      return Map(action.auth);
    case ActionTypes.LOGOUT_USER:
      return Map(action.auth);
    default:
      let auth = getAuthObject();
      if(auth.data && auth.data.access_token) {
        return Map(auth);
      }

      return state;
  }
};

export const whoAmIReducer = (state = Map({}), action) => {
  switch(action.type) {
    case ActionTypes.LOGIN_CHECK_TOKEN_SUCCESS:
      return Map(action.me);
    default:
      return state;
  }
};
