var AttributeInputWidget_1,__decorate=this&&this.__decorate||function(t,e,i,r){var s,o=arguments.length,n=o<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,i):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,i,r);else for(var d=t.length-1;d>=0;d--)(s=t[d])&&(n=(o<3?s(n):o>3?s(e,i,n):s(e,i))||n);return o>3&&n&&Object.defineProperty(e,i,n),n};import{css as t,html as e}from"lit";import{OrAssetWidget as i}from"../util/or-asset-widget";import{customElement as r,query as s,queryAll as o,state as n}from"lit/decorators.js";import{AttributeInputSettings as d}from"../settings/attribute-input-settings";import{when as a}from"lit/directives/when.js";import{throttle as l}from"lodash";import{Util as u}from"@openremote/core";import"@openremote/or-attribute-input";function getDefaultWidgetConfig(){return{attributeRefs:[],readonly:!1,showHelperText:!0}}let styling=t`
  #widget-wrapper {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
  }
    
  #error-txt {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
  }

  .attr-input {
    width: 100%;
    box-sizing: border-box;
  }
`,AttributeInputWidget=AttributeInputWidget_1=class extends i{constructor(){super(...arguments),this._loading=!1}static getManifest(){return{displayName:"Attribute",displayIcon:"form-textbox",getContentHtml:t=>new AttributeInputWidget_1(t),getDefaultConfig:()=>getDefaultWidgetConfig(),getSettingsHtml:t=>new d(t)}}refreshContent(t){this.widgetConfig=JSON.parse(JSON.stringify(this.widgetConfig))}static get styles(){return[...super.styles,styling]}disconnectedCallback(){var t;super.disconnectedCallback(),null==(t=this.resizeObserver)||t.disconnect(),delete this.resizeObserver}willUpdate(t){if(t.has("widgetConfig")&&this.widgetConfig){let t=this.widgetConfig.attributeRefs;0===t.length?this._error="noAttributesConnected":t.length>0&&!this.isAttributeRefLoaded(t[0])&&this.loadAssets(t)}return!this.resizeObserver&&this.widgetWrapperElem&&(this.resizeObserver=new ResizeObserver(l(()=>{window.dispatchEvent(new Event("resize"))},200)),this.resizeObserver.observe(this.widgetWrapperElem)),super.willUpdate(t)}loadAssets(t){if(0===t.length){this._error="noAttributesConnected";return}this._loading=!0,this._error=void 0,this.fetchAssets(t).then(t=>{this.loadedAssets=t}).catch(t=>{this._error=t.message}).finally(()=>{this._loading=!1})}render(){var t;let i=this.widgetConfig,r=i.attributeRefs.length>0&&(null==(t=this.loadedAssets[0])?void 0:t.attributes)?this.loadedAssets[0].attributes[i.attributeRefs[0].name]:void 0,s=u.getMetaValue("readOnly",r);return e`
            
            ${a(this._loading||this._error,()=>this._loading?e`<or-loading-indicator></or-loading-indicator>`:e`<or-translate id="error-txt" .value="${this._error}"></or-translate>`,()=>a(i.attributeRefs.length>0&&r&&this.loadedAssets&&this.loadedAssets.length>0,()=>{var t,o;return e`
                    <div id="widget-wrapper">
                        <or-attribute-input class="attr-input" fullWidth
                                            .assetType="${null==(t=this.loadedAssets[0])?void 0:t.type}"
                                            .attribute="${r}"
                                            .assetId="${null==(o=this.loadedAssets[0])?void 0:o.id}"
                                            .disabled="${!this.loadedAssets}"
                                            .readonly="${i.readonly||s||this.getEditMode()}"
                                            .hasHelperText="${i.showHelperText}"
                        ></or-attribute-input>
                    </div>
                `}))}
        `}};__decorate([n()],AttributeInputWidget.prototype,"_loading",void 0),__decorate([s("#widget-wrapper")],AttributeInputWidget.prototype,"widgetWrapperElem",void 0),__decorate([o(".attr-input")],AttributeInputWidget.prototype,"attributeInputElems",void 0),AttributeInputWidget=AttributeInputWidget_1=__decorate([r("attribute-input-widget")],AttributeInputWidget);export{AttributeInputWidget};