import {createReducer} from 'redux-starter-kit';
import {
  CLEAR_ERROR,
  SET_ERROR_INFO
} from '../actions/error_actions';

export const errorReducer = createReducer({
  DisplayError: false,
  ErrorCritical: false,
  ErrorStack: [],
}, {
  [CLEAR_ERROR]: state => {
    const errorStack = (state.ErrorStack.length > 0) ? state.ErrorStack.slice(1) : [];
    return {
      ...state,
      DisplayError: (errorStack.length > 0),
      ErrorStack: errorStack,
    }
  },
  [SET_ERROR_INFO]: (state, action) => {
    const errorStack = [action.payload.msg, ...state.ErrorStack];
    return {
      ...state,
      DisplayError: true,
      ErrorCritical: state.ErrorCritical || action.payload.critical,
      ErrorStack: errorStack,
    }
  }
});