/*
 * 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();this.setModalButtonsState(!1);const t=this.findInModal(this.selectorTwinImageTemplate),s=[];e.find(this.selectorTestContainer).each(((o,r)=>{const n=$(r),a=n.data("test"),i=InfoBox.render(Severity.loading,"Loading...","");n.empty().append(i);const c=new AjaxRequest(Router.getUrl(a)).get({cache:"no-cache"}).then((async e=>{const s=await e.resolve();if(!0===s.success){n.empty(),Array.isArray(s.status)&&s.status.forEach((e=>{const t=InfoBox.render(e.severity,e.title,e.message);n.append(t)}));const e=t.clone();if(e.removeClass("t3js-imageProcessing-twinImage-template"),!0===s.fileExists&&(e.find("img.reference").attr("src",s.referenceFile),e.find("img.result").attr("src",s.outputFile),e.find(this.selectorTwinImages).show()),Array.isArray(s.command)&&s.command.length>0){e.find(this.selectorCommandContainer).show();const t=[];s.command.forEach((e=>{t.push("<strong>Command:</strong>\n"+e[1]),3===e.length&&t.push("<strong>Result:</strong>\n"+e[2])})),e.find(this.selectorCommandText).html(t.join("\n"))}n.append(e)}}),(t=>{Router.handleAjaxError(t,e)}));s.push(c)})),Promise.all(s).then((()=>{this.setModalButtonsState(!0)}))}}export default new ImageProcessing;