/*
 * 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"bootstrap";import $ from"jquery";import"@typo3/install/renderable/clearable.js";import{AbstractInteractableModule}from"@typo3/install/module/abstract-interactable-module.js";import ModuleMenu from"@typo3/backend/module-menu.js";import Notification from"@typo3/backend/notification.js";import AjaxRequest from"@typo3/core/ajax/ajax-request.js";import Router from"@typo3/install/router.js";import{topLevelModuleImport}from"@typo3/backend/utility/top-level-module-import.js";import DebounceEvent from"@typo3/core/event/debounce-event.js";import RegularEvent from"@typo3/core/event/regular-event.js";class ExtensionConfiguration extends AbstractInteractableModule{constructor(){super(...arguments),this.selectorFormListener=".t3js-extensionConfiguration-form",this.selectorSearchInput=".t3js-extensionConfiguration-search"}initialize(t){this.currentModal=t,this.getContent(),t.on("keydown",(e=>{const o=t.find(this.selectorSearchInput);e.ctrlKey||e.metaKey?"f"===String.fromCharCode(e.which).toLowerCase()&&(e.preventDefault(),o.trigger("focus")):27===e.keyCode&&(e.preventDefault(),o.val("").trigger("focus"))})),new DebounceEvent("input",((t,e)=>{const o=e.value;this.search(o)}),100).delegateTo(t.get(0),this.selectorSearchInput),new RegularEvent("change",((t,e)=>{const o=e.value;this.search(o)})).delegateTo(t.get(0),this.selectorSearchInput),t.on("submit",this.selectorFormListener,(t=>{t.preventDefault(),this.write($(t.currentTarget))}))}search(t){this.currentModal.find(".search-item").each(((e,o)=>{const a=$(o);$(":contains("+t+")",a).length>0||$('input[value*="'+t+'"]',a).length>0?a.removeClass("hidden").addClass("searchhit"):a.removeClass("searchhit").addClass("hidden")})),this.currentModal.find(".searchhit").collapse("show")}getContent(){const t=this.getModalBody();new AjaxRequest(Router.getUrl("extensionConfigurationGetContent")).get({cache:"no-cache"}).then((async e=>{const o=await e.resolve();!0===o.success&&(t.html(o.html),t.get(0).querySelector(this.selectorSearchInput).clearable(),this.initializeWrap(),this.initializeColorPicker())}),(e=>{Router.handleAjaxError(e,t)}))}initializeColorPicker(){window.location!==window.parent.location?topLevelModuleImport("@typo3/backend/color-picker.js").then((({default:t})=>{parent.document.querySelectorAll(".t3js-color-input").forEach((e=>t.initialize(e)))})):import("@typo3/backend/color-picker.js").then((({default:t})=>{document.querySelectorAll(".t3js-color-input").forEach((e=>t.initialize(e)))}))}write(t){const e=this.getModalBody(),o=this.getModuleContent().data("extension-configuration-write-token"),a={};for(const e of t.serializeArray())a[e.name]=e.value;new AjaxRequest(Router.getUrl()).post({install:{token:o,action:"extensionConfigurationWrite",extensionKey:t.attr("data-extensionKey"),extensionConfiguration:a}}).then((async t=>{const e=await t.resolve();!0===e.success&&Array.isArray(e.status)?(e.status.forEach((t=>{Notification.showMessage(t.title,t.message,t.severity)})),"backend"===$("body").data("context")&&ModuleMenu.App.refreshMenu()):Notification.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")}),(t=>{Router.handleAjaxError(t,e)}))}initializeWrap(){this.findInModal(".t3js-emconf-offset").each(((t,e)=>{const o=$(e),a=o.parent(),r=o.attr("id"),n=o.attr("value").split(",");o.attr("data-offsetfield-x","#"+r+"_offset_x").attr("data-offsetfield-y","#"+r+"_offset_y").wrap('<div class="hidden"></div>');const i=$("<div>",{class:"form-multigroup-item"}).append($("<div>",{class:"input-group"}).append($("<div>",{class:"input-group-addon"}).text("x"),$("<input>",{id:r+"_offset_x",class:"form-control t3js-emconf-offsetfield","data-target":"#"+r,value:n[0]?.trim()}))),s=$("<div>",{class:"form-multigroup-item"}).append($("<div>",{class:"input-group"}).append($("<div>",{class:"input-group-addon"}).text("y"),$("<input>",{id:r+"_offset_y",class:"form-control t3js-emconf-offsetfield","data-target":"#"+r,value:n[1]?.trim()}))),l=$("<div>",{class:"form-multigroup-wrap"}).append(i,s);a.append(l),a.find(".t3js-emconf-offsetfield").on("keyup",(t=>{const e=a.find($(t.currentTarget).data("target"));e.val(a.find(e.data("offsetfield-x")).val()+","+a.find(e.data("offsetfield-y")).val())}))})),this.findInModal(".t3js-emconf-wrap").each(((t,e)=>{const o=$(e),a=o.parent(),r=o.attr("id"),n=o.attr("value").split("|");o.attr("data-wrapfield-start","#"+r+"_wrap_start").attr("data-wrapfield-end","#"+r+"_wrap_end").wrap('<div class="hidden"></div>');const i=$("<div>",{class:"form-multigroup-wrap"}).append($("<div>",{class:"form-multigroup-item"}).append($("<input>",{id:r+"_wrap_start",class:"form-control t3js-emconf-wrapfield","data-target":"#"+r,value:n[0]?.trim()})),$("<div>",{class:"form-multigroup-item"}).append($("<input>",{id:r+"_wrap_end",class:"form-control t3js-emconf-wrapfield","data-target":"#"+r,value:n[1]?.trim()})));a.append(i),a.find(".t3js-emconf-wrapfield").on("keyup",(t=>{const e=a.find($(t.currentTarget).data("target"));e.val(a.find(e.data("wrapfield-start")).val()+"|"+a.find(e.data("wrapfield-end")).val())}))}))}}export default new ExtensionConfiguration;