import{html as e,PolymerElement as t}from"../../../@polymer/polymer/polymer-element.js";import{FlattenedNodesObserver as o}from"../../../@polymer/polymer/lib/utils/flattened-nodes-observer.js";import{microTask as n}from"../../../@polymer/polymer/lib/utils/async.js";import"../../../@polymer/iron-ajax/iron-ajax.js";import"../../../@polymer/paper-spinner/paper-spinner.js";import{wipeSlot as i}from"../../utils/utils.js";class CMSToken extends t{static get template(){return e`
      <style>
        :host {
          display: inline;
          min-width: 112px;
          min-height: 112px;
          transition: 0.6s all ease;
          background-color: transparent;
        }
        :host([hax-edit-mode]) #replacementcontent {
          pointer-events: none;
        }

        paper-spinner {
          transition: 0.6s all ease;
          position: absolute;
          visibility: hidden;
          display: none;
          opacity: 0;
          height: 0;
          width: 0;
        }

        #replacementcontent {
          transition: 0.6s all ease;
          visibility: visible;
          opacity: 1;
          height: auto;
          width: auto;
        }

        :host([loading]) {
          text-align: center;
        }

        :host([loading]) paper-spinner {
          visibility: visible;
          opacity: 1;
          position: relative;
          height: 80px;
          width: 80px;
          padding: 16px;
          display: flex;
        }

        :host([loading]) #replacementcontent {
          opacity: 0;
          visibility: hidden;
          height: 0;
          width: 0;
        }
      </style>
      <iron-ajax
        id="tokenrequest"
        method="GET"
        params="[[bodyData]]"
        url="[[tokenEndPoint]]"
        handle-as="json"
        last-response="{{tokenData}}"
      ></iron-ajax>
      <paper-spinner active="[[loading]]"></paper-spinner>
      <span id="replacementcontent">
        <slot></slot>
      </span>
    `}static get tag(){return"cms-token"}static get properties(){return{loading:{type:Boolean,reflectToAttribute:!0,value:!1},token:{type:String,reflectToAttribute:!0},tokenEndPoint:{type:String},bodyData:{type:Object,computed:"_generateBodyData(token, _clickInvoked)",observer:"_tokenChanged"},_clickInvoked:{type:String,value:!1},tokenData:{type:String,observer:"_handleTokenResponse"},tokenPrefix:{type:String,value:"["},tokenSuffix:{type:String,value:"]"},_displayMode:{type:String,value:"full",observer:"_displayModeChanged"},haxEditMode:{type:Boolean,value:!1,attribute:"hax-edit-mode",reflectToAttribute:!0}}}_displayModeChanged(e,t){void 0!==e&&""!=e&&void 0!==this.token&&(this.token=this.token.replace(t,e))}_generateBodyData(e,t){if(null!==e&&""!==e){return{token:`${this.tokenPrefix}${e}${this.tokenSuffix}`,cachedResponse:t}}}_handleTokenResponse(e,t){null!==e&&void 0!==e.content&&(null!=globalThis.document.getElementById("cmstokenidtolockonto")&&(document.getElementById("cmstokenidtolockonto").setAttribute("href",e.editEndpoint),globalThis.document.getElementById("cmstokenidtolockonto").innerHTML=e.editText,document.getElementById("cmstokenidtolockonto").addEventListener("click",this.__tokenClicked.bind(this))),i(this),n.run((()=>{let t=globalThis.document.createElement("template");t.innerHTML=e.content,this.appendChild(globalThis.document.importNode(t.content,!0)),setTimeout((()=>{globalThis.WCAutoload&&globalThis.WCAutoload.process()}),0),this.loading=!1})))}_tokenChanged(e,t){void 0===e||""===e||this.loading||(void 0===this.tokenEndPoint&&void 0!==globalThis.cmstokenEndPoint&&(this.tokenEndPoint=globalThis.cmstokenEndPoint),this.tokenEndPoint&&(this.loading=!0,n.run((()=>{this.shadowRoot.querySelector("#tokenrequest").generateRequest()}))))}_windowVisibilityChanged(e){!this.loading&&this._clickInvoked&&(this.shadowRoot.querySelector("#tokenrequest").generateRequest(),this._clickInvoked=!1)}__tokenClicked(e){this._clickInvoked=!0}connectedCallback(){if(super.connectedCallback(),void 0!==this.token&&null!==this.token&&""!==this.token){0!==o.getFlattenedNodes(this).length||this.loading||(void 0===this.tokenEndPoint&&void 0!==globalThis.cmstokenEndPoint&&(this.tokenEndPoint=globalThis.cmstokenEndPoint),this.tokenEndPoint&&(this.loading=!0,n.run((()=>{this.shadowRoot.querySelector("#tokenrequest").generateRequest()}))))}globalThis.document.addEventListener("visibilitychange",this._windowVisibilityChanged.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){this.windowControllers.abort(),super.disconnectedCallback()}haxHooks(){return{editModeChanged:"haxeditModeChanged",activeElementChanged:"haxactiveElementChanged"}}haxactiveElementChanged(e,t){t&&(this.haxEditMode=t)}haxeditModeChanged(e){this.haxEditMode=e}constructor(){super(),this.windowControllers=new AbortController}static get haxProperties(){return{canScale:!0,canEditSource:!0,gizmo:{title:"CMS Token",description:"CMS token rendered on the backend",icon:"icons:code",color:"light-blue",tags:["Other","elmsln","cms","block"],handles:[{type:"cmstoken",token:"token"}],meta:{author:"HAXTheWeb core team"}},settings:{configure:[{property:"token",title:"Token",description:"Token from our CMS",inputMethod:"textfield",icon:"editor:title"}],advanced:[]},saveOptions:{wipeSlot:!0,unsetAttributes:["loading","token-data","body-data","hax-edit-mode","token-end-point"]}}}postProcessgetHaxJSONSchema(e){let t="",o="Edit";if(void 0!==this.tokenData)for(var n in t=this.tokenData.editEndpoint,o=this.tokenData.editText,this.tokenData.schema)e.properties[n]=this.tokenData.schema[n];return e.properties.__editThis={type:"string",component:{name:"a",properties:{id:"cmstokenidtolockonto",href:t,target:"_blank"},slot:o}},e}}customElements.define(CMSToken.tag,CMSToken);export{CMSToken};