import { AUTH_USER, AUTH_ERROR, FETCH_USER } from './actions/actions-auth'

const INITIAL_STATE = {
  token: '',
  error: '',
  user: {},
  locale: 'en-US'
}

export default function auth(state = INITIAL_STATE, action) {
  switch (action.type) {
    case FETCH_USER:
      return {
        ...state,
        user: action.user,
        token: action.token
      }
    case AUTH_USER:
      return {
        ...state,
        token: action.token,
        error: ''
      }
    case AUTH_ERROR:
      return {
        ...state,
        error: action.error
      }
    case 'SET-LOCALE':
      return {
        ...state,
        locale: action.locale || 'en-US'
      }

    default:
      return state
  }
}
