/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */
import{LitElement as h,css as f,html as b}from"lit";import{property as a,query as y,customElement as g}from"lit/decorators.js";import{ModuleUtility as c}from"@typo3/backend/module.js";var l=function(d,e,t,o){var n=arguments.length,i=n<3?e:o===null?o=Object.getOwnPropertyDescriptor(e,t):o,r;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(d,e,t,o);else for(var m=d.length-1;m>=0;m--)(r=d[m])&&(i=(n<3?r(i):n>3?r(e,t,i):r(e,t))||i);return n>3&&i&&Object.defineProperty(e,t,i),i};const u="@typo3/backend/module/iframe",p=()=>!0;let s=class extends h{static{this.styles=f`:host{width:100%;min-height:100%;flex:1 0 auto;display:flex;flex-direction:row}::slotted(*){min-height:100%;width:100%}`}constructor(){super(),this.module="",this.endpoint="",this.sitenameFirst=!1,this.titleComponents=null,this.addEventListener("typo3-module-load",({target:e,detail:t})=>{const o=e.getAttribute("slot");this.pushState({slotName:o,detail:t})}),this.addEventListener("typo3-module-loaded",({detail:e})=>{this.updateBrowserState(e)}),this.addEventListener("typo3-iframe-load",({detail:e})=>{let t={slotName:u,detail:e};if(t.detail.url.includes(this.stateTrackerUrl+"?state=")){const o=t.detail.url.split("?state=");t=JSON.parse(decodeURIComponent(o[1]||"{}"))}this.slotElement.getAttribute("name")!==t.slotName&&this.slotElement.setAttribute("name",t.slotName),this.markActive(t.slotName,this.slotElement.getAttribute("name")===u?null:t.detail.url,!1),this.updateBrowserState(t.detail),this.parentElement.dispatchEvent(new CustomEvent("typo3-module-load",{bubbles:!0,composed:!0,detail:t.detail}))}),this.addEventListener("typo3-iframe-loaded",({detail:e})=>{this.updateBrowserState(e),this.parentElement.dispatchEvent(new CustomEvent("typo3-module-loaded",{bubbles:!0,composed:!0,detail:e}))})}static get observedAttributes(){return[...super.observedAttributes,"sitename-first"]}connectedCallback(){super.connectedCallback(),this.sitenameFirst=this.hasAttribute("sitename-first")}attributeChangedCallback(e,t,o){super.attributeChangedCallback(e,t,o),e==="sitename-first"&&(this.sitenameFirst=o!==null,this.updateBrowserTitle())}render(){const t=c.getFromName(this.module).component||u;return b`<slot name=${t}></slot>`}updated(){const t=c.getFromName(this.module).component||u;this.markActive(t,this.endpoint)}async markActive(e,t,o=!0){const n=await this.getModuleElement(e);t&&(o||n.getAttribute("endpoint")!==t)&&n.setAttribute("endpoint",t),n.hasAttribute("active")||n.setAttribute("active","");for(let i=n.previousElementSibling;i!==null;i=i.previousElementSibling)i.removeAttribute("active");for(let i=n.nextElementSibling;i!==null;i=i.nextElementSibling)i.removeAttribute("active")}async getModuleElement(e){let t=this.querySelector(`*[slot="${e}"]`);if(t!==null)return t;try{const o=await import(e+".js");if(t=this.querySelector(`*[slot="${e}"]`),t!==null)return t;if(!("componentName"in o))throw new Error(`module ${e} is missing the "componentName" export`);t=document.createElement(o.componentName)}catch(o){throw console.error({msg:`Error importing ${e} as backend module`,err:o}),o}return t.setAttribute("slot",e),this.appendChild(t),t}async pushState(e){const t=this.stateTrackerUrl+"?state="+encodeURIComponent(JSON.stringify(e));(await this.getModuleElement(u)).setAttribute("endpoint",t)}updateBrowserTitle(){let{titleComponents:e}=this;e!==null&&(this.sitenameFirst&&(e=e.toReversed()),document.title=e.join(" \xB7 "))}updateBrowserState(e){const t=new URL(e.url||"",window.location.origin),o=new URLSearchParams(t.search),n="title"in e?e.title:"";if(n!==null){const r=[this.sitename];n!==""&&r.unshift(n),this.titleComponents=r,this.updateBrowserTitle()}if(o.has("token"))o.delete("token"),t.search=o.toString();else if(o.has("install[controller]")){const r=o.get("install[controller]");t.pathname=this.entryPoint+"module/system/"+r,t.search=""}else return;const i=t.toString();window.history.replaceState(e,"",i)}};l([a({type:String,hasChanged:p})],s.prototype,"module",void 0),l([a({type:String,hasChanged:p})],s.prototype,"endpoint",void 0),l([a({type:String,attribute:"state-tracker"})],s.prototype,"stateTrackerUrl",void 0),l([a({type:String,attribute:"sitename"})],s.prototype,"sitename",void 0),l([a({type:String,attribute:"entry-point"})],s.prototype,"entryPoint",void 0),l([y("slot",!0)],s.prototype,"slotElement",void 0),s=l([g("typo3-backend-module-router")],s);export{s as ModuleRouter};
