text: import{LitElement as e,html as t,css as i}from"../../lit-element/lit-element.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"},hidePreferencesButton:{type:Boolean,attribute:"hide-preferences-button"},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)},500))}}_computeRedirectOnSave(e){return void 0!==e}_noticeTagChanges(e,t,i,a,o){if(s.ready){if(e){const t=s.validTagList;s.validTagList=[...t,...e]}setTimeout(()=>{s.haxTray.hidePanelOps=t,s.haxTray.offsetMargin=i,s.haxTray.hidePreferencesButton=a,s.haxTray.elementAlign=o,setTimeout(()=>{this.openDefault&&(s.editMode=!0)},500)},0)}}_storeReady(e){setTimeout(()=>{this._noticeTagChanges(this.allowedTags,this.hidePanelOps,this.offsetMargin,this.hidePreferencesButton,this.elementAlign),this.__applyMO(),window.removeEventListener("hax-store-ready",this._storeReady.bind(this),{once:!0,passive:!0})},0)}_appstoreLoaded(e){setTimeout(()=>{this.ready=!0,window.removeEventListener("hax-store-app-store-loaded",this._appstoreLoaded.bind(this),{once:!0,passive:!0})},0)}constructor(){super(),this.ready=!1,window.addEventListener("hax-store-ready",this._storeReady.bind(this),{once:!0,passive:!0}),window.addEventListener("hax-store-app-store-loaded",this._appstoreLoaded.bind(this),{once:!0,passive:!0}),window.addEventListener("hax-save",this._saveFired.bind(this)),window.addEventListener("hax-cancel",this._cancelFired.bind(this)),this.__lock=!1,this.endPoint=null,this.openDefault=!1,this.hidePanelOps=!1,this.hidePreferencesButton=!1,this.elementAlign="right",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","hidePreferencesButton","elementAlign"].includes(t)&&this._noticeTagChanges(this.allowedTags,this.hidePanelOps,this.offsetMargin,this.hidePreferencesButton,this.elementAlign)})}disconnectedCallback(){this._observer&&(this._observer.disconnect(),this._observer=null),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(()=>{window.location=this.redirectLocation},0)}async _saveFired(e){this.endPoint&&(s.skipExitTrap=!0,s.editMode&&(s.editMode=!1),this.shadowRoot.querySelector("#pageupdateajax").body=await s.activeHaxBody.haxToContent(),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}});window.dispatchEvent(e),this.dispatchEvent(new CustomEvent("cms-hax-saved",{bubbles:!0,cancelable:!0,composed:!0,detail:!0})),this.redirectOnSave&&setTimeout(()=>{window.location=this.redirectLocation},500)}}}window.customElements.define(CmsHax.tag,CmsHax);export{CmsHax};