/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/brindille-router@0.1.1/bundle.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var dush=_interopDefault(require("dush")),pathToRegexp=require("path-to-regexp"),Component=_interopDefault(require("brindille-component"));function matchRoute(e,t){return pathToRegexp.match(t)(e)}function safeAddTrailingSlash(e=""){if("string"!=typeof e)throw new Error("str should be a string and is a "+typeof e);return"/"+e.replace(/^\//,"")}function getRouteByPath(e,t=[],n=""){if(""!==n&&(e=e.replace(n,"").replace(/\/\//g,"/")),e=safeAddTrailingSlash(e),!Array.isArray(t))throw new Error("Routes param needs to be an array");if(!t.length)throw new Error("You need at lease one entry in routes param");const r=t.find((t=>{const n=matchRoute(e,t.path);return!!n&&(t.params=n.params,!0)}));return!!r&&Object.assign({},r)}function getRouteById(e,t=[]){if(!Array.isArray(t))throw new Error("Routes param needs to be an array");if(!t.length)throw new Error("You need at lease one entry in routes param");const n=t.find((t=>e===t.id));return!!n&&Object.assign({},n)}function parseRoutes(e){if(!Array.isArray(e))throw new Error("[Router] routes must be an array");return e.map((e=>{if("string"==typeof e)return{id:e,path:safeAddTrailingSlash(e)};if(!e||!e.id)throw new Error("[Router] routes must either be a string or an object with an id");return{id:e.id,path:safeAddTrailingSlash(e.path||e.id)}}))}const isSpecialKeypressed=e=>e.which>1||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey,isIgnoredLink=e=>e.hasAttribute("data-router-ignore"),isDownloadLink=e=>e.hasAttribute("download")||"external"===e.getAttribute("rel"),isBlank=e=>void 0!==e.target&&"_blank"===e.target,isDifferentOrigin=(e,t=window)=>e.protocol!==t.location.protocol||e.hostname!==t.location.hostname,isSameLocation=(e,t=window)=>e.pathname===t.location.pathname&&e.search===t.location.search,isMailto=e=>void 0!==e.href&&e.href.indexOf("mailto:")>-1,isLink=e=>e&&"A"===e.nodeName.toUpperCase(),getLink=e=>isLink(e)?e:e.parentNode?getLink(e.parentNode):null,getUrl=e=>safeAddTrailingSlash(e.pathname+e.search+(e.hash||""));function checkLink(e,t=window){let n=getLink(e.target);return!(!n||e.defaultPrevented||isSpecialKeypressed(e)||isDownloadLink(n)||isIgnoredLink(n)||isBlank(n)||isDifferentOrigin(n,t)||isMailto(n))&&(e.preventDefault(),!isSameLocation(n,t)&&getUrl(n))}function createRouter(e,t={},n=window){if(!e||!e._componentInstances)throw new Error("First param of createRouter needs to be an instance of brindille component");const r=e.findInstance("View");if(!r)throw new Error("There is no View instance in your brindille App");let o=!0,i=!1;const a=dush(),s=t.baseUrl||"",u=t.baseContent||"",l=t.getContent||(({route:e})=>Promise.resolve(e.id)),c=t.beforeCompile||(e=>Promise.resolve(e)),h=t.verbose&&!0===t.verbose,d=!(!t.notFoundHandler||"function"!=typeof t.notFoundHandler)&&t.notFoundHandler,p=parseRoutes(Array.isArray(t.routes)&&t.routes.length?t.routes:["home"]),f=p[0];let g=null,m=null;function w(e){let t=checkLink(e,n);t&&P(t)}function b(...e){h&&console.log("[Router]",...e)}function y(){b("stop"),n.removeEventListener("popstate",R),n.removeEventListener("click",w)}function P(e){n.history.pushState(null,null,e),R()}function R(){!function(e){let t=getRouteByPath(e,p,s);if(!t){if(d)return d(e);t=f}i=!0,m=g,g=Object.assign({isFirstRoute:o},t),a.emit("start",g),b("route:",g.id,(o?"(first) ":" ")+JSON.stringify(g.params)),o?(o=!1,r.showFirstPage().then(k)):l({route:g,base:u,path:e}).then((e=>{const t=r.showPage(e,c);return a.emit("loaded",g),t})).then(k)}(getUrl(n.location))}function k(){i=!1,a.emit("complete",g),a.emit("update",g)}return b('baseUrl = "'+s+'"'),b('baseContent = "'+u+'"'),p.forEach((e=>{b("registering route:",e.path)})),{start:function(){b("start"),n.addEventListener("popstate",R),n.addEventListener("click",w),R()},stop:y,goTo:P,goToId:function(e,t){let n=getRouteById(e,p);if(n){let e=n.path;t&&Object.keys(t).forEach((n=>{e=e.replace(":"+n,t[n])})),P(e)}},dispose:function(){a.off("update"),y()},get routes(){return p.slice(0)},get nbListeners(){return a._allEvents.update?a._allEvents.update.length:0},get baseUrl(){return s},get baseContent(){return u},get currentRoute(){return Object.assign({},g)},get previousRoute(){return Object.assign({},m)},get isTransitionning(){return i},on:(...e)=>a.on(...e),off:(...e)=>a.off(...e)}}function safeCallbackedCall(e,t,n){if(!n||"function"!=typeof n)throw new Error("The callback param should be a function");e&&e[t]&&"function"==typeof e[t]?e[t](n):n()}class View extends Component{constructor(e){super(e),this.showPage=this.showPage.bind(this),this.showFirstPage=this.showFirstPage.bind(this)}showFirstPage(){return new Promise(((e,t)=>{this.currentPage=this._componentInstances[0],safeCallbackedCall(this.currentPage,"transitionIn",(()=>{e(!0)}))}))}showPage(e,t){return this.content=e,this.createSection(e,t).then((e=>(this.currentPage=e,this.transitionOutAndAfterIn())))}transitionOutAndAfterIn(){return new Promise(((e,t)=>{safeCallbackedCall(this._componentInstances[this._componentInstances.length-1],"transitionOut",e)})).then((()=>this.transitionIn()))}transitionIn(){return new Promise(((e,t)=>{this.disposeChildren(),this.addNewPage(),safeCallbackedCall(this.currentPage,"transitionIn",e)}))}addNewPage(){this.currentPage.$el?(this._componentInstances.push(this.currentPage),this.$el.appendChild(this.currentPage.$el)):this.$el.appendChild(this.currentPage)}getCtor(e){return"function"==typeof this.definitions?this.definitions(e):"object"==typeof this.definitions?this.definitions[e]:null}createSection(e,t){let n=(this.window||window).document.createElement("div");if(n.innerHTML=e,3===n.firstChild.nodeType)return Promise.resolve(n);n=n.firstChild;let r=n.getAttribute("data-component"),o=this.getCtor(r);return n.removeAttribute("data-component"),t&&"function"==typeof t||(t=e=>Promise.resolve(e)),t(n).then((e=>{let t=new o(e);return t.init(this.definitions),t.componentName=r,t.parent=this,t}))}}exports.View=View,exports.createRouter=createRouter,exports.getRouteById=getRouteById,exports.getRouteByPath=getRouteByPath,exports.matchRoute=matchRoute;
//# sourceMappingURL=/sm/874431870c12d334d4424a0c61a5d6449a549f1884ff1b1a1f0d188bb116fb0d.map