import { Action, combineReducers, createReducer, on } from '@ngrx/store';
import { State, initialState } from './state';
import {
  loginSuccess,
  increaseLoginFailCount,
  initialLoginFailCount,
  logout,
  logoutInitialize
} from './actions';

export const reducer = createReducer(
  initialState,
  on(loginSuccess, (state, action) => {
    return {
      ...state,
      loginRes: action.loginRes
    };
  }),

  on(increaseLoginFailCount, (state, action) => {
    return {
      ...state,
      loginFailCount: state.loginFailCount + 1
    };
  }),

  on(initialLoginFailCount, (state, action) => {
    return {
      ...state,
      loginFailCount: 0
    };
  }),

  on(logoutInitialize, (state, action) => {
    return {
      ...initialState
    };
  })
);
