var ChartWidget_1,__decorate=this&&this.__decorate||function(t,e,i,o){var s,n=arguments.length,r=n<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,i):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(t,e,i,o);else for(var a=t.length-1;a>=0;a--)(s=t[a])&&(r=(n<3?s(r):n>3?s(e,i,r):s(e,i))||r);return n>3&&r&&Object.defineProperty(e,i,r),r};import{html as t}from"lit";import{when as e}from"lit/directives/when.js";import i from"moment";import{OrAssetWidget as o}from"../util/or-asset-widget";import{customElement as s,state as n}from"lit/decorators.js";import{ChartSettings as r}from"../settings/chart-settings";import"@openremote/or-chart";function getDefaultTimeWindowOptions(){return new Map([["60Minutes",["minutes",60]],["Hour",["hours",1]],["6Hours",["hours",6]],["24Hours",["hours",24]],["Day",["days",1]],["7Days",["days",7]],["Week",["weeks",1]],["30Days",["days",30]],["Month",["months",1]],["90Days",["days",90]],["6Months",["months",6]],["365Days",["days",365]],["Year",["years",1]]])}function getDefaultTimePrefixOptions(){return["this","last","next"]}function getDefaultSamplingOptions(){return new Map([["lttb","lttb"]])}function getDefaultWidgetConfig(){let t=getDefaultTimePrefixOptions()[0],[e,o]=Array.from(getDefaultTimeWindowOptions().entries())[0],s=i().subtract(o[1],o[0]).startOf(o[0]),n=1===o[1]?i().endOf(o[0]):i();return{attributeRefs:[],attributeColors:[],datapointQuery:{type:"lttb",fromTimestamp:s.toDate().getTime(),toTimestamp:n.toDate().getTime()},chartOptions:{options:{scales:{y:{min:void 0,max:void 0},y1:{min:void 0,max:void 0}}}},showTimestampControls:!1,defaultTimePresetKey:t+e,showLegend:!0,showZoomBar:!1,stacked:!1}}let ChartWidget=ChartWidget_1=class extends o{constructor(){super(...arguments),this._loading=!1}static getManifest(){return{displayName:"Line Chart",displayIcon:"chart-line",minColumnWidth:2,minColumnHeight:2,getContentHtml:t=>new ChartWidget_1(t),getSettingsHtml(t){let e=new r(t);return e.setTimeWindowOptions(getDefaultTimeWindowOptions()),e.setTimePrefixOptions(getDefaultTimePrefixOptions()),e.setSamplingOptions(getDefaultSamplingOptions()),e},getDefaultConfig:()=>getDefaultWidgetConfig()}}refreshContent(t){if(t)this.widgetConfig=JSON.parse(JSON.stringify(this.widgetConfig));else{let t=JSON.parse(JSON.stringify(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=>{var i;let o=e.findIndex(e=>e.id===t.id),s=o>=0?e[o]:void 0,n=null==(i=null==s?void 0:s.attributes)?void 0:i[t.name];return n?[o,n]:[-1,{name:t.name}]})}).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,o,s,n,r,a,d,l,g,u,h;let f=getDefaultTimePrefixOptions(),m=null!=(e=f.find(t=>{var e,i;return null==(i=null==(e=this.widgetConfig)?void 0:e.defaultTimePresetKey)?void 0:i.startsWith(t)}))?e:f[0],p=getDefaultTimeWindowOptions(),[C,c]=null==(o=null==(i=this.widgetConfig)?void 0:i.defaultTimePresetKey)?void 0:o.split(m);return c&&p.has(c)||(c=Array.from(p.keys())[0]),t`
                    <or-chart .assets="${this.loadedAssets}" .assetAttributes="${this.assetAttributes}"
                              .attributeColors="${null==(s=this.widgetConfig)?void 0:s.attributeColors}" ?stacked="${null==(n=this.widgetConfig)?void 0:n.stacked}"
                              .attributeConfig="${(null==(r=this.widgetConfig)?void 0:r.attributeConfig)!=null?this.widgetConfig.attributeConfig:{}}"
                              .showLegend="${(null==(a=this.widgetConfig)?void 0:a.showLegend)==null||(null==(d=this.widgetConfig)?void 0:d.showLegend)}"
                              .showZoomBar="${(null==(l=this.widgetConfig)?void 0:l.showZoomBar)==null||(null==(g=this.widgetConfig)?void 0:g.showZoomBar)}"
                              .attributeControls="${!1}" .timestampControls="${!(null==(u=this.widgetConfig)?void 0:u.showTimestampControls)}"
                              .timePrefixOptions="${f}" .timeWindowOptions="${p}"
                              .timePrefixKey="${m}" .timeWindowKey="${c}"
                              .datapointQuery="${this.datapointQuery}" .chartOptions="${null==(h=this.widgetConfig)?void 0:h.chartOptions}"
                              style="height: 100%"
                    ></or-chart>
                `}))}
        `}getDefaultQuery(){return{type:"lttb",fromTimestamp:i().set("minute",-60).toDate().getTime(),toTimestamp:i().set("minute",60).toDate().getTime()}}};__decorate([n()],ChartWidget.prototype,"datapointQuery",void 0),__decorate([n()],ChartWidget.prototype,"_loading",void 0),ChartWidget=ChartWidget_1=__decorate([s("chart-widget")],ChartWidget);export{ChartWidget};