/*
 * 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 w}from"bootstrap";import"@typo3/install/renderable/clearable.js";import{AbstractInteractableModule as A}from"@typo3/install/module/abstract-interactable-module.js";import C from"@typo3/backend/modal.js";import m from"@typo3/backend/notification.js";import v from"@typo3/core/ajax/ajax-request.js";import h from"@typo3/install/router.js";import f from"@typo3/core/event/regular-event.js";import{KeyTypesEnum as T}from"@typo3/backend/enum/key-types.js";var l;(function(n){n.item=".t3js-localConfiguration-item",n.toggleAllTrigger=".t3js-localConfiguration-toggleAll",n.writeTrigger=".t3js-localConfiguration-write",n.searchTrigger=".t3js-localConfiguration-search",n.createRowTrigger=".t3js-localConfiguration-createRow",n.removeRowTrigger=".t3js-localConfiguration-removeRow",n.configurationItem=".t3js-localConfiguration-configuration-item"})(l||(l={}));class S extends A{constructor(){super(...arguments),this.toggleAll=!1}initialize(r){super.initialize(r),this.getContent(),new f("click",e=>{e.preventDefault(),this.write()}).delegateTo(r,l.writeTrigger),new f("click",()=>{const e=this.getModalBody(),o=e.querySelectorAll(".panel-collapse");this.toggleAll=!this.toggleAll;const a=this.toggleAll?"show":"hide";o.forEach(i=>{const t=e.querySelector(`[data-bs-target="#${i.id}"]`);t&&t.classList.toggle("collapsed",!this.toggleAll),w.getOrCreateInstance(i)[a]()})}).delegateTo(r,l.toggleAllTrigger),new f("keydown",e=>{const o=r.querySelector(l.searchTrigger);e.ctrlKey||e.metaKey?(e.key==="f"||e.key==="F")&&(e.preventDefault(),o.focus()):e.key===T.ESCAPE&&o.value.trim()!==""&&(e.preventDefault(),o.value="",o.focus())}).bindTo(r),new f("input",(e,o)=>{const a=o.value;this.search(a)}).delegateTo(r,l.searchTrigger),new f("change",(e,o)=>{const a=o.value;this.search(a)}).delegateTo(r,l.searchTrigger),new f("click",(e,o)=>{e.preventDefault(),o.closest(l.configurationItem)?.remove()}).delegateTo(r,l.removeRowTrigger),new f("click",(e,o)=>{e.preventDefault();const a=o.closest(".t3js-configuration-list");if(a===null)return;const i=g=>{const c=a.querySelectorAll(g);return c.item(c.length-1)},t=a.querySelectorAll(l.configurationItem);for(const g of t){const c=g.querySelectorAll('input[type="text"]');let p,d;if(a.dataset.type==="map")p=c.item(0).value.trim(),d=c.item(1).value.trim();else if(a.dataset.type==="element-list")p=void 0,d=c[0].value.trim();else return;if(p===""||d===""){for(const y of c)if(y.value===""){y.focus();return}}}const s=a.querySelector("#"+a.dataset.namespace).content.cloneNode(!0);a.querySelector(".configuration-map-item-collection").append(s),i(l.configurationItem).querySelector('input[type="text"]')?.focus()}).delegateTo(r,l.createRowTrigger)}search(r){this.currentModal.querySelectorAll(l.item).forEach(e=>{e.textContent.toLowerCase().trim().includes(r.toLowerCase())?(e.classList.remove("hidden"),e.classList.add("searchhit")):(e.classList.remove("searchhit"),e.classList.add("hidden"))}),this.currentModal.querySelectorAll(".searchhit").forEach(e=>{const o=e.closest(".panel-collapse");w.getOrCreateInstance(o).show()})}getContent(){const r=this.getModalBody();new v(h.getUrl("localConfigurationGetContent")).get({cache:"no-cache"}).then(async e=>{const o=await e.resolve();o.success===!0&&(r.innerHTML=o.html,C.setButtons(o.buttons),this.searchInput=r.querySelector(l.searchTrigger),this.searchInput.clearable())},e=>{h.handleAjaxError(e,r)})}write(){this.setModalButtonsState(!1);const r=this.getModalBody(),e=this.getModuleContent().dataset.localConfigurationWriteToken,o={},a={},i={};this.currentModal.querySelectorAll(".t3js-localConfiguration-pathValue").forEach(t=>{if(t.type==="checkbox")t.checked?o[t.dataset.path]="1":o[t.dataset.path]="0";else if(t.dataset.valuetype==="map"||t.dataset.valuetype==="element-list"){if(t.dataset.path.includes("/key[]")&&t.value!==""){const s=t.dataset.path.replace("/key[]","");a[s]===void 0&&(a[s]=[]),a[s].push(t.value)}else if(t.dataset.path.includes("/value[]")&&t.value!==""){const s=t.dataset.path.replace("/value[]","");i[s]===void 0&&(i[s]=[]),i[s].push(t.value)}}else o[t.dataset.path]=t.value});for(const t in i)if(Object.prototype.hasOwnProperty.call(i,t)){o[t]===void 0&&(o[t]={});const s=o[t];i[t].forEach((u,g)=>{if(u!=="")if(a[t]&&a[t][g]!==void 0&&a[t][g]!==""){const c=a[t][g];s[c]=u}else s[g]=u})}new v(h.getUrl()).post({install:{action:"localConfigurationWrite",token:e,configurationValues:o}}).then(async t=>{const s=await t.resolve();s.success===!0&&Array.isArray(s.status)?s.status.forEach(u=>{m.showMessage(u.title,u.message,u.severity)}):m.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{h.handleAjaxError(t,r)}).finally(()=>{this.setModalButtonsState(!0)})}}var k=new S;export{k as default};
