/*
 * 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 b,html as f}from"lit";import{property as a,query as y,customElement as g}from"lit/decorators.js";import{ModuleUtility as p}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,s;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--)(s=d[m])&&(i=(n<3?s(i):n>3?s(e,t,i):s(e,t))||i);return n>3&&i&&Object.defineProperty(e,t,i),i};const u="@typo3/backend/module/iframe",c=()=>!0;let r=class extends h{static{this.styles=b`: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=p.getFromName(this.module).component||u;return f`<slot name=${t}></slot>`}updated(){const t=p.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 s=[this.sitename];n!==""&&s.unshift(n),this.titleComponents=s,this.updateBrowserTitle()}if(!o.has("token"))if(o.has("install[controller]")){const s=o.get("install[controller]");o.delete("install[controller]"),o.delete("install[context]"),o.delete("install[colorScheme]"),o.delete("install[theme]"),t.pathname=t.pathname.replace(this.installToolPath,this.entryPoint+"module/tools/"+s)}else return;o.delete("token"),t.search=o.toString();const i=t.toString();window.history.replaceState(e,"",i)}};l([a({type:String,hasChanged:c})],r.prototype,"module",void 0),l([a({type:String,hasChanged:c})],r.prototype,"endpoint",void 0),l([a({type:String,attribute:"state-tracker"})],r.prototype,"stateTrackerUrl",void 0),l([a({type:String,attribute:"sitename"})],r.prototype,"sitename",void 0),l([a({type:String,attribute:"entry-point"})],r.prototype,"entryPoint",void 0),l([a({type:String,attribute:"install-tool-path"})],r.prototype,"installToolPath",void 0),l([y("slot",!0)],r.prototype,"slotElement",void 0),r=l([g("typo3-backend-module-router")],r);export{r as ModuleRouter};
