/*
 * 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","jquery","../AbstractInteractableModule","TYPO3/CMS/Backend/Modal","TYPO3/CMS/Backend/Notification","TYPO3/CMS/Core/Ajax/AjaxRequest","../../Renderable/FlashMessage","../../Renderable/Severity","../../Router"],(function(e,t,a,s,o,n,i,c,d,r){"use strict";a=__importDefault(a);class l extends s.AbstractInteractableModule{constructor(){super(...arguments),this.actionQueue={coreUpdateIsUpdateAvailable:{loadingMessage:"Checking for possible regular or security update",finishMessage:void 0,nextActionName:void 0},coreUpdateCheckPreConditions:{loadingMessage:"Checking if update is possible",finishMessage:"System can be updated",nextActionName:"coreUpdateDownload"},coreUpdateDownload:{loadingMessage:"Downloading new core",finishMessage:void 0,nextActionName:"coreUpdateVerifyChecksum"},coreUpdateVerifyChecksum:{loadingMessage:"Verifying checksum of downloaded core",finishMessage:void 0,nextActionName:"coreUpdateUnpack"},coreUpdateUnpack:{loadingMessage:"Unpacking core",finishMessage:void 0,nextActionName:"coreUpdateMove"},coreUpdateMove:{loadingMessage:"Moving core",finishMessage:void 0,nextActionName:"coreUpdateActivate"},coreUpdateActivate:{loadingMessage:"Activating core",finishMessage:"Core updated - please reload your browser",nextActionName:void 0}},this.selectorOutput=".t3js-coreUpdate-output",this.updateButton=".t3js-coreUpdate-button",this.buttonTemplate=null}initialize(e){this.currentModal=e,this.getData().then(()=>{this.buttonTemplate=this.findInModal(this.updateButton).clone()}),e.on("click",".t3js-coreUpdate-init",e=>{e.preventDefault();const t=(0,a.default)(e.currentTarget).attr("data-action");switch(this.findInModal(this.selectorOutput).empty(),t){case"checkForUpdate":this.callAction("coreUpdateIsUpdateAvailable");break;case"updateDevelopment":this.update("development");break;case"updateRegular":this.update("regular");break;default:throw'Unknown update action "'+t+'"'}})}getData(){const e=this.getModalBody();return new i(r.getUrl("coreUpdateGetData")).get({cache:"no-cache"}).then(async t=>{const a=await t.resolve();!0===a.success?(e.empty().append(a.html),o.setButtons(a.buttons)):n.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{r.handleAjaxError(t,e)})}update(e){"development"!==e&&(e="regular"),this.callAction("coreUpdateCheckPreConditions",e)}callAction(e,t){const a={install:{action:e}};void 0!==t&&(a.install.type=t),this.addLoadingMessage(this.actionQueue[e].loadingMessage),new i(r.getUrl()).withQueryArguments(a).get({cache:"no-cache"}).then(async a=>{const s=await a.resolve();!0===this.handleResult(s,this.actionQueue[e].finishMessage)&&void 0!==this.actionQueue[e].nextActionName&&this.callAction(this.actionQueue[e].nextActionName,t)},e=>{r.handleAjaxError(e,this.getModalBody())})}handleResult(e,t){const a=e.success;return this.removeLoadingMessage(),e.status&&"object"==typeof e.status&&this.showStatusMessages(e.status),e.action&&"object"==typeof e.action&&this.showActionButton(e.action),a&&t&&this.addMessage(d.ok,t),a}addLoadingMessage(e){const t=c.render(d.loading,e);this.findInModal(this.selectorOutput).append(t)}removeLoadingMessage(){this.findInModal(this.selectorOutput).find(".alert-loading").remove()}showStatusMessages(e){a.default.each(e,(e,t)=>{let a="",s="";const o=t.severity;t.title&&(a=t.title),t.message&&(s=t.message),this.addMessage(o,a,s)})}showActionButton(e){let t=!1,a=!1;e.title&&(t=e.title),e.action&&(a=e.action);const s=this.buttonTemplate;a&&s.attr("data-action",a),t&&s.text(t),this.findInModal(this.updateButton).replaceWith(s)}addMessage(e,t,a){const s=c.render(e,t,a);this.findInModal(this.selectorOutput).append(s)}}return new l}));