/*
 * 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{MessageUtility as o}from"@typo3/backend/utility/message-utility.js";import a from"@typo3/core/document-service.js";import d from"@typo3/backend/modal.js";class f{constructor(){this.opener=null,this.fieldReference="",this.irre={objectId:""},this.focusOpenerAndClose=()=>{this.getParent()&&this.getParent().focus(),d.dismiss(),close()},a.ready().then(()=>{const e=document.body.dataset;this.fieldReference=e.fieldReference,this.irre.objectId=e.irreObjectId})}getParent(){const e=typeof window.frames<"u"&&typeof window.frames.frameElement<"u"&&window.frames.frameElement.classList.contains("t3js-modal-iframe"),t=Array.from(top.frames||[]).filter(n=>{try{return typeof n.frameElement<"u"&&n.frameElement.classList.contains("t3js-modal-iframe")&&n.frameElement!==window.frames.frameElement}catch{return!1}});if(this.opener===null&&(e&&t.length>0?this.opener=t.pop():typeof window.parent<"u"&&typeof window.parent.document.list_frame<"u"&&window.parent.document.list_frame.parent.document.querySelector(".t3js-modal-iframe")!==null?this.opener=window.parent.document.list_frame:typeof window.parent<"u"&&typeof window.parent.frames.list_frame<"u"&&window.parent.frames.list_frame.parent.document.querySelector(".t3js-modal-iframe")!==null?this.opener=window.parent.frames.list_frame:typeof window.frames<"u"&&typeof window.frames.frameElement<"u"&&window.frames.frameElement!==null&&window.frames.frameElement.classList.contains("t3js-modal-iframe")?this.opener=window.frames.frameElement.contentWindow.parent:window.opener&&(this.opener=window.opener),this.opener&&!this.windowHasEditForm(this.opener))){const n=this.findEditFormWindow();n&&(this.opener=n)}return this.opener}insertElement(e,t,n,r,i){if(this.irre.objectId){if(this.getParent()){const s={actionName:"typo3:foreignRelation:insert",objectGroup:this.irre.objectId,table:e,uid:t};o.send(s,this.getParent())}else alert("Error - reference to main window is not set properly!"),this.focusOpenerAndClose();return i&&setTimeout(()=>this.focusOpenerAndClose(),0),!0}return this.fieldReference&&this.addElement(n,r||e+"_"+t,i),!1}windowHasEditForm(e){return e?.document?.querySelector('form[name="editform"]')!==null}findEditFormWindow(){try{for(let e=0;e<top.frames.length;e++)try{const t=top.frames[e];if(t!==window&&this.windowHasEditForm(t))return t}catch{}}catch{}return null}addElement(e,t,n){const r={actionName:"typo3:elementBrowser:elementAdded",fieldName:this.fieldReference,value:t,label:e,close:n};if(document.body.dataset.useEvents==="true"){this.dispatch(r);return}this.getParent()?(o.send(r,this.getParent()),n&&setTimeout(()=>this.focusOpenerAndClose(),0)):(alert("Error - reference to main window is not set properly!"),this.focusOpenerAndClose())}dispatch(e){window.frameElement.dispatchEvent(new CustomEvent("typo3:element-browser:message",{bubbles:!0,detail:e}))}}var m=new f;export{m as default};
