/*
 * 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 s,nothing as m,html as u}from"lit";import{property as f,query as p,customElement as h}from"lit/decorators.js";import g from"~labels/core.core";var c=function(i,e,t,o){var a=arguments.length,n=a<3?e:o===null?o=Object.getOwnPropertyDescriptor(e,t):o,l;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(i,e,t,o);else for(var d=i.length-1;d>=0;d--)(l=i[d])&&(n=(a<3?l(n):a>3?l(e,t,n):l(e,t))||n);return a>3&&n&&Object.defineProperty(e,t,n),n};const b="typo3-iframe-module";let r=class extends s{constructor(){super(...arguments),this.endpoint=""}attributeChangedCallback(e,t,o){super.attributeChangedCallback(e,t,o),e==="endpoint"&&o===t&&this.iframe.setAttribute("src",o)}connectedCallback(){super.connectedCallback(),this.endpoint&&this.dispatch("typo3-iframe-load",{url:this.endpoint,title:null})}createRenderRoot(){return this}render(){return this.endpoint?u`<iframe src=${this.endpoint} name=list_frame id=typo3-contentIframe class="scaffold-content-module-iframe t3js-scaffold-content-module-iframe" title=${g.get("iframe.listFrame")} @load=${this._loaded}></iframe>`:m}registerPagehideHandler(e){try{e.contentWindow.addEventListener("pagehide",t=>this._pagehide(t,e),{once:!0})}catch(t){throw console.error("Failed to access contentWindow of module iframe \u2013 using a foreign origin?"),t}}retrieveModuleStateFromIFrame(e){try{return{url:e.contentWindow.location.href,title:e.contentDocument.title,module:e.contentDocument.body.querySelector(".module[data-module-name]")?.getAttribute("data-module-name")}}catch{return console.error("Failed to access contentWindow of module iframe \u2013 using a foreign origin?"),{url:this.endpoint,title:null}}}_loaded({target:e}){const t=e;this.registerPagehideHandler(t);const o=this.retrieveModuleStateFromIFrame(t);this.dispatch("typo3-iframe-loaded",o)}_pagehide(e,t){new Promise(o=>window.setTimeout(o,0)).then(()=>{t.contentWindow!==null&&this.dispatch("typo3-iframe-load",{url:t.contentWindow.location.href,title:null})})}dispatch(e,t){this.dispatchEvent(new CustomEvent(e,{detail:t,bubbles:!0,composed:!0}))}};c([f({type:String})],r.prototype,"endpoint",void 0),c([p("iframe",!0)],r.prototype,"iframe",void 0),r=c([h("typo3-iframe-module")],r);export{r as IframeModuleElement,b as componentName};
