import deepDiffer from 'react-native/lib/deepDiffer';
import { NavigationActions, StateUtils } from 'react-navigation';

export const getActiveRouteForState = navigationState => (
	navigationState.routes
		? getActiveRouteForState(navigationState.routes[navigationState.index])
		: navigationState
);

export const isEqualRoute = (route1, route2) => {
	if (route1.routeName !== route2.routeName) {
		return false;
	}

	return !deepDiffer(route1.params, route2.params);
};

const PATTERN_DRAWER_ROUTE_KEY = /^Drawer(Open|Close|Toggle)$/;
export const isDrawerRoute = route => PATTERN_DRAWER_ROUTE_KEY.test(route.routeName);

const withNavigationPreventDuplicate = (getStateForAction) => {
	const defaultGetStateForAction = getStateForAction;

	const getStateForActionWithoutDuplicates = (action, state) => {
		if (action.type === NavigationActions.NAVIGATE) {
			const previousRoute = getActiveRouteForState(StateUtils.back(state));
			const currentRoute = getActiveRouteForState(state);
			const nextRoute = action;

			if (isDrawerRoute(currentRoute) && isEqualRoute(previousRoute, nextRoute)) {
				return StateUtils.back(state); // Close drawer
			}

			if (isEqualRoute(currentRoute, nextRoute)) {
				return null;
			}
		}

		return defaultGetStateForAction(action, state);
	};

	return getStateForActionWithoutDuplicates;
};

export default withNavigationPreventDuplicate;

@@ -45,17 +45,16 @@
 pDiffer'
-;
 %0Aimport 
@@ -110,17 +110,16 @@
 igation'
-;
 %0A%0Aexport
@@ -290,17 +290,16 @@
 nState%0A)
-;
 %0A%0Aexport
@@ -402,17 +402,16 @@
 rn false
-;
 %0A%09%7D%0A%0A%09re
@@ -456,20 +456,480 @@
 .params)
-;%0A%7D;
+%0A%7D%0A%0Aexport const areDuplicateRoots = (routesStack, nextRouteName) =%3E %7B%0A%09if (routesStack.length %3E 1 && routesStack%5B0%5D.routes) %7B%0A%09%09let duplicateRootFlag = false%0A%09%09routesStack%5B0%5D.routes.forEach((route) =%3E %7B%0A%09%09%09// if next route being navigated to is a tab%0A%09%09%09// we should not render it because it will%0A%09%09%09// add another duplicate tabview to the stack%0A%09%09%09if (route.routeName === nextRouteName) duplicateRootFlag = true%0A%09%09%7D)%0A%09%09return duplicateRootFlag%0A%09%7D%0A%09return false%0A%7D
 %0A%0Aconst 
@@ -984,17 +984,16 @@
 oggle)$/
-;
 %0Aexport 
@@ -1069,17 +1069,16 @@
 uteName)
-;
 %0A%0Aconst 
@@ -1186,17 +1186,16 @@
 orAction
-;
 %0A%0A%09const
@@ -1376,17 +1376,16 @@
 (state))
-;
 %0A%09%09%09cons
@@ -1430,17 +1430,16 @@
 e(state)
-;
 %0A%09%09%09cons
@@ -1458,17 +1458,16 @@
 = action
-;
 %0A%0A%09%09%09if 
@@ -1467,16 +1467,21 @@
 %0A%09%09%09if (
+%0A%09%09%09%09
 isDrawer
@@ -1502,17 +1502,28 @@
 oute) &&
- 
+%0A%09%09%09%09(%0A%09%09%09%09%09
 isEqualR
@@ -1544,32 +1544,103 @@
 oute, nextRoute)
+ %7C%7C%0A%09%09%09%09%09areDuplicateRoots(state.routes, nextRoute.routeName)%0A%09%09%09%09)%0A%09%09%09
 ) %7B%0A%09%09%09%09return S
@@ -1660,17 +1660,16 @@
 k(state)
-;
  // Clos
@@ -1690,16 +1690,21 @@
 %0A%09%09%09if (
+%0A%09%09%09%09
 isEqualR
@@ -1732,16 +1732,80 @@
 xtRoute)
+ %7C%7C%0A%09%09%09%09areDuplicateRoots(state.routes, nextRoute.routeName)%0A%09%09%09
 ) %7B%0A%09%09%09%09
@@ -1815,17 +1815,16 @@
 urn null
-;
 %0A%09%09%09%7D%0A%09%09
@@ -1878,13 +1878,11 @@
 ate)
-;
 %0A%09%7D
-;
 %0A%0A%09r
@@ -1925,12 +1925,10 @@
 ates
-;
 %0A%7D
-;
 %0A%0Aex
@@ -1974,6 +1974,5 @@
 cate
-;
 %0A

Fix bug that would allow the home view to be loaded more than once