/*
 * 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{ScaffoldIdentifierEnum as c}from"@typo3/backend/enum/viewport/scaffold-identifier.js";import n from"@typo3/backend/storage/persistent.js";var t;(function(l){l.sidebarExpanded="scaffold-sidebar-expanded",l.sidebarFlyout="scaffold-sidebar-flyout",l.toolbarExpanded="scaffold-toolbar-expanded"})(t||(t={}));class i extends CustomEvent{static{this.eventName="typo3:scaffold:sidebar:toggle"}constructor(e){super(i.eventName,{detail:{expanded:e},bubbles:!0,composed:!0})}}class a extends CustomEvent{static{this.eventName="typo3:scaffold:toolbar:toggle"}constructor(e){super(a.eventName,{detail:{expanded:e},bubbles:!0,composed:!0})}}class r extends CustomEvent{static{this.eventName="typo3:scaffold:toolbar:toggle-request"}constructor(){super(r.eventName,{bubbles:!0,composed:!0})}}class o extends CustomEvent{static{this.eventName="typo3:scaffold:search:toggle-request"}constructor(){super(o.eventName,{bubbles:!0,composed:!0})}}class d{static{this.STORAGE_KEY="typo3-sidebar-collapsed"}static{this.LARGE_SCREEN_QUERY="(min-width: 992px)"}static{this.mediaQuery=null}static{this.sidebarPreferExpanded=!0}static isSidebarExpanded(){return this.getScaffold()?.classList.contains(t.sidebarExpanded)??!1}static isSidebarFlyout(){return this.getScaffold()?.classList.contains(t.sidebarFlyout)??!1}static isSidebarVisible(){return this.isLargeScreen()?!0:this.isSidebarFlyout()}static isToolbarExpanded(){return this.getScaffold()?.classList.contains(t.toolbarExpanded)??!1}static isLargeScreen(){return window.matchMedia(this.LARGE_SCREEN_QUERY).matches}static initialize(){if(this.mediaQuery=window.matchMedia(this.LARGE_SCREEN_QUERY),this.mediaQuery.addEventListener("change",this.handleMediaQueryChange),document.body.dataset.context==="install"){const e=localStorage.getItem(this.STORAGE_KEY)==="true";this.sidebarPreferExpanded=!e}else this.sidebarPreferExpanded=this.isSidebarExpanded();this.initializeOverlay(),this.initializeEventListeners(),this.applyStateForViewport()}static toggleSidebar(e){this.getScaffold()&&(this.isLargeScreen()?this.toggleSidebarExpanded(e):this.toggleSidebarFlyout(e))}static toggleToolbar(e){const s=this.getScaffold();s&&(this.isLargeScreen()||(typeof e>"u"&&(e=!this.isToolbarExpanded()),s.classList.toggle(t.toolbarExpanded,e),e&&(s.classList.remove(t.sidebarExpanded),this.toggleSidebarFlyout(!1)),document.dispatchEvent(new a(e))))}static collapseAll(){const e=this.getScaffold();e&&e.classList.remove(t.sidebarExpanded,t.sidebarFlyout,t.toolbarExpanded)}static toggleSidebarExpanded(e){const s=this.getScaffold();s&&(e=e??!this.isSidebarExpanded(),this.sidebarPreferExpanded=e,s.classList.toggle(t.sidebarExpanded,e),s.classList.remove(t.sidebarFlyout,t.toolbarExpanded),this.persistSidebarState(e),document.dispatchEvent(new i(e)))}static toggleSidebarFlyout(e){const s=this.getScaffold();s&&(this.isLargeScreen()||(e=e??!this.isSidebarFlyout(),s.classList.toggle(t.sidebarFlyout,e),e&&s.classList.remove(t.sidebarExpanded,t.toolbarExpanded),document.dispatchEvent(new i(e))))}static initializeEventListeners(){document.addEventListener("typo3-module-load",()=>{this.toggleSidebarFlyout(!1),this.toggleToolbar(!1)}),document.addEventListener(r.eventName,()=>{this.toggleToolbar()}),document.addEventListener(o.eventName,()=>{this.collapseAll()})}static applyStateForViewport(){const e=this.getScaffold();e&&(this.isLargeScreen()?(e.classList.remove(t.sidebarFlyout),e.classList.toggle(t.sidebarExpanded,this.sidebarPreferExpanded)):e.classList.remove(t.sidebarExpanded,t.sidebarFlyout),document.dispatchEvent(new i(this.isSidebarVisible())))}static{this.handleMediaQueryChange=()=>{d.applyStateForViewport()}}static persistSidebarState(e){document.body.dataset.context==="install"?localStorage.setItem(this.STORAGE_KEY,e?"false":"true"):n.set("BackendComponents.States.typo3-sidebar",{collapsed:!e})}static getScaffold(){return document.querySelector(c.scaffold)}static initializeOverlay(){document.querySelector(".scaffold-overlay")?.addEventListener("click",s=>{s.preventDefault(),d.toggleSidebar(!1)})}}export{i as ScaffoldSidebarToggleEvent,d as ScaffoldState,t as ScaffoldStateClass,a as ScaffoldToolbarToggleEvent,o as SearchToggleRequestEvent,r as ToolbarToggleRequestEvent};
