/*
 * 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{LitElement as b,html as v,nothing as I}from"lit";import{customElement as y,property as u,query as w,state as P}from"lit/decorators.js";import{PageTree as N}from"@typo3/backend/tree/page-tree.js";import A from"@typo3/core/ajax/ajax-request.js";import"@typo3/backend/tree/tree-toolbar.js";import"@typo3/backend/element/icon-element.js";import"@typo3/backend/element/breadcrumb.js";import{TreeNodePositionEnum as g}from"@typo3/backend/tree/tree-node.js";import{cache as S}from"lit/directives/cache.js";import s from"~labels/core.core";var d=function(l,e,t,i){var r=arguments.length,n=r<3?e:i===null?i=Object.getOwnPropertyDescriptor(e,t):i,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(l,e,t,i);else for(var o=l.length-1;o>=0;o--)(c=l[o])&&(n=(r<3?c(n):r>3?c(e,t,n):c(e,t))||n);return r>3&&n&&Object.defineProperty(e,t,n),n};const _=[{label:s.get("insert_inside"),value:"inside",iconIdentifier:"actions-arrow-end"},{label:s.get("insert_after"),value:"after",iconIdentifier:"actions-arrow-down"}];class p extends CustomEvent{static{this.eventName="typo3:page-position-select-tree:insert-position-change"}constructor(e,t){super(p.eventName,{detail:{pageUid:e,position:t},bubbles:!0,composed:!0})}}class f extends CustomEvent{static{this.eventName="typo3:page-position-select-tree:insert-position-confirm"}constructor(e,t){super(f.eventName,{detail:{pageUid:e,position:t},bubbles:!0,composed:!0})}}let m=class extends N{async handleNodeAdd(e,t,i){this.requestUpdate()}};m=d([y("typo3-backend-component-page-position-select-tree")],m);let a=class extends b{constructor(){super(...arguments),this.activePageId=null,this.actions=["select"],this.insertPosition="inside",this.configuration=null,this.breadcrumbItems=[]}connectedCallback(){super.connectedCallback(),this.loadConfiguration()}createRenderRoot(){return this}loadConfiguration(){const e=top.TYPO3.settings.ajaxUrls.page_tree_browser_configuration,t=this.hasAttribute("alternative-entry-points")?JSON.parse(this.getAttribute("alternative-entry-points")):[];let i=new A(e);t.length&&(i=i.withQueryArguments("alternativeEntryPoints="+encodeURIComponent(t))),i.get().then(async r=>{const n=await r.resolve("json");n.actions=this.actions,this.configuration=n})}render(){return v`<typo3-breadcrumb .nodes=${this.breadcrumbItems}></typo3-breadcrumb>${this.configuration?S(this.renderTree()):I}`}renderTree(){const e=async()=>{const t=this.querySelector("typo3-backend-tree-toolbar");t.tree=this.tree,await this.tree.ensureActiveNodeLoaded(this.activePageId);const i=await this.applyActiveInsertPosition();i&&(await this.tree.updateComplete,this.tree.scrollNodeIntoViewIfNeeded(i),this.tree.focusNode(i))};return v`<typo3-backend-tree-toolbar .tree=${this.tree}></typo3-backend-tree-toolbar><typo3-backend-component-page-position-select-tree id=typo3-pagetree-tree .setup=${this.configuration} @tree:initialized=${e} @typo3:tree:node-selected=${this.handleNodeSelected} @typo3:tree:filter-applied=${()=>this.applyActiveInsertPosition()} @typo3:tree:filter-reset=${()=>this.applyActiveInsertPosition()}></typo3-backend-component-page-position-select-tree>`}async handleNodeSelected(e){const t=e.detail.node;if(this.isActivePagePositionNode(t)){this.dispatchEvent(new f(this.activePageId,this.insertPosition));return}const[i,r]=t.identifier.split("-");this.insertPosition=r??"inside",this.activePageId=Number(i);const n=/^\d+$/.test(t.identifier)?await this.applyActiveInsertPosition():this.syncActiveInsertPositionState();n&&this.tree.focusNode(n),this.dispatchEvent(new p(this.activePageId,this.insertPosition))}async applyActiveInsertPosition(){if(this.activePageId===null||this.activePageId===void 0)return null;let e=this.tree.nodes.find(t=>t.identifier===String(this.activePageId));return!e&&(await this.tree.ensureActiveNodeLoaded(this.activePageId),e=this.tree.nodes.find(t=>t.identifier===String(this.activePageId)),!e)?null:(await this.tree.expandNodeParents(e),await this.toggleDynamicInsertNodes(e),this.syncActiveInsertPositionState())}syncActiveInsertPositionState(){let e=null;return this.tree.nodes.forEach(t=>{t.checked=this.isActivePagePositionNode(t),t.checked&&(e=t)}),this.updateBreadcrumb(this.tree.nodes),this.tree.requestUpdate(),e}async toggleDynamicInsertNodes(e){const t=[];for(const o of this.tree.nodes)["-after","-inside"].some(h=>o.identifier.endsWith(h))&&t.push(o);for(const o of t)await this.tree.removeNode(o);const i={...e,__processed:!1,hasChildren:!1,loaded:!0,identifier:e.identifier+"-inside",parentIdentifier:e.identifier,name:s.get("insert_subpage"),icon:"actions-arrow-end",overlayIcon:"",tooltip:s.get("insert_inside")+" "+e.tooltip},n={...this.tree.nodes.find(o=>o.identifier===e.parentIdentifier),__processed:!1,hasChildren:!1,loaded:!0,identifier:`${e.identifier}-after`,parentIdentifier:e.parentIdentifier,name:s.get("insert_page"),icon:"actions-arrow-end",overlayIcon:"",tooltip:s.get("insert_after")+" "+e.tooltip};let c=[];e.hasChildren&&(c=this.tree.nodes.filter(o=>o.parentIdentifier===e.identifier)),await this.tree.addNode(i,e,g.INSIDE),e.identifier!=="0"&&await this.tree.addNode(n,e,g.AFTER);for(const o of c){const h={...o,__processed:!1,hasChildren:!1,loaded:!0,identifier:`${o.identifier}-after`,parentIdentifier:o.parentIdentifier,name:s.get("insert_page"),icon:"actions-arrow-end",overlayIcon:"",tooltip:s.get("insert_after")+" "+o.tooltip};await this.tree.addNode(h,o,g.AFTER)}}isActivePagePositionNode(e){return e.identifier===`${this.activePageId}-${this.insertPosition}`}updateBreadcrumb(e){let t=e.find(r=>r.identifier===String(this.activePageId));const i=[];for(;t;)i.push({identifier:t.identifier,label:t.name,icon:t.icon,iconOverlay:t.overlayIcon,url:null,forceShowIcon:!1}),t=e.find(r=>r.identifier===t.parentIdentifier);this.insertPosition==="after"&&i.shift(),i.unshift({identifier:`${this.activePageId}-${this.insertPosition}`,label:s.get("labels.createNew"),icon:"apps-pagetree-page-default",iconOverlay:"overlay-new",url:null,forceShowIcon:!1}),this.breadcrumbItems=i.reverse()}};d([u({type:Number,reflect:!0,attribute:"active-page"})],a.prototype,"activePageId",void 0),d([u({type:Array})],a.prototype,"actions",void 0),d([u({type:String,reflect:!0})],a.prototype,"insertPosition",void 0),d([w("typo3-backend-component-page-position-select-tree")],a.prototype,"tree",void 0),d([P()],a.prototype,"configuration",void 0),d([P()],a.prototype,"breadcrumbItems",void 0),a=d([y("typo3-backend-component-page-position-select")],a);export{p as InsertPositionChangeEvent,f as InsertPositionConfirmEvent,a as PagePositionSelect,m as PagePositionSelectTree,_ as insertPositionOptions};
