var MapWidget_1,__decorate=this&&this.__decorate||function(e,t,s,o){var i,a=arguments.length,r=a<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)r=Reflect.decorate(e,t,s,o);else for(var n=e.length-1;n>=0;n--)(i=e[n])&&(r=(a<3?i(r):a>3?i(t,s,r):i(t,s))||r);return a>3&&r&&Object.defineProperty(t,s,r),r},__awaiter=this&&this.__awaiter||function(e,t,s,o){return new(s||(s=Promise))(function(i,a){function r(e){try{d(o.next(e))}catch(e){a(e)}}function n(e){try{d(o.throw(e))}catch(e){a(e)}}function d(e){var t;e.done?i(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(r,n)}d((o=o.apply(e,t||[])).next())})};import{html as e}from"lit";import{customElement as t,query as s,state as o}from"lit/decorators.js";import{OrAssetWidget as i}from"../util/or-asset-widget";import{MapSettings as a}from"../settings/map-settings";import{Util as r,OrMapMarkersChangedEvent as n,OrMapLoadedEvent as d}from"@openremote/or-map";import{map as l}from"lit/directives/map.js";import c from"@openremote/core";import{showSnackbar as p}from"@openremote/or-mwc-components/or-mwc-snackbar";function getDefaultWidgetConfig(){return{attributeRefs:[],showLabels:!1,showUnits:!1,showGeoJson:!0,boolColors:{type:"boolean",false:"#ef5350",true:"#4caf50"},textColors:[["example1","#4caf50"],["example2","#ef5350"]],thresholds:[[0,"#4caf50"],[75,"#ff9800"],[90,"#ef5350"]],assetTypes:[],assetType:void 0,allOfType:!0,assetIds:[],attributes:[]}}let MapWidget=MapWidget_1=class extends i{constructor(){super(...arguments),this._assetsOnScreen=[],this.markers={}}static getManifest(){return{displayName:"Map",displayIcon:"map",minColumnWidth:2,minColumnHeight:2,getContentHtml:e=>new MapWidget_1(e),getSettingsHtml:e=>new a(e),getDefaultConfig:()=>getDefaultWidgetConfig()}}connectedCallback(){return this.addEventListener(d.NAME,this._onMapLoaded),this.addEventListener(n.NAME,this._onMapMarkersChanged),super.connectedCallback()}disconnectedCallback(){return this.removeEventListener(d.NAME,this._onMapLoaded),this.removeEventListener(n.NAME,this._onMapMarkersChanged),super.disconnectedCallback()}refreshContent(e){this._loadAssets()}updated(e){e.has("widgetConfig")&&this.widgetConfig&&this._loadAssets(),e.has("loadedAssets")&&this.loadedAssets&&this._loadMarkers()}_loadAssets(){this.widgetConfig.assetType&&this.widgetConfig.attributeName?this._fetchAssets([this.widgetConfig.assetType],this.widgetConfig.attributeName,this.widgetConfig.allOfType?void 0:this.widgetConfig.assetIds).then(e=>{this.loadedAssets=e}):console.debug("Could not load assets for Map widget, as the configuration is not complete! (no asset type, or no attribute)")}_fetchAssets(e,t,s){return __awaiter(this,void 0,void 0,function*(){let o=[];try{o=(yield c.rest.api.AssetResource.queryAssets({realm:{name:c.displayRealm},select:{attributes:[t,"location"]},types:e,ids:(null==s?void 0:s.length)?s:void 0})).data}catch(e){console.error(e),p(void 0,"errorOccurred")}return o})}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;">
                    ${l(this._assetsOnScreen,t=>e`
                        <or-map-marker-asset .asset="${t}" .config="${this.markers}"></or-map-marker-asset>
                    `)}
                </or-map>
            </div>
        `}_onMapLoaded(e){var t;let s=this.widgetConfig.allOfType?void 0:this.widgetConfig.assetType,o=this.loadedAssets.filter(e=>!s||e.type===s).filter(r.isAssetWithLocation);null==(t=this._map)||t.addAssets(o)}_onMapMarkersChanged(e){this._assetsOnScreen=e.detail}_loadMarkers(){for(let e of(this.markers={},new Set(this.loadedAssets.map(e=>e.type).filter(Boolean)))){let t={attributeName:this.widgetConfig.attributeName,showUnits:this.widgetConfig.showUnits,showLabel:this.widgetConfig.showLabels};switch(this.widgetConfig.valueType){case"boolean":{let e=this.widgetConfig.boolColors;t.colours={type:"boolean",true:e.true.replace("#",""),false:e.false.replace("#","")};break}case"text":t.colours=this.widgetConfig.textColors.reduce((e,[t,s])=>(e[t]=s.replace("#",""),e),{type:"string"});break;default:t.colours={type:"range",ranges:this.widgetConfig.thresholds.sort((e,t)=>t[0]-e[0]).map(([e,t])=>({min:e,colour:t.replace("#","")}))}}this.markers[e]=t}}};__decorate([o()],MapWidget.prototype,"_assetsOnScreen",void 0),__decorate([s("or-map")],MapWidget.prototype,"_map",void 0),MapWidget=MapWidget_1=__decorate([t("map-widget")],MapWidget);export{MapWidget};