import{Router as t}from"../../../../@vaadin/router/dist/vaadin-router.js";import{autorun as e}from"../../../../mobx/dist/mobx.esm.js";import{store as i}from"./haxcms-site-store.js";class HAXCMSSiteRouter extends HTMLElement{get baseURI(){return this.getAttribute("base-uri")}set baseURI(t){this.setAttribute("base-uri",t)}static get tag(){return"haxcms-site-router"}constructor(){super(),this.windowControllers=new AbortController,globalThis.HAXCMS.requestAvailability().storePieces.siteRouter=this;let o={};this.baseURI&&(o.baseUrl=this.baseURI),this.router=new t(this,o),this.__disposer=this.__disposer?this.__disposer:[],e((t=>{this._updateRouter(i.routerManifest),this.__disposer.push(t)})),globalThis.addEventListener("vaadin-router-location-changed",this._routerLocationChanged.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("haxcms-site-router-add",this.addRoutesEvent.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){for(var t in this.__disposer)this.__disposer[t].dispose();this.windowControllers.abort()}addRoutesEvent(t){this.addRoutes(t.detail)}addRoutes(t){this.router.addRoutes(t)}_updateRouter(t){if(!t||void 0===t.items)return;const e=t.items.map((t=>({path:t.slug,name:t.id,component:`fake-${t.id}-e`})));this.router.setRoutes([...e,{path:"/",component:"fake-home-e",name:"home"},{path:"/(.*)",component:"fake-404-e",name:"404"}])}lookupRoute(t=null){return!!(t&&i.routerManifest&&i.routerManifest.items&&i.routerManifest.items.filter((e=>e.slug===t)).length>0)&&i.routerManifest.items.filter((e=>e.slug===t))}_routerLocationChanged(t){if(globalThis.dispatchEvent(new CustomEvent("simple-modal-hide",{bubbles:!0,cancelable:!0,detail:{}})),globalThis.dispatchEvent(new CustomEvent("haxcms-toast-hide",{bubbles:!0,cancelable:!0,detail:{}})),globalThis.dispatchEvent(new CustomEvent("super-daemon-close",{bubbles:!0,cancelable:!0,detail:{}})),i.currentRouterLocation=t.detail.location,"404"===t.detail.location.route.name&&(t.detail.location.params[0].startsWith("files/")||t.detail.location.params[0].startsWith("assets/")))globalThis.location=t.detail.location.pathname;else if("home"===t.detail.location.route.name&&t.detail.location.search.startsWith("?p=/")&&this.lookupRoute(t.detail.location.search.replace("?p=/","").split("&")[0])){let e=this.lookupRoute(t.detail.location.search.replace("?p=/","").split("&")[0])[0];i.activeId=e.id,globalThis.history.replaceState({},null,t.detail.location.search.replace("?p=/","").split("&")[0]),t.detail.location.search=t.detail.location.search.replace("?p=/","").split("&")[0],t.detail.location.route.name=e.id,t.detail.location.route.path=t.detail.location.search,t.detail.location.route.component=`fake-${e.id}-e`,i.location=t.detail.location}else i.location=t.detail.location}}customElements.define(HAXCMSSiteRouter.tag,HAXCMSSiteRouter);export{HAXCMSSiteRouter};