/**
 * Copyright 2019 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text.
 */
import{store as e}from"./haxcms-site-store.js";import{autorun as t,toJS as i}from"../../../../mobx/dist/mobx.esm.js";import{varExists as s,varGet as o,localStorageGet as n,localStorageSet as a}from"../../../utils/utils.js";import"../../../simple-colors-shared-styles/simple-colors-shared-styles.js";import"../../../anchor-behaviors/anchor-behaviors.js";const HAXCMSTheme=function(n){return class extends n{constructor(){super(),this.windowControllers=new AbortController,this.HAXCMSThemeSettings={autoScroll:!1,scrollTarget:globalThis},this.__disposer=this.__disposer?this.__disposer:[],this.HAXCMSThemeWiring=new HAXCMSThemeWiring(this)}_colorChanged(e){e&&(this.hexColor=this._getHexColor(e))}_getHexColor(e){let t=e.replace("-text",""),i=globalThis.SimpleColorsSharedStyles.colors;return i[t]?i[t][6]:"#000000"}_editModeChanged(t,i){void 0!==i&&(e.editMode=t,this.__styleReapply())}_contentContainerChanged(e,t){!e||void 0!==t&&null!=t?e&&t?(this.HAXCMSThemeWiring.disconnect(this),this.HAXCMSThemeWiring.connect(this,e)):t&&null==e&&this.HAXCMSThemeWiring.disconnect(this):this.HAXCMSThemeWiring.connect(this,e)}_locationChanged(t,i){if(!t||void 0===t.route)return;if("home"==t.route.name){const t=e.routerManifest.items.find((e=>void 0!==e.id));t&&(e.activeId=t.id)}}connectedCallback(){super.connectedCallback(),setTimeout((()=>{if(0===this.childNodes.length){let t=document.createRange().createContextualFragment(e.activeItemContent);this.appendChild(t)}this.__styleReapply()}),50),t((t=>{this.activeItemContent=i(e.activeItemContent),this.__disposer.push(t)})),t((t=>{this.editMode=i(e.editMode),this.__disposer.push(t)})),t((t=>{this.isLoggedIn=i(e.isLoggedIn),this.__disposer.push(t)})),t((t=>{const n=i(e.manifest);if(n&&s(n,"title")&&(globalThis.document.title=n.title),n&&s(n,"metadata.theme.variables.cssVariable")){let e=n.metadata.theme.variables.cssVariable.replace("--simple-colors-default-theme-","").split("-");e.pop(),this.accentColor=e.join("-");var a=o(n,"metadata.theme.variables.cssVariable",null);a=null==a?o(n,"metadata.theme.variables.hexCode","#ff0074"):`var(${a})`,globalThis.document.body.style.setProperty("--haxcms-color",a)}this.__disposer.push(t)})),t((t=>{this._location=i(e.location),this.__disposer.push(t)}))}__styleReapply(){globalThis.dispatchEvent(new Event("resize"))}disconnectedCallback(){for(var e in delete this.contentContainer,this.__disposer)this.__disposer[e].dispose();super.disconnectedCallback()}resetActive(){globalThis.history.pushState(null,null,e.location.baseUrl),globalThis.dispatchEvent(new PopStateEvent("popstate")),this.dispatchEvent(new CustomEvent("haxcms-active-item-changed",{bubbles:!0,composed:!0,cancelable:!0,detail:{}}))}}};class HAXCMSThemeWiring{constructor(i,s=!0){globalThis.SimpleColorsSharedStyles.requestAvailability(),s&&null==n("HAXCMSSystemData",null)&&a("HAXCMSSystemData",{}),this.__disposer=this.__disposer?this.__disposer:[],t((t=>{e.jwt&&e.themeElement&&e.cmsSiteEditorBackend.tag&&e.themeElement.contentContainer&&e.isLoggedIn&&this.connect(e.themeElement,e.themeElement.contentContainer),this.__disposer.push(t)}))}connect(t,i){this.windowControllers&&this.windowControllers.abort(),this.windowControllers=new AbortController,globalThis.addEventListener("haxcms-active-item-changed",this._activeItemUpdate.bind(t),{signal:this.windowControllers.signal}),globalThis.addEventListener("haxcms-edit-mode-changed",this._globalEditChanged.bind(t),{signal:this.windowControllers.signal}),globalThis.addEventListener("haxcms-trigger-update",this._triggerUpdate.bind(t),{signal:this.windowControllers.signal}),this.cmsSiteEditorInstance&&globalThis.document.body.appendChild(this.cmsSiteEditorInstance),e.jwt&&e.cmsSiteEditorBackend.tag&&e.isLoggedIn&&(this.cmsSiteEditorInstance=e.cmsSiteEditorAvailability(),e.cmsSiteEditor.instance.appElement=t,e.cmsSiteEditor.instance.appendTarget=i,e.cmsSiteEditor.instance.appendTarget.appendChild(e.cmsSiteEditor.instance))}disconnect(e){this.windowControllers.abort(),this.cmsSiteEditorInstance&&globalThis.document.body.appendChild(this.cmsSiteEditorInstance)}_globalEditChanged(e){this.editMode=e.detail}_activeItemUpdate(t){let i=t.detail;if(i&&void 0!==i.id){e.activeId=i.id;const t=new CustomEvent("json-outline-schema-active-item-changed",{bubbles:!0,composed:!0,cancelable:!0,detail:i});this.dispatchEvent(t),void 0!==i.title?globalThis.document.title=e.routerManifest.title+" - "+i.title:globalThis.document.title=e.routerManifest.title}else globalThis.document.title=e.routerManifest.title}_triggerUpdate(e){this.dispatchEvent(new CustomEvent("haxcms-active-item-changed",{bubbles:!0,composed:!0,cancelable:!0,detail:{}}))}}export{HAXCMSTheme,HAXCMSThemeWiring};