/*
 *
 * Ebill reducer
 *
 */

import { fromJS } from 'immutable';
import * as types from './constants';

const initialState = fromJS({
  createTemplateInProgress: false,
  createResponse: {},
});

function ebillReducer(state = initialState, action) {
  switch (action.type) {
    case types.CREATE_TEMPLATE_REQUEST:
      return state
        .set('createTemplateInProgress', true)
        .set('createTemplateError', false)
        .set('createTemplateErrorMessage', fromJS(""));
    case types.CREATE_TEMPLATE_SUCCESS:
      return state
        .set('createTemplateInProgress', false)
        .set('createResponse', action.data)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg))
        .set('createTemplateError', (action.statusCode !== undefined && action.statusCode > 300));
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.GET_TEMPLATE_DETAILS_REQUEST:
      return state
        .set('getTemplateDetailsInProgress', true);
    case types.GET_TEMPLATE_DETAILS_SUCCESS:
      return state
        .set('getTemplateDetailsInProgress', false)
        .set('templateDetails', fromJS(action.data));
    case types.GET_TEMPLATE_DETAILS_FAILURE:
      return state
        .set('getTemplateDetailsInProgress', false);
    case types.GET_CMS_EDITOR_DATA_REQUEST:
      return state
        .set('fetchingCmsData', true)
        .set('cmsData', '')
        .set('fetchingCmsDataFailed', false);
    case types.GET_CMS_EDITOR_DATA_SUCCESS:
      return state
        .set('fetchingCmsData', false)
        .set('cmsData', action.data);
    case types.GET_CMS_EDITOR_DATA_FAILURE:
      return state
        .set('fetchingCmsData', false)
        .set('fetchingCmsDataFailed', true);
    case types.CLEAR_ALL_VALUES:
      return state
        .set('createTemplateInProgress', false)
        .set('createResponse', fromJS({}))
        .set('createTemplateError', false)
        .set('fetchingCmsData', false)
        .set('cmsData', '')
        .set('fetchingCmsDataFailed', false);
    case types.CLEAR_EBILL_CRUD_RESPONSE_REQUEST:
      return state
        .set('createResponse', fromJS({}));
    default:
      return state;
  }
}

export default ebillReducer;
