var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.RootNavigation=void 0;exports.getActiveRouteName=getActiveRouteName;exports.setRootNavigation=void 0;exports.useBackButtonHandler=useBackButtonHandler;exports.useNavigationPersistence=useNavigationPersistence;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=require("react");var _reactNative=require("react-native");var RootNavigation=exports.RootNavigation={navigate:function navigate(name){name;},goBack:function goBack(){},resetRoot:function resetRoot(state){},getRootState:function getRootState(){return{};}};var setRootNavigation=exports.setRootNavigation=function setRootNavigation(ref){var _loop=function _loop(method){RootNavigation[method]=function(){if(ref.current){var _ref$current;return(_ref$current=ref.current)[method].apply(_ref$current,arguments);}};};for(var method in RootNavigation){_loop(method);}};function getActiveRouteName(state){var route=state.routes[state.index];if(!route.state)return route.name;return getActiveRouteName(route.state);}function useBackButtonHandler(ref,canExit){var canExitRef=(0,_react.useRef)(canExit);(0,_react.useEffect)(function(){canExitRef.current=canExit;},[canExit]);(0,_react.useEffect)(function(){var onBackPress=function onBackPress(){var navigation=ref.current;if(navigation==null){return false;}var routeName=getActiveRouteName(navigation.getRootState());if(canExitRef.current(routeName)){return false;}if(navigation.canGoBack()){navigation.goBack();return true;}return false;};_reactNative.BackHandler.addEventListener('hardwareBackPress',onBackPress);return function(){return _reactNative.BackHandler.removeEventListener('hardwareBackPress',onBackPress);};},[ref]);}function useNavigationPersistence(storage,persistenceKey){var _useState=(0,_react.useState)(),_useState2=(0,_slicedToArray2.default)(_useState,2),initialNavigationState=_useState2[0],setInitialNavigationState=_useState2[1];var _useState3=(0,_react.useState)(true),_useState4=(0,_slicedToArray2.default)(_useState3,2),isRestoringNavigationState=_useState4[0],setIsRestoringNavigationState=_useState4[1];var routeNameRef=(0,_react.useRef)();var onNavigationStateChange=function onNavigationStateChange(state){var previousRouteName=routeNameRef.current;var currentRouteName=getActiveRouteName(state);if(previousRouteName!==currentRouteName){__DEV__&&console.tron.log(currentRouteName);}routeNameRef.current=currentRouteName;storage.save(persistenceKey,state);};var restoreState=(0,_react.useCallback)(function _callee(){var state;return _regenerator.default.async(function(_context){while(1)switch(_context.prev=_context.next){case 0:_context.prev=0;_context.next=1;return _regenerator.default.awrap(storage.load(persistenceKey));case 1:state=_context.sent;if(state)setInitialNavigationState(state);case 2:_context.prev=2;setIsRestoringNavigationState(false);return _context.finish(2);case 3:case"end":return _context.stop();}},null,null,[[0,,2,3]],Promise);},[persistenceKey,storage]);(0,_react.useEffect)(function(){if(isRestoringNavigationState)restoreState();},[isRestoringNavigationState,restoreState]);return{onNavigationStateChange:onNavigationStateChange,restoreState:restoreState,initialNavigationState:initialNavigationState};}
//# sourceMappingURL=navigation-utilities.js.map