/* @flow */
import { REHYDRATE } from 'redux-persist/constants';

import type { NavigationState, Action } from '../types';
import { navigateToAccountPicker } from './navActions';
import { getFirstIfDeepEqual } from '../utils/immutability';
import { getStateForRoute, getInitialRoute } from './navSelectors';
import AppNavigator from './AppNavigator';
import {
  RESET_NAVIGATION,
  INITIAL_FETCH_COMPLETE,
  ACCOUNT_SWITCH,
  LOGIN_SUCCESS,
  LOGOUT,
} from '../actionConstants';

export default (
  state: NavigationState = getStateForRoute('loading'),
  action: Action,
): NavigationState => {
  switch (action.type) {
    case REHYDRATE:
      return getStateForRoute(getInitialRoute(action.payload));

    case RESET_NAVIGATION:
      return getStateForRoute(getInitialRoute(state));

    case ACCOUNT_SWITCH:
      return getStateForRoute('loading');

    case LOGIN_SUCCESS:
      return getStateForRoute('main');

    case INITIAL_FETCH_COMPLETE:
      return getFirstIfDeepEqual(
        state,
        getStateForRoute('main'),
        (a, b) => state.routes[0].routeName === 'main',
      );

    case LOGOUT:
      return AppNavigator.router.getStateForAction(navigateToAccountPicker(), state);

    default:
      return AppNavigator.router.getStateForAction(action, state);
  }
};

@@ -120,64 +120,8 @@
 s';%0A
-import %7B navigateToAccountPicker %7D from './navActions';%0A
 impo
@@ -1074,78 +1074,34 @@
 urn 
-AppNavigator.router.getStateForAction(navigateToAccountPicker(), state
+getStateForRoute('account'
 );%0A%0A

Reset navigation to AccountPickScreen on logout. (#1661)