import $ from"jquery";const NAME="Layout",DATA_KEY="lte.layout",JQUERY_NO_CONFLICT=$.fn[NAME],SELECTOR_HEADER=".main-header",SELECTOR_MAIN_SIDEBAR=".main-sidebar",SELECTOR_SIDEBAR=".main-sidebar .sidebar",SELECTOR_CONTENT=".content-wrapper",SELECTOR_CONTROL_SIDEBAR_CONTENT=".control-sidebar-content",SELECTOR_CONTROL_SIDEBAR_BTN='[data-widget="control-sidebar"]',SELECTOR_FOOTER=".main-footer",SELECTOR_PUSHMENU_BTN='[data-widget="pushmenu"]',SELECTOR_LOGIN_BOX=".login-box",SELECTOR_REGISTER_BOX=".register-box",SELECTOR_PRELOADER=".preloader",CLASS_NAME_SIDEBAR_COLLAPSED="sidebar-collapse",CLASS_NAME_SIDEBAR_FOCUSED="sidebar-focused",CLASS_NAME_LAYOUT_FIXED="layout-fixed",CLASS_NAME_CONTROL_SIDEBAR_SLIDE_OPEN="control-sidebar-slide-open",CLASS_NAME_CONTROL_SIDEBAR_OPEN="control-sidebar-open",Default={scrollbarTheme:"os-theme-light",scrollbarAutoHide:"l",panelAutoHeight:!0,panelAutoHeightMode:"min-height",preloadDuration:200,loginRegisterAutoHeight:!0};class Layout{constructor(e,t){this._config=t,this._element=e}fixLayoutHeight(e=null){const t=$("body");let o=0;(t.hasClass("control-sidebar-slide-open")||t.hasClass("control-sidebar-open")||"control_sidebar"===e)&&(o=$(".control-sidebar-content").outerHeight());const i={window:$(window).height(),header:$(".main-header").length>0?$(".main-header").outerHeight():0,footer:$(".main-footer").length>0?$(".main-footer").outerHeight():0,sidebar:$(SELECTOR_SIDEBAR).length>0?$(SELECTOR_SIDEBAR).height():0,controlSidebar:o},s=this._max(i);let a=this._config.panelAutoHeight;!0===a&&(a=0);const n=$(SELECTOR_CONTENT);!1!==a&&(s===i.controlSidebar?n.css(this._config.panelAutoHeightMode,s+a):s===i.window?n.css(this._config.panelAutoHeightMode,s+a-i.header-i.footer):n.css(this._config.panelAutoHeightMode,s+a-i.header),this._isFooterFixed()&&n.css(this._config.panelAutoHeightMode,parseFloat(n.css(this._config.panelAutoHeightMode))+i.footer)),t.hasClass("layout-fixed")&&(void 0!==$.fn.overlayScrollbars?$(SELECTOR_SIDEBAR).overlayScrollbars({className:this._config.scrollbarTheme,sizeAutoCapable:!0,scrollbars:{autoHide:this._config.scrollbarAutoHide,clickScrolling:!0}}):$(SELECTOR_SIDEBAR).css("overflow-y","auto"))}fixLoginRegisterHeight(){const e=$("body"),t=$(".login-box, .register-box");if(0===t.length)e.css("height","auto"),$("html").css("height","auto");else{const o=t.height();e.css(this._config.panelAutoHeightMode)!==o&&e.css(this._config.panelAutoHeightMode,o)}}_init(){this.fixLayoutHeight(),!0===this._config.loginRegisterAutoHeight?this.fixLoginRegisterHeight():this._config.loginRegisterAutoHeight===parseInt(this._config.loginRegisterAutoHeight,10)&&setInterval(this.fixLoginRegisterHeight,this._config.loginRegisterAutoHeight),$(SELECTOR_SIDEBAR).on("collapsed.lte.treeview expanded.lte.treeview",(()=>{this.fixLayoutHeight()})),$(".main-sidebar").on("mouseenter mouseleave",(()=>{$("body").hasClass("sidebar-collapse")&&this.fixLayoutHeight()})),$(SELECTOR_PUSHMENU_BTN).on("collapsed.lte.pushmenu shown.lte.pushmenu",(()=>{setTimeout((()=>{this.fixLayoutHeight()}),300)})),$(SELECTOR_CONTROL_SIDEBAR_BTN).on("collapsed.lte.controlsidebar",(()=>{this.fixLayoutHeight()})).on("expanded.lte.controlsidebar",(()=>{this.fixLayoutHeight("control_sidebar")})),$(window).resize((()=>{this.fixLayoutHeight()})),setTimeout((()=>{$("body.hold-transition").removeClass("hold-transition")}),50),setTimeout((()=>{const e=$(".preloader");e&&(e.css("height",0),setTimeout((()=>{e.children().hide()}),200))}),this._config.preloadDuration)}_max(e){let t=0;return Object.keys(e).forEach((o=>{e[o]>t&&(t=e[o])})),t}_isFooterFixed(){return"fixed"===$(".main-footer").css("position")}static _jQueryInterface(e=""){return this.each((function(){let t=$(this).data(DATA_KEY);const o=$.extend({},Default,$(this).data());t||(t=new Layout($(this),o),$(this).data(DATA_KEY,t)),"init"===e||""===e?t._init():"fixLayoutHeight"!==e&&"fixLoginRegisterHeight"!==e||t[e]()}))}}$(window).on("load",(()=>{Layout._jQueryInterface.call($("body"))})),$(`${SELECTOR_SIDEBAR} a`).on("focusin",(()=>{$(".main-sidebar").addClass("sidebar-focused")})).on("focusout",(()=>{$(".main-sidebar").removeClass("sidebar-focused")})),$.fn[NAME]=Layout._jQueryInterface,$.fn[NAME].Constructor=Layout,$.fn[NAME].noConflict=function(){return $.fn[NAME]=JQUERY_NO_CONFLICT,Layout._jQueryInterface};export default Layout;
