var KpiWidget_1,__decorate=this&&this.__decorate||function(t,e,i,o){var r,s=arguments.length,d=s<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)d=Reflect.decorate(t,e,i,o);else for(var n=t.length-1;n>=0;n--)(r=t[n])&&(d=(s<3?r(d):s>3?r(e,i,d):r(e,i))||d);return s>3&&d&&Object.defineProperty(e,i,d),d};import{customElement as t,state as e}from"lit/decorators.js";import{when as i}from"lit/directives/when.js";import{OrAssetWidget as o}from"../util/or-asset-widget";import{html as r}from"lit";import{KpiSettings as s}from"../settings/kpi-settings";import"@openremote/or-attribute-card";function getDefaultWidgetConfig(){return{attributeRefs:[],period:"day",decimals:0,deltaFormat:"absolute",showTimestampControls:!1}}let KpiWidget=KpiWidget_1=class extends o{constructor(){super(...arguments),this._loading=!1}static getManifest(){return{displayName:"KPI",displayIcon:"label",minColumnWidth:1,minColumnHeight:1,getContentHtml:t=>new KpiWidget_1(t),getSettingsHtml:t=>new s(t),getDefaultConfig:()=>getDefaultWidgetConfig()}}refreshContent(t){this.loadAssets(this.widgetConfig.attributeRefs)}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),o=i>=0?e[i]:void 0;return o&&o.attributes?[i,o.attributes[t.name]]:void 0}).filter(t=>!!t)}).catch(t=>{this._error=t.message}).finally(()=>{this._loading=!1})}render(){var t;return r`
            <div style="position: relative; height: 100%; overflow: hidden;">
                ${i(this._loading||this._error,()=>r`
                        <div style="position: absolute; top: -5%; width: 100%; height: 105%; background: white; z-index: 1; display: flex; justify-content: center; align-items: center; text-align: center;">
                            ${i(this._loading,()=>r`
                                <or-loading-indicator></or-loading-indicator>
                            `,()=>r`
                                <or-translate .value="${this._error}"></or-translate>
                            `)}
                        </div>
                    `)}
                <or-attribute-card .assets="${this.loadedAssets}" .assetAttributes="${this.assetAttributes}" .period="${this.widgetConfig.period}"
                                   .deltaFormat="${this.widgetConfig.deltaFormat}" .mainValueDecimals="${this.widgetConfig.decimals}"
                                   showControls="${null==(t=this.widgetConfig)?void 0:t.showTimestampControls}" showTitle="${!1}" hideAttributePicker="${!0}" style="height: 100%;">
                </or-attribute-card>
            </div>
        `}};__decorate([e()],KpiWidget.prototype,"_loading",void 0),KpiWidget=KpiWidget_1=__decorate([t("kpi-widget")],KpiWidget);export{KpiWidget};