/*
 * 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 S from"@typo3/core/document-service.js";import d from"@typo3/core/event/regular-event.js";class u{constructor(){this.localStorageKey="collapse-states-",this.localStorageKeyDefaultSuffix="general",this.searchValueSelector=".t3js-collapse-search-term",this.searchField=null,this.searchForm=null,this.stateCache=new Map,S.ready().then(()=>{this.searchField=document.querySelector(this.searchValueSelector),this.searchField!==null&&(this.searchForm=this.searchField.closest("form"),this.searchField.value=h.get(this.searchField.dataset.persistCollapseSearchKey)??""),this.registerEventListener(),this.recoverStates()})}registerEventListener(){const a='.collapse[data-persist-collapse-state="true"]';new d("show.bs.collapse",t=>{const e=t.target;e.dataset.persistCollapseState==="true"&&(this.searchField!==null&&this.searchField.value===""||e.dataset.persistCollapseStateNotIfSearch===void 0)&&this.toStorage(e,!0)}).delegateTo(document,a),new d("hide.bs.collapse",t=>{const e=t.target;e.dataset.persistCollapseState==="true"&&(this.searchField!==null&&this.searchField.value===""||e.dataset.persistCollapseStateNotIfSearch===void 0)&&this.toStorage(e,!1)}).delegateTo(document,a),this.searchForm!==null&&new d("submit",t=>{t.preventDefault(),this.searchField!==null&&this.searchField.value===""&&this.recoverStates()}).bindTo(this.searchForm)}recoverStates(){document.querySelectorAll('.collapse[data-persist-collapse-state="true"]').forEach(t=>{const e=t.dataset.persistCollapseStateSuffix??this.localStorageKeyDefaultSuffix,l=this.fromStorage(e),s=t.id;if(s===""||!this.shallRecoverState(t))return;const o=(t.dataset.persistCollapseStateIfState??"shown")==="shown",i=(t.dataset.persistCollapseStateIfState??"hidden")==="hidden",c=t.classList.contains("show");if(o===!0){if(l[s]===!0){if(!c){const r=document.querySelector('[data-bs-target="#'+s+'"]');r.classList.remove("collapsed"),r.setAttribute("aria-expanded","true"),t.classList.add("show")}}else if(c){const r=document.querySelector('[data-bs-target="#'+s+'"]');r.classList.add("collapsed"),r.setAttribute("aria-expanded","false"),t.classList.remove("show")}}if(i===!0){if(l[s]===!1){if(c){const r=document.querySelector('[data-bs-target="#'+s+'"]');r.classList.add("collapsed"),r.setAttribute("aria-expanded","false"),t.classList.remove("show")}}else if(!c){const r=document.querySelector('[data-bs-target="#'+s+'"]');r.classList.remove("collapsed"),r.setAttribute("aria-expanded","true"),t.classList.add("show")}}})}shallRecoverState(a){return a.dataset.persistCollapseStateNotIfSearch===void 0||a.dataset.persistCollapseStateNotIfSearch==="false"?!0:this.searchField!==null&&this.searchField.value===""}fromStorage(a){let t;if(this.stateCache.has(this.localStorageKey+a))t=this.stateCache.get(this.localStorageKey+a);else{const e=h.get(this.localStorageKey+a);t=e!==null?JSON.parse(e):{},this.stateCache.set(this.localStorageKey+a,t)}return t}toStorage(a,t){const e=a.id,l=a.dataset.persistCollapseStateSuffix??this.localStorageKeyDefaultSuffix,s=this.fromStorage(l),o=(a.dataset.persistCollapseStateIfState??"shown")==="shown",i=(a.dataset.persistCollapseStateIfState??"hidden")==="hidden";t===!0&&o===!0&&s[e]!==!0&&(s[e]=!0,this.updateStates(this.localStorageKey+l,s)),t===!0&&i===!0&&s[e]===!1&&(delete s[e],this.updateStates(this.localStorageKey+l,s)),t===!1&&i===!0&&s[e]!==!1&&(s[e]=!1,this.updateStates(this.localStorageKey+l,s)),t===!1&&o===!0&&s[e]===!0&&(delete s[e],this.updateStates(this.localStorageKey+l,s))}updateStates(a,t){h.set(a,JSON.stringify(t)),this.stateCache.set(a,t)}}var n=new u;export{u as CollapseStatePersister,n as default};
