/*
 * 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 r}from"@typo3/backend/utility/message-utility.js";import d from"@typo3/backend/storage/client.js";import s from"@typo3/core/event/regular-event.js";import o from"@typo3/backend/modal.js";class i{constructor(){new s("setup:confirmation:response",i.handleConfirmationResponse).delegateTo(document,'[data-event-name="setup:confirmation:response"]'),new s("click",(e,t)=>{const a=new CustomEvent(t.dataset.eventName,{bubbles:!0,detail:{payload:t.dataset.eventPayload}});t.dispatchEvent(a)}).delegateTo(document,'[data-event="click"][data-event-name]'),document.querySelectorAll("[data-setup-avatar-field]").forEach(e=>{const t=e.dataset.setupAvatarField,a=document.getElementById("clear_button_"+t),n=document.getElementById("add_button_"+t);n.addEventListener("click",()=>this.avatarOpenFileBrowser(n.dataset.setupAvatarUrl)),a?.addEventListener("click",()=>this.avatarClearExistingImage(t))}),document.querySelector("[data-setup-avatar-field]")!==null&&this.initializeMessageListener()}static handleConfirmationResponse(e){if(e.detail.result&&e.detail.payload==="resetConfiguration"){d.unsetByPrefix("");const t=document.querySelector("#setValuesToDefault");t.value="1",t.form.submit()}}static hideElement(e){e.style.display="none"}initializeMessageListener(){window.addEventListener("message",e=>{if(!r.verifyOrigin(e.origin))throw new Error("Denied message sent by "+e.origin);if(e.data.actionName==="typo3:foreignRelation:insert"){if(typeof e.data.objectGroup>"u")throw new Error("No object group defined for message");const t=e.data.objectGroup.match(/avatar-(.+)$/);if(t===null)return;this.avatarSetFileUid(t[1],e.data.uid)}})}avatarOpenFileBrowser(e){o.advanced({type:o.types.iframe,content:e,size:o.sizes.large})}avatarClearExistingImage(e){const t=document.getElementById("field_"+e),a=document.getElementById("image_"+e),n=document.getElementById("clear_button_"+e);n&&i.hideElement(n),a&&i.hideElement(a),t.value="delete"}avatarSetFileUid(e,t){this.avatarClearExistingImage(e);const a=document.getElementById("field_"+e),n=document.getElementById("add_button_"+e);a.value=t,n.classList.remove("btn-default"),n.classList.add("btn-info"),this.avatarWindowRef instanceof Window&&!this.avatarWindowRef.closed&&(this.avatarWindowRef.close(),this.avatarWindowRef=null)}}var l=new i;export{l as default};
