import{LitElement as e,html as t,css as i}from"../../lit/index.js";import"../../@polymer/iron-ajax/iron-ajax.js";import"../h-a-x/h-a-x.js";import{HAXStore as s}from"../hax-body/lib/hax-store.js";class CmsHax extends e{static get styles(){return[i`
        :host {
          display: block;
          font-size: 16px;
          box-sizing: content-box;
        }
      `]}render(){return t`
      <iron-ajax
        id="pageupdateajax"
        url="${this.endPoint}"
        method="${this.method}"
        content-type="application/json"
        handle-as="json"
        @response="${this._handleUpdateResponse}"
      ></iron-ajax>
      <h-a-x app-store="${this.__appStore}"></h-a-x>
    `}static get tag(){return"cms-hax"}decodeHTMLEntities(e){for(var t=[["amp","&"],["apos","'"],["#x27","'"],["#x2F","/"],["#39","'"],["#47","/"],["lt","<"],["gt",">"],["nbsp"," "],["quot",'"']],i=0,s=t.length;i<s;++i)e=e.replace(new RegExp("&"+t[i][0]+";","g"),t[i][1]);return e}static get properties(){return{ready:{type:Boolean},openDefault:{type:Boolean,reflect:!0,attribute:"open-default"},hidePanelOps:{type:Boolean,attribute:"hide-panel-ops"},offsetMargin:{type:String,reflect:!0,attribute:"offset-margin"},elementAlign:{type:String,attribute:"element-align"},allowedTags:{type:Array,attribute:"allowed-tags"},endPoint:{type:String,attribute:"end-point"},method:{type:String},appStoreConnection:{type:String,attribute:"app-store-connection"},__appStore:{type:String},syncBody:{type:Boolean,attribute:"sync-body"},bodyValue:{type:String,attribute:"body-value"},hideMessage:{type:Boolean,attribute:"hide-message"},redirectLocation:{type:String,attribute:"redirect-location"},redirectOnSave:{type:Boolean,attribute:"redirect-on-save"},__imported:{type:Boolean}}}_activeHaxBodyUpdated(e){if(!this.__imported){this.__imported=!0;let e=this.querySelector("template");null!=e&&(s.activeHaxBody.importContent(e.innerHTML),setTimeout((()=>{this.openDefault&&(s.editMode=!0)}),2e3))}}_computeRedirectOnSave(e){return void 0!==e}_noticeTagChanges(e,t,i,o){if(s.ready){if(e){const t=s.validTagList;s.validTagList=[...t,...e]}setTimeout((()=>{s.haxTray.hidePanelOps=t,s.haxTray.offsetMargin=i,s.elementAlign=o,setTimeout((()=>{this.openDefault&&(s.editMode=!0)}),2e3)}),0)}}_storeReady(e){setTimeout((()=>{this._noticeTagChanges(this.allowedTags,this.hidePanelOps,this.offsetMargin,this.elementAlign),this.__applyMO(),this.windowControllersReady.abort()}),0)}_appstoreLoaded(e){setTimeout((()=>{this.ready=!0,this.windowControllersLoaded.abort()}),0)}constructor(){super(),this.windowControllers=new AbortController,this.windowControllersReady=new AbortController,this.windowControllersLoaded=new AbortController,this.ready=!1,globalThis.addEventListener("hax-store-ready",this._storeReady.bind(this),{once:!0,passive:!0,signal:this.windowControllersReady.signal}),globalThis.addEventListener("hax-store-app-store-loaded",this._appstoreLoaded.bind(this),{once:!0,passive:!0,signal:this.windowControllersLoaded.signal}),globalThis.addEventListener("hax-save-body-value",this._saveFired.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("hax-cancel",this._cancelFired.bind(this),{signal:this.windowControllers.signal}),this.__lock=!1,this.endPoint=null,this.openDefault=!1,this.hidePanelOps=!1,this.elementAlign="left",this.method="PUT",this.syncBody=!1,this.bodyValue="",this.hideMessage=!1,this.__imported=!1,setTimeout((()=>{import("./lib/cms-token.js"),import("./lib/cms-block.js"),import("./lib/cms-views.js"),import("./lib/cms-entity.js")}),0)}_makeAppStore(e){this.__appStore=this.decodeHTMLEntities(e)}updated(e){super.updated&&super.updated(e),e.forEach(((e,t)=>{"redirectLocation"==t&&(this.redirectOnSave=this._computeRedirectOnSave(this[t])),"ready"==t&&this.ready&&this.shadowRoot&&this._activeHaxBodyUpdated(this.ready),"appStoreConnection"==t&&this._makeAppStore(this[t]),["allowedTags","hidePanelOps","offsetMargin","elementAlign"].includes(t)&&this._noticeTagChanges(this.allowedTags,this.hidePanelOps,this.offsetMargin,this.elementAlign)}))}disconnectedCallback(){this._observer&&(this._observer.disconnect(),this._observer=null),this.windowControllers.abort(),super.disconnectedCallback()}async __applyMO(){!this._observer&&this.syncBody&&s.activeHaxBody&&(this._observer=new MutationObserver((async e=>{this.__lock||(this.__lock=!0,this.dispatchEvent(new CustomEvent("hax-body-content-changed",{bubbles:!0,cancelable:!0,composed:!0,detail:await s.activeHaxBody.haxToContent()})),setTimeout((()=>{this.__lock=!1}),100))})),this._observer.observe(s.activeHaxBody,{childList:!0,subtree:!0}))}_cancelFired(e){s.skipExitTrap=!0,s.editMode=!1,this.redirectOnSave&&setTimeout((()=>{globalThis.location=this.redirectLocation}),0)}async _saveFired(e){this.endPoint&&(s.skipExitTrap=!0,s.editMode&&(s.editMode=!1),this.shadowRoot.querySelector("#pageupdateajax").body=e.detail.value,this.shadowRoot.querySelector("#pageupdateajax").generateRequest())}_handleUpdateResponse(e){if(!this.hideMessage){const e=new CustomEvent("simple-toast-show",{bubbles:!0,cancelable:!0,composed:!0,detail:{text:"Saved!",duration:3e3}});globalThis.dispatchEvent(e),this.dispatchEvent(new CustomEvent("cms-hax-saved",{bubbles:!0,cancelable:!0,composed:!0,detail:!0})),this.redirectOnSave&&setTimeout((()=>{globalThis.location=this.redirectLocation}),2e3)}}}customElements.define(CmsHax.tag,CmsHax);export{CmsHax};