import{html as t,PolymerElement as e}from"../../../@polymer/polymer/polymer-element.js";import{FlattenedNodesObserver as i}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 o}from"../../utils/utils.js";class CMSEntity extends e{static get template(){return t`
      <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="entityrequest"
        method="GET"
        params="[[bodyData]]"
        url="[[entityEndPoint]]"
        handle-as="json"
        last-response="{{entityData}}"
      ></iron-ajax>
      <paper-spinner active="[[loading]]"></paper-spinner>
      <span id="replacementcontent"><slot></slot></span>
    `}static get tag(){return"cms-entity"}static get properties(){return{loading:{type:Boolean,reflectToAttribute:!0,value:!1},entityType:{type:String,reflectToAttribute:!0},entityId:{type:String,reflectToAttribute:!0},entityDisplayMode:{type:String,reflectToAttribute:!0},entityEndPoint:{type:String},bodyData:{type:Object,computed:"_generateBodyData(entityType, entityId, entityDisplayMode)",observer:"_entityChanged"},entityData:{type:String,observer:"_handleEntityResponse"},entityPrefix:{type:String,observer:"["},entitySuffix:{type:String,observer:"]"},haxEditMode:{type:Boolean,value:!1,attribute:"hax-edit-mode",reflectToAttribute:!0}}}haxHooks(){return{editModeChanged:"haxeditModeChanged",activeElementChanged:"haxactiveElementChanged"}}haxactiveElementChanged(t,e){e&&(this.haxEditMode=e)}haxeditModeChanged(t){this.haxEditMode=t}_generateBodyData(t,e,i){if(null!==t&&""!==t&&null!==e&&""!==e)return{type:`${t}`,id:`${e}`,display_mode:`${i}`}}_handleEntityResponse(t,e){null!==t&&void 0!==t.content&&(null!=globalThis.document.getElementById("cmstokenidtolockonto")&&(document.getElementById("cmstokenidtolockonto").setAttribute("href",t.editEndpoint),globalThis.document.getElementById("cmstokenidtolockonto").innerHTML=t.editText),o(this),n.run((()=>{let e=globalThis.document.createElement("span");e.innerHTML=t.content;let i=e.cloneNode(!0);this.appendChild(i),setTimeout((()=>{this.loading=!1,globalThis.WCAutoload&&globalThis.WCAutoload.process()}),600)})))}_entityChanged(t,e){void 0===t||""===t||this.loading||(void 0===this.entityEndPoint&&void 0!==globalThis.cmsentityEndPoint&&(this.entityEndPoint=globalThis.cmsentityEndPoint),this.entityEndPoint&&(this.loading=!0,n.run((()=>{this.shadowRoot.querySelector("#entityrequest").generateRequest()}))))}connectedCallback(){if(super.connectedCallback(),void 0!==this.entity&&null!==this.entity&&""!==this.entity){0!==i.getFlattenedNodes(this).length||this.loading||(void 0===this.entityEndPoint&&void 0!==globalThis.cmsentityEndPoint&&(this.entityEndPoint=globalThis.cmsentityEndPoint),this.entityEndPoint&&(this.loading=!0,n.run((()=>{this.shadowRoot.querySelector("#entityrequest").generateRequest()}))))}}static get haxProperties(){return{canScale:!0,canEditSource:!0,gizmo:{title:"CMS Entity",description:"CMS entity rendered on the backend",icon:"places:spa",color:"light-blue",tags:["Other","elmsln","cms","block"],handles:[{type:"cmsentity",entity:"entity"}],meta:{author:"HAXTheWeb core team"}},settings:{configure:[{property:"entityType",title:"Type",description:"type from our CMS",inputMethod:"select",options:{node:"Node",user:"User",file:"File"},icon:"editor:title"},{property:"entityID",title:"ID",description:"id from our CMS",inputMethod:"textfield",icon:"editor:title"},{property:"entityDisplayMode",title:"Display mode",description:"display mode from our CMS",inputMethod:"textfield",icon:"editor:title"}],advanced:[]},saveOptions:{wipeSlot:!0,unsetAttributes:["loading","entity-data","body-data","hax-edit-mode","entity-end-point"]}}}postProcessgetHaxJSONSchema(t){return t.properties.__editThis={type:"string",component:{name:"a",properties:{id:"cmstokenidtolockonto",href:"",target:"_blank"},slot:"Edit this content"}},t}}customElements.define(CMSEntity.tag,CMSEntity);export{CMSEntity};