var MapWidget_1,__decorate=this&&this.__decorate||function(e,t,s,i){var o,r=arguments.length,a=r<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,s):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(e,t,s,i);else for(var n=e.length-1;n>=0;n--)(o=e[n])&&(a=(r<3?o(a):r>3?o(t,s,a):o(t,s))||a);return r>3&&a&&Object.defineProperty(t,s,a),a},__awaiter=this&&this.__awaiter||function(e,t,s,i){return new(s||(s=Promise))(function(o,r){function a(e){try{l(i.next(e))}catch(e){r(e)}}function n(e){try{l(i.throw(e))}catch(e){r(e)}}function l(e){var t;e.done?o(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(a,n)}l((i=i.apply(e,t||[])).next())})};import{html as e}from"lit";import{customElement as t}from"lit/decorators.js";import{OrAssetWidget as s}from"../util/or-asset-widget";import{MapSettings as i}from"../settings/map-settings";import{when as o}from"lit/directives/when.js";import r,{Util as a}from"@openremote/core";import{showSnackbar as n}from"@openremote/or-mwc-components/or-mwc-snackbar";import"@openremote/or-map";function getDefaultWidgetConfig(){return{attributeRefs:[],showLabels:!1,showUnits:!1,showGeoJson:!0,boolColors:{type:"boolean",false:"#ef5350",true:"#4caf50"},textColors:[["example","4caf50"],["example2","ef5350"]],thresholds:[[0,"#4caf50"],[75,"#ff9800"],[90,"#ef5350"]],assetTypes:[],assetType:void 0,allOfType:!0,assetIds:[],attributes:[]}}let MapWidget=MapWidget_1=class extends s{constructor(){super(...arguments),this.markers={}}static getManifest(){return{displayName:"Map",displayIcon:"map",minColumnWidth:2,minColumnHeight:2,getContentHtml:e=>new MapWidget_1(e),getSettingsHtml:e=>new i(e),getDefaultConfig:()=>getDefaultWidgetConfig()}}refreshContent(e){this.loadAssets()}updated(e){e.has("widgetConfig")&&this.widgetConfig&&this.loadAssets()}loadAssets(){return __awaiter(this,void 0,void 0,function*(){this.widgetConfig.assetType&&this.widgetConfig.attributeName&&this.widgetConfig.allOfType?this.fetchAssetsByType([this.widgetConfig.assetType],this.widgetConfig.attributeName).then(e=>{this.loadedAssets=e}):this.widgetConfig.assetType&&this.widgetConfig.attributeName&&!this.widgetConfig.allOfType&&this.fetchAssetsById([this.widgetConfig.assetType],this.widgetConfig.attributeName,this.widgetConfig.assetIds).then(e=>{this.loadedAssets=e})})}fetchAssetsById(e,t,s){return __awaiter(this,void 0,void 0,function*(){let i=[];return yield r.rest.api.AssetResource.queryAssets({realm:{name:r.displayRealm},select:{attributes:[t,"location"]},types:e,ids:s}).then(e=>{i=e.data,this.markers={}}).catch(e=>{console.error(e),n(void 0,"errorOccurred")}),i})}fetchAssetsByType(e,t){return __awaiter(this,void 0,void 0,function*(){let s=[];return yield r.rest.api.AssetResource.queryAssets({realm:{name:r.displayRealm},select:{attributes:[t,"location"]},types:e}).then(e=>{s=e.data,this.markers={}}).catch(e=>{console.error(e),n(void 0,"errorOccurred")}),s})}render(){return e`
            <div style="height: 100%; display: flex; flex-direction: column; overflow: hidden;">
                <or-map id="miniMap" class="or-map" .zoom="${this.widgetConfig.zoom}" .center="${this.widgetConfig.center}" .showGeoJson="${this.widgetConfig.showGeoJson}" style="flex: 1;">
                    ${o(this.loadedAssets,()=>this.getMarkerTemplates())}
                </or-map>
            </div>
        `}getMarkerTemplates(){return this.loadedAssets.filter(e=>{if(!e.attributes)return!1;let t=e.attributes.location;return!t||!t.meta||!t.meta.hasOwnProperty("showOnDashboard")||!!a.getMetaValue("showOnDashboard",t)}).map(t=>{if(this.markers)if(this.markers[t.type]={attributeName:this.widgetConfig.attributeName},this.markers[t.type].showUnits=this.widgetConfig.showUnits,this.markers[t.type].showLabel=this.widgetConfig.showLabels,"boolean"==this.widgetConfig.valueType)this.widgetConfig.boolColors.true=this.widgetConfig.boolColors.true.replace("#",""),this.widgetConfig.boolColors.false=this.widgetConfig.boolColors.false.replace("#",""),this.markers[t.type].colours=this.widgetConfig.boolColors;else if("text"==this.widgetConfig.valueType){var s={type:"string"};this.widgetConfig.textColors.map(e=>{s[e[0]]=e[1].replace("#","")}),this.markers[t.type].colours=s}else{var i=[];this.widgetConfig.thresholds.sort((e,t)=>e[0]>t[0]?-1:1).map((e,t)=>{var s={min:e[0],colour:e[1].replace("#","")};i.push(s)}),this.markers[t.type].colours={type:"range",ranges:i}}return e`
                <or-map-marker-asset .asset="${t}" .config="${this.markers}"></or-map-marker-asset>
            `})}};MapWidget=MapWidget_1=__decorate([t("map-widget")],MapWidget);export{MapWidget};