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 i}from"../../../@polymer/polymer/lib/utils/async.js";import"../../../@polymer/iron-ajax/iron-ajax.js";import"../../../@polymer/paper-spinner/paper-spinner.js";import{wipeSlot as l}from"../../utils/utils.js";class CMSBlock extends t{static get template(){return e`
      <style>
        :host {
          display: block;
          min-width: 112px;
          min-height: 112px;
          transition: 0.6s all ease;
          background-color: transparent;
        }
        :host([hax-edit-mode]) #replacementcontent {
          pointer-events: none;
        }
        paper-spinner {
          visibility: hidden;
          opacity: 0;
          height: 80px;
          width: 80px;
          padding: 16px;
        }
        #replacementcontent {
          visibility: visible;
          opacity: 1;
        }
        :host([loading]) {
          text-align: center;
        }
        :host([loading]) paper-spinner {
          visibility: visible;
          opacity: 1;
        }
        :host([loading]) #replacementcontent {
          opacity: 0;
          visibility: hidden;
        }
      </style>
      <iron-ajax
        id="blockrequest"
        method="GET"
        params="[[bodyData]]"
        url="[[blockEndPoint]]"
        handle-as="json"
        last-response="{{blockData}}"
      ></iron-ajax>
      <paper-spinner active="[[loading]]"></paper-spinner>
      <span id="replacementcontent"><slot></slot></span>
    `}static get tag(){return"cms-block"}static get properties(){return{loading:{type:Boolean,reflectToAttribute:!0,value:!1},blockModule:{type:String,reflectToAttribute:!0},blockDelta:{type:String,reflectToAttribute:!0},blockEndPoint:{type:String},bodyData:{type:Object,computed:"_generateBodyData(blockModule, blockDelta)",observer:"_blockChanged"},blockData:{type:String,observer:"_handleblockResponse"},blockPrefix:{type:String,observer:"["},blockSuffix:{type:String,observer:"]"},haxEditMode:{type:Boolean,value:!1,attribute:"hax-edit-mode",reflectToAttribute:!0}}}_generateBodyData(e,t){if(null!==e&&""!==e&&null!==t&&""!==t)return{module:`${e}`,delta:`${t}`}}haxHooks(){return{editModeChanged:"haxeditModeChanged",activeElementChanged:"haxactiveElementChanged"}}haxactiveElementChanged(e,t){t&&(this.haxEditMode=t)}haxeditModeChanged(e){this.haxEditMode=e}_handleblockResponse(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),l(this),i.run((()=>{let t=globalThis.document.createElement("span");t.innerHTML=e.content;let o=t.cloneNode(!0);this.appendChild(o),setTimeout((()=>{this.loading=!1,globalThis.WCAutoload&&globalThis.WCAutoload.process()}),600)})))}_blockChanged(e,t){void 0===e||""===e||this.loading||(void 0===this.blockEndPoint&&void 0!==globalThis.cmsblockEndPoint&&(this.blockEndPoint=globalThis.cmsblockEndPoint),this.blockEndPoint&&(this.loading=!0,i.run((()=>{this.shadowRoot.querySelector("#blockrequest").generateRequest()}))))}connectedCallback(){if(super.connectedCallback(),void 0!==this.blockModule&&null!==this.blockModule&&""!==this.blockModule){0!==o.getFlattenedNodes(this).length||this.loading||(void 0===this.blockEndPoint&&void 0!==globalThis.cmsblockEndPoint&&(this.blockEndPoint=globalThis.cmsblockEndPoint),this.blockEndPoint&&(this.loading=!0,i.run((()=>{this.shadowRoot.querySelector("#blockrequest").generateRequest()}))))}}static get haxProperties(){return{canScale:!0,canEditSource:!0,gizmo:{title:"CMS Block",description:"CMS block rendered on the backend",icon:"image:crop-square",color:"light-blue",tags:["Other","elmsln","cms","block"],handles:[{type:"cmsblock",block:"block"}],meta:{author:"HAXTheWeb core team"}},settings:{configure:[{property:"blockModule",title:"Module",description:"Module to load from our CMS",inputMethod:"textfield",icon:"editor:title"},{property:"blockDelta",title:"Delta",description:"Delta of the block to load from our CMS",inputMethod:"textfield",icon:"editor:title"}],advanced:[]},saveOptions:{wipeSlot:!0,unsetAttributes:["loading","block-data","body-data","hax-edit-mode","block-end-point"]}}}postProcessgetHaxJSONSchema(e){return e.properties.__editThis={type:"string",component:{name:"a",properties:{id:"cmstokenidtolockonto",href:"",target:"_blank"},slot:"Edit this block"}},e}}customElements.define(CMSBlock.tag,CMSBlock);export{CMSBlock};