/*
 * 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{SeverityEnum as m}from"@typo3/backend/enum/severity.js";import d from"jquery";import{Carousel as h}from"bootstrap";import c from"@typo3/backend/modal.js";import a from"@typo3/backend/severity.js";import p from"@typo3/backend/icons.js";import{topLevelModuleImport as f}from"@typo3/backend/utility/top-level-module-import.js";class g{constructor(){this.setup={slides:[],settings:{},forceSelection:!0,$carousel:null,carousel:null},this.originalSetup=d.extend(!0,{},this.setup)}set(t,e){return this.setup.settings[t]=e,this}addSlide(t,e,s="",n=m.info,i,r){const o={identifier:t,title:e,content:s,severity:n,progressBarTitle:i,callback:r};return this.setup.slides.push(o),this}async addFinalProcessingSlide(t){t||(t=()=>{this.dismiss()});const e=await p.getIcon("spinner-circle",p.sizes.large,null,null),s=document.createElement("div");s.classList.add("text-center"),s.append(document.createRange().createContextualFragment(e)),this.addSlide("final-processing-slide",top.TYPO3.lang["wizard.processing.title"],s,a.notice,top.TYPO3.lang["wizard.progressStep.finish"],t)}show(){const t=this.generateSlides(),e=this.setup.slides[0];c.advanced({title:e.title,content:t,severity:e.severity,staticBackdrop:!0,buttons:[{text:top.TYPO3.lang["wizard.button.cancel"],active:!0,btnClass:"btn-default float-start",name:"cancel",trigger:()=>{this.getComponent().trigger("wizard-dismiss")}},{text:top.TYPO3.lang["wizard.button.prev"],btnClass:"btn-"+a.getCssClass(e.severity),name:"prev"},{text:top.TYPO3.lang["wizard.button.next"],btnClass:"btn-"+a.getCssClass(e.severity),name:"next"}],additionalCssClasses:["modal-multi-step-wizard"],callback:s=>{f("@typo3/backend/element/progress-tracker-element.js").then(()=>{this.setup.carousel=new h(s.querySelector(".carousel")),this.addButtonContainer(),this.addProgressBar(),this.initializeEvents()})}}),this.getComponent().on("wizard-visible",()=>{this.setup.forceSelection&&(this.lockPrevStep(),this.lockNextStep()),this.runSlideCallback(e,this.setup.$carousel.find(".carousel-item").first())}).on("wizard-dismissed",()=>{this.setup=d.extend(!0,{},this.originalSetup)})}getComponent(){return this.setup.$carousel===null&&this.generateSlides(),this.setup.$carousel}dismiss(){c.dismiss()}lockNextStep(){const t=this.setup.$carousel.closest(".modal").find('button[name="next"]');return t.prop("disabled",!0),t}next(){this.setup.carousel.next()}previous(){this.setup.carousel.prev()}unlockNextStep(){const t=this.setup.$carousel.closest(".modal").find('button[name="next"]');return t.prop("disabled",!1),t}lockPrevStep(){const t=this.setup.$carousel.closest(".modal").find('button[name="prev"]');return t.prop("disabled",!0),t}unlockPrevStep(){const t=this.setup.$carousel.closest(".modal").find('button[name="prev"]');return t.prop("disabled",!1),t}triggerStepButton(t){const e=this.setup.$carousel.closest(".modal").find('button[name="'+t+'"]');return e.length>0&&e.prop("disabled")!==!0&&e.get(0).click(),e}blurCancelStep(){const t=this.setup.$carousel.closest(".modal").find('button[name="cancel"]');return t.trigger("blur"),t}initializeEvents(){const t=this.setup.$carousel.closest(".modal");this.initializeSlideNextEvent(t),this.initializeSlidePrevEvent(t),this.setup.$carousel.get(0).addEventListener("slide.bs.carousel",s=>{s.direction==="left"?this.nextSlideChanges(t):this.prevSlideChanges(t)}),this.setup.$carousel.get(0).addEventListener("slid.bs.carousel",s=>{const n=this.setup.$carousel.data("currentIndex"),i=this.setup.slides[n];this.setup.forceSelection&&this.lockNextStep(),this.runSlideCallback(i,d(s.relatedTarget))});const e=this.getComponent();e.on("wizard-dismiss",this.dismiss),c.currentModal.addEventListener("typo3-modal-hidden",()=>{e.trigger("wizard-dismissed")}),c.currentModal.addEventListener("typo3-modal-shown",()=>{e.trigger("wizard-visible")})}initializeSlideNextEvent(t){t.find(".modal-footer").find('button[name="next"]').off().on("click",()=>{this.setup.carousel.next()})}initializeSlidePrevEvent(t){t.find(".modal-footer").find('button[name="prev"]').off().on("click",()=>{this.setup.carousel.prev()})}nextSlideChanges(t){this.initializeSlideNextEvent(t);const e=t.find(".modal-title"),s=this.setup.$carousel.data("currentSlide")+1,n=this.setup.$carousel.data("currentIndex"),i=n+1;t.find(".carousel-item:eq("+i+")").empty().append(this.setup.slides[i].content),e.text(this.setup.slides[i].title),this.unlockPrevStep(),this.setup.$carousel.data("currentSlide",s),this.setup.$carousel.data("currentIndex",i),t.find("typo3-backend-progress-tracker").attr("active",i+1),this.updateCurrentSeverity(t,n,i)}prevSlideChanges(t){this.initializeSlidePrevEvent(t);const e=t.find(".modal-title"),n=t.find(".modal-footer").find('button[name="next"]'),i=this.setup.$carousel.data("currentSlide")-1,r=this.setup.$carousel.data("currentIndex"),o=r-1;t.find(".carousel-item:eq("+o+")").empty().append(this.setup.slides[o].content),e.text(this.setup.slides[o].title),o>0?this.unlockPrevStep():this.lockPrevStep(),this.setup.$carousel.data("currentSlide",i),this.setup.$carousel.data("currentIndex",o),n.text(top.TYPO3.lang["wizard.button.next"]),t.find("typo3-backend-progress-tracker").attr("active",o+1),this.updateCurrentSeverity(t,r,o)}updateCurrentSeverity(t,e,s){t.find(".modal-footer").find('button[name="next"]').removeClass("btn-"+a.getCssClass(this.setup.slides[e].severity)).addClass("btn-"+a.getCssClass(this.setup.slides[s].severity)),t.removeClass("modal-severity-"+a.getCssClass(this.setup.slides[e].severity)).addClass("modal-severity-"+a.getCssClass(this.setup.slides[s].severity))}runSlideCallback(t,e){typeof t.callback=="function"&&t.callback(e,this.setup.settings,t.identifier)}addProgressBar(){const t=this.setup.$carousel.find(".carousel-item").length,e=Math.max(1,t),s=Math.round(100/e);if(this.setup.$carousel.data("initialStep",s).data("slideCount",e).data("realSlideCount",t).data("currentIndex",0).data("currentSlide",1),e>1){const n=document.createElement("typo3-backend-progress-tracker");n.stages=this.setup.slides.map(u=>u.progressBarTitle);const i=this.setup.$carousel.get(0).closest(".modal-content"),r=i.querySelector(".modal-body"),o=document.createElement("div");o.classList.add("modal-progress"),o.appendChild(n),i.insertBefore(o,r)}}addButtonContainer(){this.setup.$carousel.closest(".modal").find(".modal-footer .btn").wrapAll('<div class="modal-btn-group" />')}generateSlides(){if(this.setup.$carousel!==null)return this.setup.$carousel;const t=document.createElement("div");t.classList.add("carousel","slide"),t.dataset.bsRide="false";const e=document.createElement("div");e.classList.add("carousel-inner"),e.role="listbox",t.append(e);for(let s=0;s<this.setup.slides.length;++s){const n=this.setup.slides[s],i=document.createElement("div");typeof n.content=="string"?i.textContent=n.content:n.content instanceof d?i.replaceChildren(n.content.get(0)):i.replaceChildren(n.content);const r=document.createElement("div");r.classList.add("carousel-item"),r.dataset.bsSlide=n.identifier,r.dataset.step=s.toString(10),r.append(i),e.append(r)}return this.setup.$carousel=d(t),this.setup.$carousel.find(".carousel-item").first().addClass("active"),this.setup.$carousel}}let l;try{window.opener&&window.opener.TYPO3&&window.opener.TYPO3.MultiStepWizard&&(l=window.opener.TYPO3.MultiStepWizard),parent&&parent.window.TYPO3&&parent.window.TYPO3.MultiStepWizard&&(l=parent.window.TYPO3.MultiStepWizard),top&&top.TYPO3&&top.TYPO3.MultiStepWizard&&(l=top.TYPO3.MultiStepWizard)}catch{}l||(l=new g,typeof TYPO3<"u"&&(TYPO3.MultiStepWizard=l));var S=l;export{S as default};
