var GaugeWidget_1,__decorate=this&&this.__decorate||function(t,e,i,s){var r,n=arguments.length,o=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(t,e,i,s);else for(var a=t.length-1;a>=0;a--)(r=t[a])&&(o=(n<3?r(o):n>3?r(e,i,o):r(e,i))||o);return n>3&&o&&Object.defineProperty(e,i,o),o};import{html as t,css as e}from"lit";import{customElement as i,state as s}from"lit/decorators.js";import{OrAssetWidget as r}from"../util/or-asset-widget";import{GaugeSettings as n}from"../settings/gauge-settings";import{when as o}from"lit/directives/when.js";import"@openremote/or-gauge";function getDefaultWidgetConfig(){return{attributeRefs:[],thresholds:[[0,"#4caf50"],[75,"#ff9800"],[90,"#ef5350"]],decimals:0,min:0,max:100,valueType:"number"}}let styling=e`
  #error-txt {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
  }
`,GaugeWidget=GaugeWidget_1=class extends r{constructor(){super(...arguments),this._loading=!1}static getManifest(){return{displayName:"Gauge",displayIcon:"gauge",minColumnWidth:1,minColumnHeight:1,getContentHtml:t=>new GaugeWidget_1(t),getSettingsHtml:t=>new n(t),getDefaultConfig:()=>getDefaultWidgetConfig()}}refreshContent(t){this.loadAssets(this.widgetConfig.attributeRefs)}static get styles(){return[...super.styles,styling]}willUpdate(t){if(t.has("widgetConfig")&&this.widgetConfig){let t=this.widgetConfig.attributeRefs,e=null==t?void 0:t.filter(t=>this.isAttributeRefLoaded(t));(null==e?void 0:e.length)!==(t?t.length:0)&&this.loadAssets(t)}return super.willUpdate(t)}loadAssets(t){if(0===t.length){this._error="noAttributesConnected";return}this._loading=!0,this._error=void 0,this.fetchAssets(t).then(e=>{this.loadedAssets=e,this.assetAttributes=null==t?void 0:t.map(t=>{let i=e.findIndex(e=>e.id===t.id),s=i>=0?e[i]:void 0;return s&&s.attributes?[i,s.attributes[t.name]]:void 0}).filter(t=>!!t)}).catch(t=>{this._error=t.message}).finally(()=>{this._loading=!1})}render(){return t`
            ${o(this._loading||this._error,()=>this._loading?t`<or-loading-indicator></or-loading-indicator>`:t`<or-translate id="error-txt" .value="${this._error}"></or-translate>`,()=>o(this.loadedAssets&&this.assetAttributes&&this.loadedAssets.length>0&&this.assetAttributes.length>0,()=>t`
                    <or-gauge .asset="${this.loadedAssets[0]}" .assetAttribute="${this.assetAttributes[0]}" .thresholds="${this.widgetConfig.thresholds}"
                              .decimals="${this.widgetConfig.decimals}" .min="${this.widgetConfig.min}" .max="${this.widgetConfig.max}"
                              style="height: 100%; overflow: hidden;">
                    </or-gauge>
                `,()=>t`
                    <div style="height: 100%; display: flex; justify-content: center; align-items: center; text-align: center;">
                        <span><or-translate value="noAttributeConnected"></or-translate></span>
                    </div>
                `))}
        `}};__decorate([s()],GaugeWidget.prototype,"_loading",void 0),GaugeWidget=GaugeWidget_1=__decorate([i("gauge-widget")],GaugeWidget);export{GaugeWidget};