/*
 * 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 h from"@typo3/backend/storage/client.js";import"@typo3/backend/input/clearable.js";import u from"@typo3/core/document-service.js";import f from"@typo3/core/event/debounce-event.js";import m from"@typo3/core/event/regular-event.js";import p from"mark.js";import S from"@typo3/backend/utility/dom-helper.js";class g{constructor(){this.searchValueSelector=".t3js-collapse-search-term",this.searchValue="",this.markInstances=[],u.ready().then(()=>{if(this.treeContainers=document.querySelectorAll(".t3js-collapse-states-search-tree"),this.treeContainers.length!==0){this.numberOfSearchMatchesContainer=document.querySelectorAll(".t3js-collapse-states-search-numberOfSearchMatches"),this.searchField=document.querySelector(this.searchValueSelector),this.searchForm=this.searchField.closest("form"),this.searchSessionKey=this.searchField.dataset.persistCollapseSearchKey,this.searchValue=h.get(this.searchSessionKey)??"",this.registerEvents();for(let e=0;e<this.treeContainers.length;e++)this.markInstances[e]=new p(this.treeContainers[e]);this.searchValue!==""&&(this.searchField.value=this.searchValue,this.searchField.dispatchEvent(new Event("keyup")),this.searchForm.requestSubmit())}})}registerEvents(){this.searchField.clearable({onClear:e=>{e.closest("form").requestSubmit()}}),new f("input",()=>{this.searchForm.requestSubmit()}).bindTo(this.searchField),new m("submit",e=>{e.preventDefault();for(let t=0;t<this.treeContainers.length;t++)this.filterTree(this.searchField.value,this.treeContainers[t],this.numberOfSearchMatchesContainer[t],this.markInstances[t])}).bindTo(this.searchForm)}filterTree(e,t,s,a){if(e=e.toLowerCase(),a.unmark(),h.set(this.searchSessionKey,e),e.length<3){s.classList.add("hidden");return}const n=new Set,c=[...this.findNodesByIdentifier(e,t),...this.findNodesByValue(e,t),...this.findNodesByComment(e,t),...this.findNodesByConstantSubstitution(e,t)];s.innerText=String(TYPO3.lang["collapse-state-search.numberOfSearchMatches"]).replace("%s",String(c.length)),s.classList.remove("hidden"),c.forEach(r=>{if(r===null)return;const o=r.parentElement.querySelector('[data-bs-toggle="collapse"]')?.dataset.bsTarget;o!==void 0&&n.add(o.substring(1));const l=S.parents(r,".collapse");for(const i of l)n.add(i.id)});const d=Array.from(t.querySelectorAll(".collapse"));for(const r of d){const o=r.classList.contains("show"),l=r.id;if(n.has(l)){if(!o){const i=document.querySelector('[data-bs-target="#'+l+'"]');i.classList.remove("collapsed"),i.setAttribute("aria-expanded","true"),r.classList.add("show")}}else if(o){const i=document.querySelector('[data-bs-target="#'+l+'"]');i.classList.add("collapsed"),i.setAttribute("aria-expanded","false"),r.classList.remove("show")}}a.mark(e,{element:"span",className:"text-highlight"})}findNodesByIdentifier(e,t){return Array.from(t.querySelectorAll(".treelist-label")).filter(s=>s.textContent.toLowerCase().includes(e))}findNodesByValue(e,t){return Array.from(t.querySelectorAll(".treelist-value")).filter(a=>a.textContent.toLowerCase().includes(e)).map(a=>a.previousElementSibling)}findNodesByComment(e,t){return Array.from(t.querySelectorAll(".treelist-comment")).filter(s=>s.textContent.toLowerCase().includes(e))}findNodesByConstantSubstitution(e,t){return Array.from(t.querySelectorAll(".treelist-constant-substitution")).filter(s=>s.textContent.toLowerCase().includes(e))}}var y=new g;export{y as default};
