import{html as e,PolymerElement as t}from"../../../@polymer/polymer/polymer-element.js";import{FlattenedNodesObserver as i}from"../../../@polymer/polymer/lib/utils/flattened-nodes-observer.js";import{microTask as o}from"../../../@polymer/polymer/lib/utils/async.js";import{wipeSlot as n}from"../../utils/utils.js";import"../../../@polymer/iron-ajax/iron-ajax.js";import"../../../@polymer/paper-spinner/paper-spinner.js";class CMSViews 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="viewsrequest"
        method="GET"
        params="[[bodyData]]"
        url="[[viewsEndPoint]]"
        handle-as="json"
        last-response="{{viewsData}}"
      ></iron-ajax>
      <paper-spinner active="[[loading]]"></paper-spinner>
      <span id="replacementcontent"><slot></slot></span>
    `}static get tag(){return"cms-views"}static get properties(){return{loading:{type:Boolean,reflectToAttribute:!0,value:!1},viewsName:{type:String,reflectToAttribute:!0},viewsDisplay:{type:String,reflectToAttribute:!0},viewsEndPoint:{type:String},bodyData:{type:Object,computed:"_generateBodyData(viewsName, viewsDisplay)",observer:"_viewsChanged"},viewsData:{type:String,observer:"_handleviewsResponse"},viewsPrefix:{type:String,observer:"["},viewsSuffix:{type:String,observer:"]"},haxEditMode:{type:Boolean,value:!1,attribute:"hax-edit-mode",reflectToAttribute:!0}}}haxHooks(){return{editModeChanged:"haxeditModeChanged",activeElementChanged:"haxactiveElementChanged"}}haxactiveElementChanged(e,t){t&&(this.haxEditMode=t)}haxeditModeChanged(e){this.haxEditMode=e}_generateBodyData(e,t){if(null!==e&&""!==e)return{name:`${e}`,display:`${t}`}}_handleviewsResponse(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),n(this),o.run((()=>{let t=globalThis.document.createElement("span");t.innerHTML=e.content;let i=t.cloneNode(!0);this.appendChild(i),setTimeout((()=>{this.loading=!1,globalThis.WCAutoload&&globalThis.WCAutoload.process()}),600)})))}_viewsChanged(e,t){void 0===e||""===e||this.loading||(void 0===this.viewsEndPoint&&void 0!==globalThis.cmsviewsEndPoint&&(this.viewsEndPoint=globalThis.cmsviewsEndPoint),this.viewsEndPoint&&(this.loading=!0,o.run((()=>{this.shadowRoot.querySelector("#viewsrequest").generateRequest()}))))}connectedCallback(){if(super.connectedCallback(),void 0!==this.viewsName&&null!==this.viewsName&&""!==this.viewsName){0!==i.getFlattenedNodes(this).length||this.loading||(void 0===this.viewsEndPoint&&void 0!==globalThis.cmsviewsEndPoint&&(this.viewsEndPoint=globalThis.cmsviewsEndPoint),this.viewsEndPoint&&(this.loading=!0,o.run((()=>{this.shadowRoot.querySelector("#viewsrequest").generateRequest()}))))}}static get haxProperties(){return{canScale:!0,canEditSource:!0,gizmo:{title:"CMS View",description:"CMS views rendered on the backend",icon:"icons:view-module",color:"light-blue",tags:["Other","elmsln","cms","block"],handles:[{type:"cmsviews",views:"views"}],meta:{author:"HAXTheWeb core team"}},settings:{configure:[{property:"viewsName",title:"Name",description:"Name of the view from our CMS",inputMethod:"textfield",icon:"editor:title"},{property:"viewsDisplay",title:"Display",description:"Display within that view from our CMS",inputMethod:"textfield",icon:"editor:title"}],advanced:[]},saveOptions:{wipeSlot:!0,unsetAttributes:["loading","views-data","body-data","hax-edit-mode","views-end-point"]}}}postProcessgetHaxJSONSchema(e){return e.properties.__editThis={type:"string",component:{name:"a",properties:{id:"cmstokenidtolockonto",href:"",target:"_blank"},slot:"Edit this view"}},e}}customElements.define(CMSViews.tag,CMSViews);export{CMSViews};