import { Actions, ActionType } from './auth.action';

import { State, initialState } from './auth.state';

export function reducer(state: State = initialState, action: Actions): State {
  switch (action.type) {
    case ActionType.LoginProcessing: {
      return {
        ...state,
        processing: action.payload.processing
      };
    }

    case ActionType.LoginSuccess: {
      return {
        ...state,
        processing: false,
        logined: true,
        user: action.payload.user
      };
    }

    case ActionType.ChangeUser: {
      return {
        ...state,
        user: action.payload.user
      };
    }

    case ActionType.LogoutSuccess: {
      return {
        ...state,
        processing: false,
        logined: false,
        user: null
      };
    }

    default: {
      return state;
    }
  }
}
