/*
 * 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{AbstractInteractableModule as l}from"@typo3/install/module/abstract-interactable-module.js";import u from"@typo3/backend/modal.js";import h from"@typo3/backend/notification.js";import d from"@typo3/core/ajax/ajax-request.js";import{FlashMessage as p}from"@typo3/install/renderable/flash-message.js";import r from"@typo3/install/renderable/severity.js";import s from"@typo3/install/router.js";import g from"@typo3/core/event/regular-event.js";var o;(function(i){i.output=".t3js-coreUpdate-output",i.updateButton=".t3js-coreUpdate-button"})(o||(o={}));class f extends l{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.buttonTemplate=null}initialize(e){super.initialize(e),this.loadModuleFrameAgnostic("@typo3/install/renderable/flash-message.js").then(async()=>{await this.getData(),this.buttonTemplate=this.findInModal(o.updateButton)?.cloneNode(!0)}),new g("click",(t,a)=>{if(t.preventDefault(),!a.hasAttribute("data-action")){this.callAction("coreUpdateIsUpdateAvailable");return}const n=a.dataset.action;switch(this.findInModal(o.output).innerHTML="",n){case"updateDevelopment":this.update("development");break;case"updateRegular":this.update("regular");break;default:throw'Unknown update action "'+n+'"'}}).delegateTo(e,".t3js-coreUpdate-init")}getData(){const e=this.getModalBody();return new d(s.getUrl("coreUpdateGetData")).get({cache:"no-cache"}).then(async t=>{const a=await t.resolve();a.success===!0?(e.innerHTML=a.html,u.setButtons(a.buttons)):h.error("Something went wrong","The request was not processed successfully. Please check the browser's console and TYPO3's log.")},t=>{s.handleAjaxError(t,e)})}update(e){e!=="development"&&(e="regular"),this.callAction("coreUpdateCheckPreConditions",e)}callAction(e,t){const a={install:{action:e}};t!==void 0&&(a.install.type=t),this.addLoadingMessage(this.actionQueue[e].loadingMessage),new d(s.getUrl()).withQueryArguments(a).get({cache:"no-cache"}).then(async n=>{const c=await n.resolve();this.handleResult(c,this.actionQueue[e].finishMessage)===!0&&this.actionQueue[e].nextActionName!==void 0&&this.callAction(this.actionQueue[e].nextActionName,t)},n=>{s.handleAjaxError(n,this.getModalBody())})}handleResult(e,t){const a=e.success;return this.removeLoadingMessage(),e.status&&typeof e.status=="object"&&this.showStatusMessages(e.status),e.action&&typeof e.action=="object"&&this.showActionButton(e.action),a&&t&&this.addMessage(r.ok,t),a}addLoadingMessage(e){this.addMessage(r.loading,e)}removeLoadingMessage(){this.findInModal(o.output).querySelector("typo3-install-flashmessage")?.remove()}showStatusMessages(e){for(const t of e)this.addMessage(t.severity,t.title??"",t.message??"")}showActionButton(e){const t=this.buttonTemplate;e.action&&t.setAttribute("data-action",e.action),e.title&&(t.innerText=e.title),this.findInModal(o.updateButton).replaceWith(t)}addMessage(e,t,a){this.findInModal(o.output).append(p.create(e,t,a))}}var m=new f;export{m as default};
