/*
 * 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{AbstractInteractableModule as m}from"@typo3/install/module/abstract-interactable-module.js";import f from"@typo3/core/ajax/ajax-request.js";import{FlashMessage as v}from"@typo3/install/renderable/flash-message.js";import{InfoBox as h}from"@typo3/install/renderable/info-box.js";import"@typo3/install/renderable/language-packs.js";import k from"@typo3/install/renderable/severity.js";import d from"@typo3/install/router.js";var c;(function(U){U.outputContainer=".t3js-languagePacks-output",U.contentContainer=".t3js-languagePacks-mainContent",U.notifications=".t3js-languagePacks-notifications"})(c||(c={}));class r extends m{constructor(){super(...arguments),this.activeLanguages=[],this.activeExtensions=[],this.packsUpdateDetails={toHandle:0,handled:0,updated:0,new:0,failed:0,skipped:0},this.notifications=[]}static pluralize(t,a="pack",i="s",n=0){return t!==1&&n!==1?a+i:a}initialize(t){super.initialize(t),Promise.all([this.loadModuleFrameAgnostic("@typo3/install/renderable/info-box.js"),this.loadModuleFrameAgnostic("@typo3/install/renderable/flash-message.js"),this.loadModuleFrameAgnostic("@typo3/install/renderable/language-packs.js")]).then(()=>{this.getData()})}getData(){const t=this.getModalBody();new f(d.getUrl("languagePacksGetData")).get({cache:"no-cache"}).then(async a=>{const i=await a.resolve(),{success:n,html:e,...s}=i;if(n===!0){this.activeLanguages=i.activeLanguages,this.activeExtensions=i.activeExtensions,t.innerHTML=e;const p=t.parentElement.querySelector(c.contentContainer);p.innerHTML="";const u=window.location!==window.parent.location?parent.document:document,l=u.createElement("typo3-install-language-matrix");l.data=s,this.getModuleContent().dataset.configurationIsWritable==="true"&&l.setAttribute("configurationIsWritable",""),l.addEventListener("activate-language",o=>{this.activateLanguage(o.detail.iso)}),l.addEventListener("deactivate-language",o=>{this.deactivateLanguage(o.detail.iso)}),l.addEventListener("download-packs",o=>{this.updatePacks(o.detail?.iso||void 0,void 0)});const g=u.createElement("typo3-install-extension-matrix");g.data=s,g.addEventListener("download-packs",o=>{this.updatePacks(o.detail?.iso||void 0,o.detail?.extension||void 0)}),p.append(l,g)}else this.addNotification(h.create(k.error,"Something went wrong"));this.renderNotifications()},a=>{d.handleAjaxError(a,t)})}activateLanguage(t){const a=this.getModalBody(),i=this.findInModal(c.outputContainer);this.renderProgressBar(i),this.getNotificationBox().innerHTML="",new f(d.getUrl()).post({install:{action:"languagePacksActivateLanguage",token:this.getModuleContent().dataset.languagePacksActivateLanguageToken,iso:t}}).then(async n=>{const e=await n.resolve();i.innerHTML="",e.success===!0&&Array.isArray(e.status)?e.status.forEach(s=>{this.addNotification(h.create(s.severity,s.title,s.message))}):this.addNotification(h.create(k.error,"Something went wrong")),this.getData()},n=>{d.handleAjaxError(n,a)})}deactivateLanguage(t){const a=this.getModalBody(),i=this.findInModal(c.outputContainer);this.renderProgressBar(i),this.getNotificationBox().innerHTML="",new f(d.getUrl()).post({install:{action:"languagePacksDeactivateLanguage",token:this.getModuleContent().dataset.languagePacksDeactivateLanguageToken,iso:t}}).then(async n=>{const e=await n.resolve();i.innerHTML="",e.success===!0&&Array.isArray(e.status)?e.status.forEach(s=>{this.addNotification(h.create(s.severity,s.title,s.message))}):this.addNotification(h.create(k.error,"Something went wrong")),this.getData()},n=>{d.handleAjaxError(n,a)})}updatePacks(t,a){const i=this.findInModal(c.outputContainer),n=this.findInModal(c.contentContainer),e=t===void 0?this.activeLanguages:[t];let s=!0,p=this.activeExtensions;a!==void 0&&(p=[a],s=!1),this.packsUpdateDetails={toHandle:e.length*p.length,handled:0,updated:0,new:0,failed:0,skipped:0};const u=this.renderProgressBar(i,this.packsUpdateDetails.toHandle===1?void 0:{value:0,max:this.packsUpdateDetails.toHandle,label:"0 of "+this.packsUpdateDetails.toHandle+" language "+r.pluralize(this.packsUpdateDetails.toHandle)+" updated"});n.innerHTML="",e.forEach(l=>{p.forEach(g=>{this.getNotificationBox().innerHTML="",new f(d.getUrl()).post({install:{action:"languagePacksUpdatePack",token:this.getModuleContent().dataset.languagePacksUpdatePackToken,iso:l,extension:g}}).then(async o=>{const D=await o.resolve();D.success===!0?(this.packsUpdateDetails.handled++,D.packResult==="new"?this.packsUpdateDetails.new++:D.packResult==="update"?this.packsUpdateDetails.updated++:D.packResult==="skipped"?this.packsUpdateDetails.skipped++:this.packsUpdateDetails.failed++,this.packUpdateDone(s,e,u)):(this.packsUpdateDetails.handled++,this.packsUpdateDetails.failed++,this.packUpdateDone(s,e,u))},()=>{this.packsUpdateDetails.handled++,this.packsUpdateDetails.failed++,this.packUpdateDone(s,e,u)})})})}packUpdateDone(t,a,i){const n=this.getModalBody();this.packsUpdateDetails.handled===this.packsUpdateDetails.toHandle?(this.addNotification(h.create(k.ok,"Language packs updated",this.packsUpdateDetails.new+" new language "+r.pluralize(this.packsUpdateDetails.new)+" downloaded, "+this.packsUpdateDetails.updated+" language "+r.pluralize(this.packsUpdateDetails.updated)+" updated, "+this.packsUpdateDetails.skipped+" language "+r.pluralize(this.packsUpdateDetails.skipped)+" skipped, "+this.packsUpdateDetails.failed+" language "+r.pluralize(this.packsUpdateDetails.failed)+" not available")),t===!0?new f(d.getUrl()).post({install:{action:"languagePacksUpdateIsoTimes",token:this.getModuleContent().dataset.languagePacksUpdateIsoTimesToken,isos:a}}).then(async e=>{(await e.resolve()).success===!0?this.getData():this.addNotification(v.create(k.error,"Something went wrong"))},e=>{d.handleAjaxError(e,n)}):this.getData()):(i.value=this.packsUpdateDetails.handled,i.label=this.packsUpdateDetails.handled+" of "+this.packsUpdateDetails.toHandle+" language "+r.pluralize(this.packsUpdateDetails.handled,"pack","s",this.packsUpdateDetails.toHandle)+" updated")}getNotificationBox(){return this.findInModal(c.notifications)}addNotification(t){this.notifications.push(t)}renderNotifications(){const t=this.getNotificationBox();for(const a of this.notifications)t.appendChild(a);this.notifications=[]}}var w=new r;export{w as default};
