/*
 *
 * Create reducer
 *
 */

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

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

function createReducer(state = initialState, action) {
  switch (action.type) {
    case types.DEFAULT_ACTION:
      return state;
    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('response', action.data)
        .set('createTemplateError', (action.statusCode !== undefined && action.statusCode > 300))
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CREATE_TEMPLATE_FAILURE:
      return state
        .set('createTemplateInProgress', false)
        .set('createTemplateError', true)
        .set('createTemplateErrorMessage', fromJS(action.errorMsg));
    case types.CLEAR_CREATE_RESPONSE_REQUEST:
      return state.set('response', {});
    case types.UPLOAD_ASSET_REQUEST:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', true);
    case types.UPLOAD_ASSET_SUCCESS:
      return state
        .set('uploadAssetSuccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
        .set('assetUploading', false)
        .set('uploadedAssetData', action.data);
    case types.UPLOAD_ASSET_FAILURE:
      return state
        .set('uploadAssetSuccess', false)
        .set('assetUploading', false);
    case types.CLEAR_ASSET:
      return state
        .delete('uploadedAssetData');
    case types.GET_IOS_CTAS:
      return state
        .set("getIosCtasSucccess", false)
        .set("iosCtasLoading", true);
    case types.GET_IOS_CTAS_SUCCESS:
      return state
        .set('getIosCtasSucccess', (action.statusCode !== undefined && action.statusCode !== '' && action.statusCode < 300))
        .set('iosCtasLoading', false)
        .set('iosCtasData', action.data);
    case types.GET_IOS_CTAS_FAILURE:
      return state
        .set('getIosCtasSucccess', false)
        .set('iosCtasLoading', false);
    case types.RESET_STORE:
      return state.delete('uploadedAssetData').delete('iosCtasData');
    default:
      return state;
  }
}

export default createReducer;
