/*
 * 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{nothing as d,html as i}from"lit";import{live as k}from"lit/directives/live.js";import{TaskStatus as v,Task as g}from"@lit/task";import o from"~labels/backend.wizards.page";import y from"~labels/core.db.pages";import p from"~labels/core.core";import b from"@typo3/core/ajax/ajax-request.js";import{SeverityEnum as $}from"@typo3/backend/enum/severity.js";class m{constructor(t){this.context=t,this.key="doktype",this.title=y.get("doktype"),this.autoAdvance=!0,this.selectedDoktype=null,this.searchTerm="",this.hasDispatchedAutoAdvance=!1,this.initDoktypesTask()}isComplete(){return this.getValue()!==null}render(){return this.task.status===v.INITIAL&&this.task.run(),this.task.render({complete:t=>{let a=!1,n=d;if(this.getValue()===null){const e=this.context.getStoreData(this.key),s=this.context?.configuration?.doktype??null,c=t.filter(r=>r.value!=="--div--");if(e!=null)this.setValue(e);else if(s!==null)c.some(r=>r.value===s)?(this.setValue(s),a=!0):n=i`<typo3-backend-alert severity=${$.warning} heading=${o.get("step.doktype.not_allowed_doktype.title")} message=${o.get("step.doktype.not_allowed_doktype.message")} show-icon></typo3-backend-alert>`;else if(c.length>0){const[r]=c;this.setValue(r.value),a=c.length===1}}if(a&&!this.hasDispatchedAutoAdvance)return this.hasDispatchedAutoAdvance=!0,this.context.dispatchAutoAdvance(),this.context.wizard.renderLoader();const f=t.filter(e=>e.value==="--div--"||e.label.toLowerCase().includes(this.searchTerm.toLowerCase())),h=[];let l={label:"",items:[]};f.forEach(e=>{e.value==="--div--"?(l.items.length>0&&h.push(l),l={label:e.label,items:[]}):l.items.push(e)}),l.items.length>0&&h.push(l);const u=h.filter(e=>e.items.length>0);return i`${n}<div class=doktype-selection><h2 class=h4>${o.get("step.doktype.headline")}</h2><div class=doktype-selection__search><div class="input-group mb-4"><label for=doktype-search class=visually-hidden>${p.get("labels.label.searchString")}</label> <input type=search autocomplete=off id=doktype-search class="form-control form-control-sm search-input" value=${this.searchTerm} placeholder=${p.get("tree.searchTermInfo")} @input=${e=>this.handleSearch(e)}></div></div>${u.length==0?i`<div role=alert class="alert alert-info">${o.get("step.doktype.filter.noResults")}</div>`:d}<div class=form-check-card-container>${u.map(e=>i`${e.label?i`<h3 class=form-check-card-container-headline>${e.label}</h3>`:d} ${e.items.map(s=>i`<div class="form-check form-check-type-card"><input class=form-check-input type=radio name=doktype id=mode-${s.value} value=${s.value} .checked=${k(this.getValue()===s.value)} @change=${()=>this.setValue(s.value)}> <label class=form-check-label for=mode-${s.value}><span class="form-check-label-header form-check-label-header-inherit"> <typo3-backend-icon identifier=${s.icon} size=small></typo3-backend-icon> ${s.label} </span></label></div>`)}`)}</div></div>`},error:t=>this.context.wizard.renderError(o.get("step.doktype.load_error"),t),pending:()=>this.context.wizard.renderLoader()})}reset(){this.setValue(null),this.context.clearStoreData(this.key),this.initDoktypesTask()}getValue(){return this.selectedDoktype}setValue(t){this.selectedDoktype=t,this.context.wizard.requestUpdate()}beforeAdvance(){this.context.setStoreData(this.key,this.getValue())}getSummaryData(){const t=this.context.getStoreData(this.key);if(!t||!this.task.value)return[];const a=this.task.value.find(n=>n.value===t);return a?[{label:this.title,value:i`<typo3-backend-icon identifier=${a.icon} size=small class=me-1></typo3-backend-icon>${a.label}`}]:[]}handleSearch(t){this.searchTerm=t.target.value,this.context.wizard.requestUpdate()}initDoktypesTask(){this.task=new g(this.context.wizard,{task:async()=>await(await new b(TYPO3.settings.ajaxUrls.wizard_page_get_doktypes).withQueryArguments({data:this.context.getDataStore()}).get()).resolve(),autoRun:!1})}}export{m as DoktypeStep,m as default};
