/*
 * 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{AbstractInteractableModule}from"@typo3/install/module/abstract-interactable-module.js";import Modal from"@typo3/backend/modal.js";import Notification from"@typo3/backend/notification.js";import AjaxRequest from"@typo3/core/ajax/ajax-request.js";import InfoBox from"@typo3/install/renderable/info-box.js";import Severity from"@typo3/install/renderable/severity.js";import Router from"@typo3/install/router.js";class ImageProcessing extends AbstractInteractableModule{constructor(){super(...arguments),this.selectorExecuteTrigger=".t3js-imageProcessing-execute",this.selectorTestContainer=".t3js-imageProcessing-twinContainer",this.selectorTwinImageTemplate=".t3js-imageProcessing-twinImage-template",this.selectorCommandContainer=".t3js-imageProcessing-command",this.selectorCommandText=".t3js-imageProcessing-command-text",this.selectorTwinImages=".t3js-imageProcessing-images"}initialize(e){this.currentModal=e,this.getData(),e.on("click",this.selectorExecuteTrigger,(e=>{e.preventDefault(),this.runTests()}))}getData(){const e=this.getModalBody();new AjaxRequest(Router.getUrl("imageProcessingGetData")).get({cache:"no-cache"}).then((async t=>{const s=await t.resolve();!0===s.success?(e.empty().append(s.html),Modal.setButtons(s.buttons),this.runTests()):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)}))}runTests(){const e=this.getModalBody(),t=this.findInModal(this.selectorExecuteTrigger);this.setModalButtonsState(!1);const s=this.findInModal(this.selectorTwinImageTemplate),o=[];e.find(this.selectorTestContainer).each(((t,r)=>{const a=$(r),n=a.data("test"),i=InfoBox.render(Severity.loading,"Loading...","");a.empty().html(i);const c=new AjaxRequest(Router.getUrl(n)).get({cache:"no-cache"}).then((async e=>{const t=await e.resolve();if(!0===t.success){a.empty(),Array.isArray(t.status)&&t.status.forEach((e=>{const t=InfoBox.render(e.severity,e.title,e.message);a.append(t)}));const e=s.clone();if(e.removeClass("t3js-imageProcessing-twinImage-template"),!0===t.fileExists&&(e.find("img.reference").attr("src",t.referenceFile),e.find("img.result").attr("src",t.outputFile),e.find(this.selectorTwinImages).show()),Array.isArray(t.command)&&t.command.length>0){e.find(this.selectorCommandContainer).show();const s=[];t.command.forEach((e=>{s.push("<strong>Command:</strong>\n"+e[1]),3===e.length&&s.push("<strong>Result:</strong>\n"+e[2])})),e.find(this.selectorCommandText).html(s.join("\n"))}a.append(e)}}),(t=>{Router.handleAjaxError(t,e)}));o.push(c)})),Promise.all(o).then((()=>{t.removeClass("disabled").prop("disabled",!1)}))}}export default new ImageProcessing;