/*
 * 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 l,nothing as p}from"lit";import{live as d}from"lit/directives/live.js";import{styleMap as f}from"lit/directives/style-map.js";import{repeat as u}from"lit/directives/repeat.js";import{Task as m,TaskStatus as h}from"@lit/task";import y from"@typo3/core/ajax/ajax-request.js";import"@typo3/backend/element/alert-element.js";import"@typo3/backend/element/icon-element.js";import"@typo3/backend/element/spinner-element.js";import n from"~labels/backend.wizards.localization";class g{constructor(e){this.context=e,this.key="contentRecordSelection",this.title=n.get("step.content_selection.title"),this.autoAdvance=!0,this.lastTargetLanguage=null,this.lastSourceLanguage=null,this.hasDispatchedAutoAdvance=!1,this.hasUserInteracted=!1,this.selectedRecordUids=[],this.task=new m(this.context.wizard,{task:async([t,i,s])=>await(await new y(TYPO3.settings.ajaxUrls.wizard_localization_get_content).withQueryArguments({pageUid:t,targetLanguage:i,sourceLanguage:s}).get()).resolve(),args:()=>[this.context.recordUid,this.context.getStoreData("targetLanguage"),this.context.getStoreData("sourceLanguage")],autoRun:!1})}isComplete(){return!0}render(){const e=this.context.getStoreData("targetLanguage"),t=this.context.getStoreData("sourceLanguage");return(this.lastTargetLanguage!==e||this.lastSourceLanguage!==t||this.task.status===h.INITIAL)&&(this.lastTargetLanguage=e,this.lastSourceLanguage=t,this.hasDispatchedAutoAdvance=!1,this.hasUserInteracted=!1,this.task.run()),this.task.render({complete:s=>{if(this.getValue().length===0&&!this.hasUserInteracted){const o=this.context.getStoreData("selectedRecordUids");o&&o.length>0&&this.setValue(o)}if(s.layout.elementCount===0&&!this.hasDispatchedAutoAdvance)return this.hasDispatchedAutoAdvance=!0,this.context.dispatchAutoAdvance(),this.context.wizard.renderLoader();const a=this.getAllRecordUids(s.layout);this.getValue().length===0&&a.length>0&&!this.hasUserInteracted&&this.setValue(a);const c=a.length>0&&a.every(o=>this.getValue().includes(o));return l`<div class=localization-record-selection><div class="d-flex justify-content-between align-items-center mb-3"><div><h2 class=h4>${n.get("step.content_selection.headline")}</h2><p class=mb-0>${n.get("step.content_selection.description")}</p></div>${a.length>0?l`<button type=button id=selectionToggle class="btn btn-primary" @click=${()=>this.handleSelectionToggle()}>${c?n.get("step.content_selection.button.deselect_all"):n.get("step.content_selection.button.select_all")}</button>`:p}</div>${a.length===0?l`<typo3-backend-alert severity=3 message=${n.get("step.content_selection.no_content.message")} show-icon></typo3-backend-alert>`:l`<div class=record-selection-content>${this.renderLayout(s.layout)}</div>`}</div>`},error:s=>this.context.wizard.renderError(n.get("step.content_selection.error.message"),s),pending:()=>this.context.wizard.renderLoader()})}reset(){this.setValue([]),this.context.clearStoreData("selectedRecordUids"),this.hasUserInteracted=!1}getValue(){return this.selectedRecordUids}setValue(e){this.selectedRecordUids=e,this.context.setStoreData("selectedRecordUids",e)}beforeAdvance(){this.context.setStoreData("selectedRecordUids",this.getValue())}getSelectedRecordsWithDetails(){const e=this.getValue(),t=[];return this.task.value&&this.task.value.layout.rows.forEach(i=>{i.columns.forEach(s=>{s.records.forEach(a=>{e.includes(a.uid)&&t.push(a)})})}),t}getSummaryData(){const e=this.getSelectedRecordsWithDetails();return e.length===0?[]:[{label:n.get("step.content_selection.summary.title"),value:l`<ul class=list-group>${e.map(i=>l`<li class=list-group-item><span title="id=${i.uid}"> <typo3-backend-icon identifier=${i.icon} size=small class=me-1></typo3-backend-icon> </span>${i.title}</li>`)}</ul>`}]}renderLayout(e){let t=1;return l`<div class=pagelayout>${e.rows.map(i=>{const s=this.renderRow(i,t);return t+=1,s})}</div>`}renderRow(e,t){let i=1;return l`${u(e.columns,s=>s.position,s=>{const a=this.renderColumn(s,t,i);return i+=s.colspan||1,a})}`}renderColumn(e,t,i){const s=e.records.filter(r=>this.getValue().includes(r.uid)),a=s.length===e.records.length,c=s.length>0&&s.length<e.records.length,o=f({"--pagelayout-cell-col":i,"--pagelayout-cell-colspan":e.colspan||1,"--pagelayout-cell-row":t,"--pagelayout-cell-rowspan":e.rowspan||1});return l`<div class=pagelayout-cell style=${o}><div class=pagelayout-cell-header>${e.records.length>0?l`<div class="form-check form-check-type-toggle"><input class=form-check-input type=checkbox id=records-column-${e.position} .checked=${d(a)} .indeterminate=${c} @change=${r=>this.handleColumnToggle(r,e)}> <label class=form-check-label for=records-column-${e.position}>${e.label}</label></div>`:l`<div class="form-check form-check-type-toggle"><div class=form-check-text>${e.label}</div></div>`}</div><div class=pagelayout-cell-content>${u(e.records,r=>r.uid,r=>this.renderRecord(r))}</div></div>`}renderRecord(e){return l`<div class="form-check form-check-type-toggle"><input type=checkbox class=form-check-input id=record-uid-${e.uid} .checked=${d(this.getValue().includes(e.uid))} @change=${t=>this.handleRecordToggle(t,e)}> <label class=form-check-label for=record-uid-${e.uid}><span title="id=${e.uid}"> <typo3-backend-icon identifier=${e.icon} size=small></typo3-backend-icon> </span>${e.title}</label></div>`}handleColumnToggle(e,t){const i=e.currentTarget,s=t.records.map(a=>a.uid);if(i.checked){const a=s.filter(c=>!this.getValue().includes(c));this.setValue([...this.getValue(),...a])}else this.setValue(this.getValue().filter(a=>!s.includes(a)));this.hasUserInteracted=!0}handleRecordToggle(e,t){e.currentTarget.checked?this.getValue().includes(t.uid)||this.setValue([...this.getValue(),t.uid]):this.setValue(this.getValue().filter(s=>s!==t.uid)),this.hasUserInteracted=!0}getAllRecordUids(e){const t=[];return e.rows.forEach(i=>{i.columns.forEach(s=>{s.records.forEach(a=>{t.push(a.uid)})})}),t}handleSelectionToggle(){if(!this.task||this.task.status!==h.COMPLETE||!this.task.value)return;this.hasUserInteracted=!0;const e=this.getAllRecordUids(this.task.value.layout),t=this.getValue().length===0;this.setValue(t?e:[])}}export{g as ContentRecordSelectionStep,g as default};
