/*
 * 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 d}from"bootstrap";import p from"@typo3/core/security-utility.js";import f from"@typo3/backend/modal.js";import s from"@typo3/core/event/regular-event.js";import u from"@typo3/backend/severity.js";import g from"@typo3/backend/storage/client.js";import m from"@typo3/backend/form-engine.js";import C from"~labels/core.core";import a from"~labels/backend.alt_doc";var n;(function(r){r.actionFieldSelector=".t3js-flex-control-action",r.sectionContentContainerSelector=".t3js-flex-section-content",r.sectionContainerLabelSelector=".t3js-formengine-label",r.deleteContainerButtonSelector=".t3js-delete",r.contentPreviewSelector=".content-preview"})(n||(n={}));class l{constructor(e,t){this.securityUtility=new p,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.panelButton=t.querySelector(":scope > .panel-heading .panel-button"),this.panelHeading=this.panelButton.closest(".panel-heading"),this.registerEvents()}static getCollapseInstance(e,t){return d.getInstance(e)??new d(e,{toggle:t})}getCollapseContent(){return this.containerContent}getStatus(){return{id:this.containerId,collapsed:this.panelButton.getAttribute("aria-expanded")==="false"}}async registerEvents(){this.parentContainer.isRestructuringAllowed()&&this.registerDelete(),this.registerPanelToggle(),await this.registerToggle(),this.registerPreviewUpdate()}registerDelete(){new s("click",()=>{const e=a.get("flexform.section.delete.title"),t=a.get("flexform.section.delete.message"),i=f.confirm(e,t,u.warning,[{text:a.get("buttons.confirm.delete_record.no"),active:!0,btnClass:"btn-default",name:"no"},{text:a.get("buttons.confirm.delete_record.yes"),btnClass:"btn-warning",name:"yes"}]);i.addEventListener("button.clicked",o=>{if(o.target.name==="yes"){const c=this.container.querySelector(n.actionFieldSelector);c.value="DELETE",this.container.appendChild(c),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 s("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)}i.hideModal()})}).bindTo(this.container.querySelector(n.deleteContainerButtonSelector))}async registerToggle(){const e=(g.get(this.toggleKeyInLocalStorage)??"1")==="1";l.getCollapseInstance(this.containerContent,!e),await m.ready(),this.generatePreview()}registerPanelToggle(){["hide.bs.collapse","show.bs.collapse"].forEach(e=>{new s(e,t=>{if(t.target!==this.containerContent)return;const i=t.type==="hide.bs.collapse";g.set(this.toggleKeyInLocalStorage,i?"1":"0")}).bindTo(this.containerContent)})}registerPreviewUpdate(){["input","change"].forEach(e=>{new s(e,()=>{this.generatePreview()}).delegateTo(this.containerContent,'input[type="text"], textarea')})}generatePreview(){let e="";const t=this.containerContent.querySelectorAll('input[type="text"], textarea');for(const i of t){let o=this.securityUtility.stripHtml(i.value);o.length>50&&(o=o.substring(0,50)+"..."),e+=(e?" / ":"")+o}e===""&&(e="["+C.get("labels.no_title")+"]"),this.panelHeading.querySelector(n.contentPreviewSelector).textContent=e}}export{l as default};
