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