/*
* 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 o}from"lit";import{TaskStatus as c,Task as l}from"@lit/task";import{insertPositionOptions as u}from"@typo3/backend/tree/page-position-select.js";import n from"~labels/core.misc";import h from"@typo3/core/ajax/ajax-request.js";class r{constructor(t){this.context=t,this.key="position",this.title=n.get("selectPosition"),this.autoAdvance=!0,this.positionData=null,this.hasDispatchedAutoAdvance=!1,this.initSummaryTask()}isComplete(){return Number.isFinite(this.positionData?.pageUid)}render(){if(this.getValue()===null){const e=this.context.getStoreData(this.key),s=this.context?.configuration?.positionData??null;if(e!=null)this.setValue(e);else if(s!==null&&(this.setValue(s),(this.context?.configuration?.preventPositionAutoAdvance??!1)==!1&&!this.hasDispatchedAutoAdvance))return this.hasDispatchedAutoAdvance=!0,this.context.dispatchAutoAdvance(),this.context.wizard.renderLoader()}const t=this.getValue();return o`this.handleInsertPositionChange(e)} @typo3:page-position-select-tree:insert-position-confirm=${()=>this.handleInsertPositionConfirm()}>`}reset(){this.setValue(null),this.context.clearStoreData(this.key)}getValue(){return this.positionData}setValue(t){this.positionData=t,this.context.wizard.requestUpdate()}beforeAdvance(){this.context.setStoreData(this.key,this.getValue()),this.initSummaryTask()}getSummaryData(){const t=this.context.getStoreData(this.key),e=t.pageUid,s=u.find(i=>i.value===t.insertPosition);this.summaryTask.status===c.INITIAL&&this.summaryTask.run([e]);const a=this.summaryTask.render({complete:i=>o`${i.title} [pages:${i.uid}]`,error:()=>this.context.wizard.renderError(n.get("pageSelectPositionError")),pending:()=>this.context.wizard.renderLoader()});return[{label:n.get("pageSelectPosition"),value:o`${a} (${s?.label})`}]}handleInsertPositionChange(t){this.setValue({pageUid:t.detail.pageUid,insertPosition:t.detail.position})}handleInsertPositionConfirm(){this.context.dispatchAutoAdvance()}initSummaryTask(){this.summaryTask=new l(this.context.wizard,{task:async([t])=>await(await new h(TYPO3.settings.ajaxUrls.wizard_page_get_page_detail).withQueryArguments({pageUid:t}).get()).resolve(),autoRun:!1})}}export{r as PositionStep,r as default};