/*
 *
 * LanguageProvider reducer
 *
 */

import { fromJS } from 'immutable';

import * as types from './constants';

const initialState = fromJS({
  locale: types.DEFAULT_LOCALE,
  languageLocale: types.DEFAULT_LOCALE,
  messages: {},
  localeLoading: false,
});

function languageProviderReducer(state = initialState, action) {
  switch (action.type) {
    case types.GET_LOCIZ_MESSAGE_REQUEST:
      return state
        .set('localeLoading', true);
    case types.GET_LOCIZ_MESSAGE_SUCCESS:
      return state
        .set('localeLoading', false)
        .set('languageLocale', action.locale)
        .set('messages', fromJS(action.data));
    case types.GET_LOCIZ_MESSAGE_FAILURE:
      return state.set('localeLoading', false);
    default:
      return state;
  }
}

export default languageProviderReducer;
