define(["exports","./../dom.js"],function(_exports,_dom){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;var prevContext,prevPageContext,base="",allRoutes=[];function page(path,routeInfo){routeInfo?(page.callbacks[path.toUpperCase()]=routeInfo,allRoutes.push(routeInfo)):page.start(path)}page.getRoutes=function(){return allRoutes},page.callbacks={},page.current="",page.base=function(path){if(0===arguments.length)return base;base=path};var loaded="complete"===document.readyState;function onpopstate(e){var path;loaded&&!function(event){event=event.state||{};return!1!==previousPopState.navigate?(previousPopState=event,!1):(previousPopState=event,!0)}(e)&&(e.state?(path=e.state.path,page.replace(path,e.state,null,!0)):page.show(location.pathname+location.hash,void 0,void 0,!1,!0))}function Context(path,state){var i=(path="/"===path[0]&&0!==path.indexOf(base)?base+"#!"+path:path).indexOf("?");this.canonicalPath=path,this.path=path.replace(base,"")||"/",this.path=this.path.replace("#!","")||"/",this.state=state||{},this.state.path=path,this.rawQueryString=~i?path.slice(i+1):"",this.params=this.rawQueryString?function(str){var params={};if("string"!=typeof str)return{};for(var entries=new URLSearchParams(str).entries(),result=entries.next();!result.done;){var pair=result.value;params[pair[0]]=pair[1],result=entries.next()}return params}(this.rawQueryString):{},this.pathname="string"!=typeof(state=~i?path.slice(0,i):path)?state:decodeURIComponent(state.replace(/\+/g," ")),this.hash=""}loaded||_dom.default.addEventListener(window,"load",function onWindowLoad(){setTimeout(function(){loaded=!0},0),_dom.default.removeEventListener(window,"load",onWindowLoad,{once:!0})},{once:!0}),page.start=function(options){var url;!1!==(options=options||{}).popstate&&window.addEventListener("popstate",onpopstate,!1),~location.hash.indexOf("#!")?(url=location.hash.substr(2),(options=location.href.toString()).indexOf("?")>=options.indexOf("#!")&&(url+=location.search)):url=location.pathname+location.search+location.hash,page.replace(url,null,!0)},page.show=function(path,state,dispatch,push,isBack){var promise,path=new Context(path,state);return path.isBack=isBack,page.current=path.path,!1!==dispatch&&(promise=page.dispatch(path)),!1!==path.handled&&!1!==push&&path.pushState(),promise||Promise.resolve()},page.restorePreviousState=function(){prevContext=prevPageContext,page.show(prevContext.pathname,prevContext.state,!1,!0,!1)},page.back=function(){history.back()},page.forward=function(){history.forward()},page.canGoBack=function(){return 1<history.length},page.replace=function(path,state,dispatch,isBack){path=new Context(path,state);return path.isBack=isBack,page.current=path.path,path.save(),!1!==dispatch&&page.dispatch(path),path},page.getRoute=function(path){var callbacks=page.callbacks,qsIndex=path.indexOf("?");return callbacks[(~qsIndex?path.slice(0,qsIndex):path).toUpperCase()]},page.createContext=function(path){return new Context(path)},page.dispatch=function(ctx){prevPageContext=prevContext,prevContext=ctx;var callbacks=page.callbacks,path=ctx.path,qsIndex=path.indexOf("?"),callbacks=callbacks[(~qsIndex?path.slice(0,qsIndex):path).toUpperCase()];if(callbacks)return page.handleRoute(ctx,callbacks)},Context.prototype.pushState=function(){history.pushState(this.state,"","/"!==this.path?"#!"+this.path:this.canonicalPath)},Context.prototype.save=function(){history.replaceState(this.state,"","/"!==this.path?"#!"+this.path:this.canonicalPath)};var previousPopState={};page.pushState=function(state,title,url){url="#!"+url,history.pushState(state,title,url),previousPopState=state},page.handleAnchorClick=function(e){var orig,el;1!==function(e){e=e||window.event;var which=e.which;null==which&&(which=e.button);return which}(e)||e.metaKey||e.ctrlKey||e.shiftKey||e.defaultPrevented||(el=(el=e.target)&&el.closest("A"))&&"A"===el.nodeName&&!el.hasAttribute("download")&&"external"!==el.getAttribute("rel")&&("#"===el.getAttribute("href")?e.preventDefault():!el.target&&function(href){var origin=location.protocol+"//"+location.hostname;location.port&&(origin+=":"+location.port);return href&&0===href.indexOf(origin)}(el.href)&&(orig=el=el.pathname+el.search+(el.hash||""),el=(el=0===el.indexOf(base)?el.substr(base.length):el).replace("#!",""),e.preventDefault(),page.show(orig)))};_exports.default=page});