import { NavigationState } from 'react-navigation';
import { Action, AuthActions } from '../actions';
import { RootNavigator, Routes } from '../navigation/';

const initialState = RootNavigator.router.getStateForAction(
  RootNavigator.router.getActionForPathAndParams(Routes.SPLASH), undefined,
);

export function navigationReducer(state: NavigationState = initialState, action: Action): NavigationState {
  switch (action.type) {
    case AuthActions.ActionKey.LOGOUT_SUCCESS:
    case AuthActions.ActionKey.LOGOUT_FAIL:
      return initialState;
    default:
      const nextState = RootNavigator.router.getStateForAction(action, state);

      return nextState || state;
  }
}
