var BarChartWidget_1,__decorate=this&&this.__decorate||function(t,e,i,r){var o,a=arguments.length,s=a<3?e:null===r?r=Object.getOwnPropertyDescriptor(e,i):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,i,r);else for(var n=t.length-1;n>=0;n--)(o=t[n])&&(s=(a<3?o(s):a>3?o(e,i,s):o(e,i))||s);return a>3&&s&&Object.defineProperty(e,i,s),s};import{html as t}from"lit";import{when as e}from"lit/directives/when.js";import i from"moment";import{OrAssetWidget as r}from"../util/or-asset-widget";import{customElement as o,state as a}from"lit/decorators.js";import{BarChartSettings as s}from"../settings/barchart-settings";import"@openremote/or-attribute-barchart";import{BarChartInterval as n,OrAttributeBarChart as d}from"@openremote/or-attribute-barchart";function getDefaultWidgetConfig(){let t=d.getDefaultTimeWindowOptions().get("Day"),e=i().subtract(t[1],t[0]).startOf(t[0]),r=1===t[1]?i().endOf(t[0]):i();return{attributeRefs:[],attributeColors:[],attributeSettings:{},datapointQuery:{type:"interval",fromTimestamp:e.toDate().getTime(),toTimestamp:r.toDate().getTime()},chartOptions:{options:{scales:{y:{min:void 0,max:void 0},y1:{min:void 0,max:void 0}}}},showTimestampControls:!1,defaultTimeWindowKey:"Day",defaultTimePrefixKey:"this",defaultInterval:n.AUTO,showLegend:!0,stacked:!1,decimals:2}}let BarChartWidget=BarChartWidget_1=class extends r{constructor(){super(...arguments),this._loading=!1}static getManifest(){return{displayName:"Bar Chart",displayIcon:"chart-bar",minColumnWidth:2,minColumnHeight:2,getContentHtml:t=>new BarChartWidget_1(t),getSettingsHtml(t){let e=new s(t);return e.setTimeWindowOptions(d.getDefaultTimeWindowOptions()),e.setTimePrefixOptions(d.getDefaultTimePrefixOptions()),e.setIntervalOptions(d.getDefaultIntervalOptions()),e},getDefaultConfig:()=>getDefaultWidgetConfig()}}refreshContent(t){if(t)this.widgetConfig=structuredClone(this.widgetConfig);else{let t=structuredClone(this.widgetConfig.datapointQuery);t.fromTimestamp=void 0,t.toTimestamp=void 0,this.datapointQuery=t}}willUpdate(t){if(!this.widgetConfig.datapointQuery&&(this.widgetConfig.datapointQuery=this.getDefaultQuery(),t.has("widgetConfig")||t.set("widgetConfig",this.widgetConfig)),t.has("widgetConfig")&&this.widgetConfig){this.datapointQuery=this.widgetConfig.datapointQuery;let t=this.widgetConfig.attributeRefs;0===t.length?this._error="noAttributesConnected":(null==t?void 0:t.filter(t=>!this.isAttributeRefLoaded(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),r=i>=0?e[i]:void 0;return r&&r.attributes?[i,r.attributes[t.name]]:void 0}).filter(t=>!!t)}).catch(t=>{this._error=t.message}).finally(()=>{this._loading=!1})}render(){return t`
            ${e(this._loading,()=>t`
                <or-loading-indicator></or-loading-indicator>
                
            `,()=>e(this._error,()=>t`
                <div style="height: 100%; display: flex; justify-content: center; align-items: center; text-align: center;">
                    <span><or-translate .value="${this._error}"></or-translate></span>
                </div>
                
            `,()=>{var e,i,r,o,a,s,n,d,l,g;return t`
                    <or-attribute-barchart .assets="${this.loadedAssets}" .assetAttributes="${this.assetAttributes}"
                                           .attributeColors="${null==(e=this.widgetConfig)?void 0:e.attributeColors}" .attributeConfig="${null==(i=this.widgetConfig)?void 0:i.attributeSettings}"
                                           ?showLegend=${null==(r=this.widgetConfig)?void 0:r.showLegend} ?stacked=${null==(o=this.widgetConfig)?void 0:o.stacked}
                                           .timestampControls="${!(null==(a=this.widgetConfig)?void 0:a.showTimestampControls)}"
                                           .interval="${null==(s=this.widgetConfig)?void 0:s.defaultInterval}"
                                           .timePrefixKey="${null==(n=this.widgetConfig)?void 0:n.defaultTimePrefixKey}"
                                           .timeWindowKey="${null==(d=this.widgetConfig)?void 0:d.defaultTimeWindowKey}"
                                           .datapointQuery="${this.datapointQuery}" .chartOptions="${null==(l=this.widgetConfig)?void 0:l.chartOptions}"
                                           .decimals="${null==(g=this.widgetConfig)?void 0:g.decimals}"
                                           style="height: 100%"
                    ></or-attribute-barchart>
                `}))}
        `}getDefaultQuery(){return{type:"interval",fromTimestamp:i().subtract(30,"days").toDate().getTime(),toTimestamp:i().toDate().getTime()}}};__decorate([a()],BarChartWidget.prototype,"datapointQuery",void 0),__decorate([a()],BarChartWidget.prototype,"_loading",void 0),BarChartWidget=BarChartWidget_1=__decorate([o("barchart-widget")],BarChartWidget);export{BarChartWidget};