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