/*
 * 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 l from"@typo3/core/ajax/ajax-request.js";import{ModuleStateStorage as a}from"@typo3/backend/storage/module-state-storage.js";const u=f=>{class c extends f{constructor(){super(...arguments),this.selectActiveNodeInLoadedNodes=t=>{let e=a.current(this.moduleStateType);if(!e.identifier)return;const{nodes:r}=t.detail,i=this.transformModuleStateIdentifierToNodeIdentifier(e.treeIdentifier),o=this.transformModuleStateIdentifierToNodeIdentifier(e.identifier),n=r.find(s=>e.treeIdentifier!==null&&s.__treeIdentifier===i||e.treeIdentifier===null&&s.identifier===o);if(!n)return;e.treeIdentifier===null&&(e=a.updateWithTreeIdentifier(this.moduleStateType,this.transformNodeIdentifierToModuleStateIdentifier(n.identifier),this.transformNodeIdentifierToModuleStateIdentifier(n.__treeIdentifier))),n.checked=!0;const d=r.find(s=>s.__treeIdentifier===n.__parents.join("_"));d&&!d.__expanded&&this.tree.updateComplete.then(()=>this.tree.expandNodeParents(n))},this.fetchActiveNodeIfMissing=async()=>{const t=a.current(this.moduleStateType);t.identifier&&(this.tree.nodes.find(e=>e.checked)||await this.selectActiveViaRootline(t.identifier))},this.moduleStateUpdated=async t=>{const e=t.detail.state.identifier;if(!this.tree||e&&e===t.detail.oldState.identifier&&this.tree.nodes.find(d=>d.checked))return;if(!e){console.error("invalid identifier",t.detail);return}this.tree.loading&&await this.tree.loadComplete;const r=this.transformModuleStateIdentifierToNodeIdentifier(e),i=this.tree.nodes.find(d=>d.identifier===r),o=this.tree.nodes.find(d=>d.checked);if(i&&i===o){await this.tree.expandNodeParents(i);return}const n=!1;if(i){await this.selectActiveNodeByParents(e,i.__parents,n);return}await this.selectActiveViaRootline(e)}}connectedCallback(){super.connectedCallback(),document.addEventListener("typo3:module-state-storage:update:"+this.moduleStateType,this.moduleStateUpdated)}disconnectedCallback(){super.disconnectedCallback(),document.removeEventListener("typo3:module-state-storage:update:"+this.moduleStateType,this.moduleStateUpdated)}transformModuleStateIdentifierToNodeIdentifier(t){return t}transformNodeIdentifierToModuleStateIdentifier(t){return t}async selectActiveViaRootline(t){const e=new URL(this.tree.settings.rootlineUrl,window.location.origin);e.searchParams.set("identifier",t);const r=await new l(e.toString()).get({cache:"no-cache"}),{rootline:i}=await r.resolve();i.pop(),await this.selectActiveNodeByParents(t,i.map(n=>this.transformModuleStateIdentifierToNodeIdentifier(n)),!1)}async selectActiveNodeByParents(t,e,r=!0){await this.tree.expandParents(e);const i=this.transformModuleStateIdentifierToNodeIdentifier(t),o=this.tree.nodes.find(n=>n.identifier===i);o&&this.tree.selectNode(o,r)}}return c};export{u as TreeModuleState};
