/*
 * 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{html as r}from"lit";import{live as c}from"lit/directives/live.js";import{Task as d,TaskStatus as h}from"@lit/task";import u from"@typo3/core/ajax/ajax-request.js";import i from"~labels/backend.wizards.localization";class p{constructor(e){this.context=e,this.key="handler",this.title=i.get("step.handler_selection.title"),this.autoAdvance=!0,this.hasDispatchedAutoAdvance=!1,this.selectedHandler=null,this.task=new d(this.context.wizard,{task:async([t,a,n,s,l])=>{if(n==null||s==null||l==null)return[];try{return await(await new u(TYPO3.settings.ajaxUrls.wizard_localization_get_handlers).withQueryArguments({recordType:t,recordUid:a,sourceLanguage:n,targetLanguage:s,mode:l}).get()).resolve()}catch(o){return console.warn("Failed to fetch handlers:",o),[]}},args:()=>[this.context.recordType,this.context.recordUid,this.context.getStoreData("sourceLanguage"),this.context.getStoreData("targetLanguage"),this.context.getStoreData("localizationMode")],autoRun:!1})}isComplete(){return this.getValue()!==null}render(){return this.task.status===h.INITIAL&&this.task.run(),this.task.render({complete:e=>{if(this.getValue()==null){const a=this.context.getStoreData("localizationHandler");a!=null&&this.setValue(a)}let t=!1;return this.getValue()==null&&e.length>0&&(this.setValue(e[0].identifier),e.length===1&&(t=!0)),t&&!this.hasDispatchedAutoAdvance?(this.hasDispatchedAutoAdvance=!0,this.context.dispatchAutoAdvance(),this.context.wizard.renderLoader()):e.length===0?r`<div class="alert alert-warning"><p>${i.get("step.handler_selection.no_handlers")}</p></div>`:r`<div class=localization-handler-selection><h2 class=h4>${i.get("step.handler_selection.headline")}</h2><p>${i.get("step.handler_selection.description")}</p><div class=form-check-card-container>${e.map(a=>this.renderHandlerOption(a))}</div></div>`},error:e=>this.context.wizard.renderError(i.get("step.handler_selection.error"),e),pending:()=>this.context.wizard.renderLoader()})}reset(){this.setValue(null),this.context.clearStoreData("localizationHandler")}getValue(){return this.selectedHandler}setValue(e){this.selectedHandler=e}beforeAdvance(){this.context.setStoreData("localizationHandler",this.getValue())}getSummaryData(){const e=this.context.getStoreData("localizationHandler");if(e==null||!this.task.value)return[];const t=this.task.value.find(a=>a.identifier===e);return t?[{label:i.get("step.handler_selection.summary_label"),value:r`<typo3-backend-icon identifier=${t.iconIdentifier} size=small class=me-1></typo3-backend-icon>${t.label}`}]:[]}renderHandlerOption(e){const t=this.getValue()===e.identifier;return r`<div class="form-check form-check-type-card"><input class=form-check-input type=radio name=localization-handler id=handler-${e.identifier} value=${e.identifier} .checked=${c(t)} @change=${()=>this.setValue(e.identifier)}> <label class=form-check-label for=handler-${e.identifier}><span class=form-check-label-header> <typo3-backend-icon identifier=${e.iconIdentifier} size=small></typo3-backend-icon> ${e.label} </span> <span class=form-check-label-body>${e.description} </span></label></div>`}}export{p as HandlerSelectionStep};
