/*
 * 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!
 */
var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};define(["require","exports","TYPO3/CMS/Backend/Modal","TYPO3/CMS/Backend/Notification","TYPO3/CMS/Core/Event/RegularEvent","TYPO3/CMS/Core/Event/DebounceEvent","TYPO3/CMS/Core/Ajax/AjaxRequest"],(function(e,t,o,a,i,n,r){"use strict";var s,l;Object.defineProperty(t,"__esModule",{value:!0}),t.NewContentElementWizard=void 0,r=__importDefault(r),function(e){e.wizardWindow="t3-new-content-element-wizard-window"}(s||(s={})),function(e){e.modalBodySelector=".t3js-modal-body",e.modalTabsSelector=".t3js-tabs",e.elementsFilterSelector=".t3js-contentWizard-search",e.noResultSelector=".t3js-filter-noresult",e.wizardWindowSelector=".t3-new-content-element-wizard-window",e.wizardElementSelector=".t3js-media-new-content-element-wizard",e.wizardElementWithTargetSelector="button[data-target]",e.wizardElementWithPositionMapArugmentsSelector="button[data-position-map-arguments]"}(l||(l={}));class d{constructor(e){var t;this.modal=e,this.elementsFilter=this.modal.querySelector(l.elementsFilterSelector),null===(t=this.modal.querySelector(l.modalBodySelector))||void 0===t||t.classList.add(s.wizardWindow),this.registerEvents()}static getTabIdentifier(e){const t=e.querySelector("a"),[,o]=t.href.split("#");return o}static countVisibleContentElements(e){return e.querySelectorAll(l.wizardElementSelector+":not(.hidden)").length}registerEvents(){new i("shown.bs.modal",()=>{this.elementsFilter.focus()}).bindTo(this.modal),new i("keydown",e=>{const t=e.target;"Escape"===e.code&&(e.stopImmediatePropagation(),t.value="")}).bindTo(this.elementsFilter),new n("keyup",e=>{this.filterElements(e.target)},150).bindTo(this.elementsFilter),new i("search",e=>{this.filterElements(e.target)}).bindTo(this.elementsFilter),new i("click",e=>{e.preventDefault(),e.stopPropagation()}).delegateTo(this.modal,[l.modalTabsSelector,".disabled"].join(" ")),new i("click",(e,t)=>{e.preventDefault();const a=t.dataset.target;a&&(o.dismiss(),top.list_frame.location.href=a)}).delegateTo(this.modal,[l.wizardWindowSelector,l.wizardElementWithTargetSelector].join(" ")),new i("click",(e,t)=>{if(e.preventDefault(),!t.dataset.positionMapArguments)return;const o=JSON.parse(t.dataset.positionMapArguments);o.url&&new r.default(o.url).post({defVals:o.defVals,saveAndClose:o.saveAndClose?"1":"0"}).then(async e=>{this.modal.querySelector(l.wizardWindowSelector).innerHTML=await e.raw().text()}).catch(()=>{a.error("Could not load module data")})}).delegateTo(this.modal,[l.wizardWindowSelector,l.wizardElementWithPositionMapArugmentsSelector].join(" "))}filterElements(e){const t=this.modal.querySelector(l.modalTabsSelector),o=this.modal.querySelector(l.noResultSelector);this.modal.querySelectorAll(l.wizardElementSelector).forEach(t=>{const o=t.textContent.trim().replace(/\s+/g," ");t.classList.toggle("hidden",""!==e.value&&!RegExp(e.value,"i").test(o))});const a=d.countVisibleContentElements(this.modal);t.parentElement.classList.toggle("hidden",0===a),o.classList.toggle("hidden",a>0),this.switchTabIfNecessary(t)}switchTabIfNecessary(e){const t=e.querySelector(".active").parentElement,o=Array.from(t.parentElement.children);for(let e of o){const t=d.getTabIdentifier(e),o=e.querySelector("a");o.classList.toggle("disabled",!this.hasTabContent(t)),o.classList.contains("disabled")?o.setAttribute("tabindex","-1"):o.removeAttribute("tabindex")}if(!this.hasTabContent(d.getTabIdentifier(t)))for(let a of o){if(a===t)continue;const o=d.getTabIdentifier(a);if(this.hasTabContent(o)){this.switchTab(e.parentElement,o);break}}}hasTabContent(e){const t=this.modal.querySelector("#"+e);return d.countVisibleContentElements(t)>0}switchTab(e,t){const o=e.querySelector(`a[href="#${t}"]`),a=this.modal.querySelector("#"+t);e.querySelector("a.active").classList.remove("active"),e.querySelector(".tab-pane.active").classList.remove("active"),o.classList.add("active"),a.classList.add("active")}}t.NewContentElementWizard=d}));