/*
 * 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{Collapse as c}from"bootstrap";import f from"@typo3/core/security-utility.js";import u from"@typo3/backend/modal.js";import l from"@typo3/core/event/regular-event.js";import p from"@typo3/backend/severity.js";import{selector as d}from"@typo3/core/literals.js";import g from"@typo3/backend/storage/client.js";var n;(function(i){i.toggleSelector='[data-bs-toggle="flexform-inline"]',i.actionFieldSelector=".t3js-flex-control-action",i.controlSectionSelector=".t3js-formengine-irre-control",i.sectionContentContainerSelector=".t3js-flex-section-content",i.sectionContainerLabelSelector=".t3js-formengine-label",i.deleteContainerButtonSelector=".t3js-delete",i.contentPreviewSelector=".content-preview"})(n||(n={}));class s{constructor(e,t){this.securityUtility=new f,this.parentContainer=e,this.container=t,this.containerContent=t.querySelector(n.sectionContentContainerSelector),this.containerId=t.dataset.flexformContainerId,this.toggleKeyInLocalStorage=`formengine-flex-${e.getSectionContainer().id}-${this.containerId}-collapse`,this.panelHeading=t.querySelector(d`[data-bs-target="#flexform-container-${this.containerId}"]`),this.panelButton=this.panelHeading.querySelector(d`[aria-controls="flexform-container-${this.containerId}"]`),this.registerEvents()}static getCollapseInstance(e,t){return c.getInstance(e)??new c(e,{toggle:t})}getStatus(){return{id:this.containerId,collapsed:this.panelButton.getAttribute("aria-expanded")==="false"}}registerEvents(){this.parentContainer.isRestructuringAllowed()&&this.registerDelete(),this.registerPanelToggle(),this.registerToggle()}registerDelete(){new l("click",()=>{const e=TYPO3.lang["flexform.section.delete.title"]||"Delete this container?",t=TYPO3.lang["flexform.section.delete.message"]||"Are you sure you want to delete this container?",o=u.confirm(e,t,p.warning,[{text:TYPO3.lang["buttons.confirm.delete_record.no"]||"Cancel",active:!0,btnClass:"btn-default",name:"no"},{text:TYPO3.lang["buttons.confirm.delete_record.yes"]||"Yes, delete this container",btnClass:"btn-warning",name:"yes"}]);o.addEventListener("button.clicked",r=>{if(r.target.name==="yes"){const a=this.container.querySelector(n.actionFieldSelector);a.value="DELETE",this.container.appendChild(a),this.container.classList.add("t3-flex-section--deleted"),this.container.closest(".t3-form-field-container.t3-form-flex")?.querySelector(n.sectionContainerLabelSelector)?.classList.add("has-change"),new l("transitionend",()=>{this.container.classList.add("hidden");const h=new CustomEvent("formengine:flexform:container-deleted",{detail:{containerId:this.containerId}});this.parentContainer.getContainer().dispatchEvent(h)}).bindTo(this.container)}o.hideModal()})}).bindTo(this.container.querySelector(n.deleteContainerButtonSelector))}registerToggle(){const e=(g.get(this.toggleKeyInLocalStorage)??"1")==="1",t=s.getCollapseInstance(this.containerContent,!e);this.generatePreview(),new l("click",()=>{t.toggle()}).delegateTo(this.container,`${n.toggleSelector} .form-irre-header-cell:not(${n.controlSectionSelector}`)}registerPanelToggle(){["hide.bs.collapse","show.bs.collapse"].forEach(e=>{new l(e,t=>{const o=t.type==="hide.bs.collapse";g.set(this.toggleKeyInLocalStorage,o?"1":"0"),this.panelButton.setAttribute("aria-expanded",o?"false":"true"),this.panelButton.parentElement.classList.toggle("collapsed",o),this.generatePreview()}).bindTo(this.containerContent)})}generatePreview(){let e="";if(this.getStatus().collapsed){const t=this.containerContent.querySelectorAll('input[type="text"], textarea');for(const o of t){let r=this.securityUtility.stripHtml(o.value);r.length>50&&(r=r.substring(0,50)+"..."),e+=(e?" / ":"")+r}}this.panelHeading.querySelector(n.contentPreviewSelector).textContent=e}}export{s as default};
