/*
 * 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"@typo3/backend/form-engine/element/select-tree.js";import"@typo3/backend/form-engine/element/select-tree-toolbar.js";import"@typo3/backend/element/icon-element.js";import n from"@typo3/core/document-service.js";import s from"@typo3/backend/form-engine.js";class o{constructor(e,t,i,r){if(this.recordField=null,this.tree=null,this.selectNode=a=>{const d=a.detail.node;this.updateAncestorsIndeterminateState(d),this.calculateIndeterminate(this.tree.nodes),this.saveCheckboxes(),this.tree.setup.input.dispatchEvent(new Event("change",{bubbles:!0,cancelable:!0}))},this.loadDataAfter=a=>{this.tree.nodes=a.detail.nodes.map(d=>(d.__indeterminate=!1,d)),this.calculateIndeterminate(this.tree.nodes)},this.saveCheckboxes=()=>{typeof this.recordField>"u"||(this.recordField.value=this.tree.getSelectedNodes().map(a=>a.identifier).join(","))},i instanceof Function)throw new Error("Function `callback` is not supported anymore since TYPO3 v12.0");n.ready().then(()=>{this.initialize(e,t,r)})}initialize(e,t,i){this.recordField=document.getElementById(t);const r=document.getElementById(e);this.tree=document.createElement("typo3-backend-form-selecttree"),this.tree.addEventListener("typo3:tree:nodes-prepared",this.loadDataAfter),this.tree.addEventListener("typo3:tree:node-selected",this.selectNode),i instanceof Array&&this.tree.addEventListener("typo3:tree:node-selected",()=>{s.processOnFieldChange(i)});const a={id:e,dataUrl:this.generateRequestUrl(),readOnlyMode:parseInt(this.recordField.dataset.readOnly,10)===1,input:this.recordField,exclusiveNodesIdentifiers:this.recordField.dataset.treeExclusiveKeys,validation:JSON.parse(this.recordField.dataset.formengineValidationRules)[0],expandUpToLevel:this.recordField.dataset.treeExpandUpToLevel,unselectableElements:[]};this.tree.addEventListener("tree:initialized",()=>{if(this.recordField.dataset.treeShowToolbar){const d=document.createElement("typo3-backend-form-selecttree-toolbar");d.tree=this.tree,this.tree.prepend(d)}}),this.tree.setup=a,r.append(this.tree)}generateRequestUrl(){const e={tableName:this.recordField.dataset.tablename,fieldName:this.recordField.dataset.fieldname,uid:this.recordField.dataset.uid,defaultValues:this.recordField.dataset.defaultvalues,overrideValues:this.recordField.dataset.overridevalues,recordTypeValue:this.recordField.dataset.recordtypevalue,dataStructureIdentifier:this.recordField.dataset.datastructureidentifier,flexFormSheetName:this.recordField.dataset.flexformsheetname,flexFormFieldName:this.recordField.dataset.flexformfieldname,flexFormContainerName:this.recordField.dataset.flexformcontainername,flexFormContainerIdentifier:this.recordField.dataset.flexformcontaineridentifier,flexFormContainerFieldName:this.recordField.dataset.flexformcontainerfieldname,flexFormSectionContainerIsNew:this.recordField.dataset.flexformsectioncontainerisnew,command:this.recordField.dataset.command};return TYPO3.settings.ajaxUrls.record_tree_data+"&"+new URLSearchParams(e).toString()}updateAncestorsIndeterminateState(e){let t=!1;e.__treeParents.forEach(i=>{const r=this.tree.getNodeByTreeIdentifier(i);r.__indeterminate=e.checked||e.__indeterminate||t,t=r.checked||r.__indeterminate||e.checked||e.__indeterminate})}calculateIndeterminate(e){e.forEach(t=>{(t.checked||t.__indeterminate)&&t.__treeParents&&t.__treeParents.length>0&&t.__treeParents.forEach(i=>{const r=this.tree.getNodeByTreeIdentifier(i);r.__indeterminate=!0})})}}export{o as SelectTreeElement};
