/*
 *
 * Create reducer
 *
 */

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

const initialState = fromJS({
  editTemplateInProgress: false,
  editResponse: {},
  getTemplateDetailsInProgress: false,
  templateDetails: {},
});

function editReducer(state = initialState, action) {
  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    case types.EDIT_TEMPLATE_REQUEST:
      return state
        .set('editTemplateInProgress', true)
        .set('editTemplateError', false)
        .set('editTemplateErrorMessage', fromJS(""));
    case types.EDIT_TEMPLATE_SUCCESS:
      return state.set('editTemplateInProgress', false)
        .set('editResponse', action.data)
        .set('editTemplateError', (action.statusCode !== undefined && action.statusCode > 300))
        .set('editTemplateErrorMessage', fromJS(action.errorMsg));
    case types.EDIT_TEMPLATE_FAILURE:
      return state
        .set('editTemplateInProgress', false)
        .set('editTemplateError', true)
        .set('editTemplateErrorMessage', 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', action.data);
    case types.GET_TEMPLATE_DETAILS_FAILURE:
      return state.set('getTemplateDetailsInProgress', false);
    case types.CLEAR_EDIT_RESPONSE_REQUEST:
      return state.set('editResponse', {});
    case types.RESET_EDIT_TEMPLATE:
      return state.set('templateDetails', {});
    default:
      return state;
  }
}

export default editReducer;
