import _objectSpread from"@babel/runtime/helpers/objectSpread";import invariant from'./utils/invariant';var StateUtils={get:function get(state,key){return state.routes.find(function(route){return route.key===key;})||null;},indexOf:function indexOf(state,key){return state.routes.findIndex(function(route){return route.key===key;});},has:function has(state,key){return!!state.routes.some(function(route){return route.key===key;});},push:function push(state,route){invariant(StateUtils.indexOf(state,route.key)===-1,'should not push route with duplicated key %s',route.key);var routes=state.routes.slice();routes.push(route);return _objectSpread({},state,{index:routes.length-1,routes:routes});},pop:function pop(state){if(state.index<=0){return state;}var routes=state.routes.slice(0,-1);return _objectSpread({},state,{index:routes.length-1,routes:routes});},jumpToIndex:function jumpToIndex(state,index){if(index===state.index){return state;}invariant(!!state.routes[index],'invalid index %s to jump to',index);return _objectSpread({},state,{index:index});},jumpTo:function jumpTo(state,key){var index=StateUtils.indexOf(state,key);return StateUtils.jumpToIndex(state,index);},back:function back(state){var index=state.index-1;var route=state.routes[index];return route?StateUtils.jumpToIndex(state,index):state;},forward:function forward(state){var index=state.index+1;var route=state.routes[index];return route?StateUtils.jumpToIndex(state,index):state;},replaceAndPrune:function replaceAndPrune(state,key,route){var index=StateUtils.indexOf(state,key);var replaced=StateUtils.replaceAtIndex(state,index,route);return _objectSpread({},replaced,{routes:replaced.routes.slice(0,index+1)});},replaceAt:function replaceAt(state,key,route){var preserveIndex=arguments.length>3&&arguments[3]!==undefined?arguments[3]:false;var index=StateUtils.indexOf(state,key);var nextIndex=preserveIndex?state.index:index;var nextState=StateUtils.replaceAtIndex(state,index,route);nextState.index=nextIndex;return nextState;},replaceAtIndex:function replaceAtIndex(state,index,route){invariant(!!state.routes[index],'invalid index %s for replacing route %s',index,route.key);if(state.routes[index]===route&&index===state.index){return state;}var routes=state.routes.slice();routes[index]=route;return _objectSpread({},state,{index:index,routes:routes});},reset:function reset(state,routes,index){invariant(routes.length&&Array.isArray(routes),'invalid routes to replace');var nextIndex=index===undefined?routes.length-1:index;if(state.routes.length===routes.length&&state.index===nextIndex){var compare=function compare(route,ii){return routes[ii]===route;};if(state.routes.every(compare)){return state;}}invariant(!!routes[nextIndex],'invalid index %s to reset',nextIndex);return _objectSpread({},state,{index:nextIndex,routes:routes});}};export default StateUtils; //# sourceMappingURL=StateUtils.js.map