/*
 * 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 s}from"@typo3/backend/utility/message-utility.js";import a from"@typo3/core/document-service.js";import f from"@typo3/backend/modal.js";class d{constructor(){this.opener=null,this.fieldReference="",this.rte={parameters:"",configuration:""},this.irre={objectId:""},this.focusOpenerAndClose=()=>{this.getParent()&&this.getParent().focus(),f.dismiss(),close()},a.ready().then(()=>{const e=document.body.dataset;this.fieldReference=e.fieldReference,this.rte.parameters=e.rteParameters,this.rte.configuration=e.rteConfiguration,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(r=>{try{return typeof r.frameElement<"u"&&r.frameElement.classList.contains("t3js-modal-iframe")&&r.frameElement!==window.frames.frameElement}catch{return!1}});return 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}insertElement(e,t,r,n,i){if(this.irre.objectId){if(this.getParent()){const o={actionName:"typo3:foreignRelation:insert",objectGroup:this.irre.objectId,table:e,uid:t};s.send(o,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.rte.parameters&&!this.rte.configuration&&this.addElement(r,n||e+"_"+t,i),!1}addElement(e,t,r){if(this.getParent()){const n={actionName:"typo3:elementBrowser:elementAdded",fieldName:this.fieldReference,value:t,label:e};s.send(n,this.getParent()),r&&setTimeout(()=>this.focusOpenerAndClose(),0)}else alert("Error - reference to main window is not set properly!"),this.focusOpenerAndClose()}}var m=new d;export{m as default};
