var __decorate=this&&this.__decorate||function(t,e,r,i){var s,o=arguments.length,a=o<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,r,i);else for(var n=t.length-1;n>=0;n--)(s=t[n])&&(a=(o<3?s(a):o>3?s(e,r,a):s(e,r))||a);return o>3&&a&&Object.defineProperty(e,r,a),a},__awaiter=this&&this.__awaiter||function(t,e,r,i){return new(r||(r=Promise))(function(s,o){function a(t){try{c(i.next(t))}catch(t){o(t)}}function n(t){try{c(i.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?s(t.value):((e=t.value)instanceof r?e:new r(function(t){t(e)})).then(a,n)}c((i=i.apply(t,e||[])).next())})};import{customElement as t,property as e}from"lit/decorators.js";import{OrMapMarker as r}from"./or-map-marker";import{AssetModelUtil as i}from"@openremote/model";import s,{subscribe as o,Util as a}from"@openremote/core";import{getMarkerIconAndColorFromAssetType as n}from"../util";export function getMarkerConfigForAssetType(t,e){if(t&&e&&t[e])return t[e]}export function getMarkerConfigAttributeName(t,e){let r=getMarkerConfigForAssetType(t,e);if(r)return r.attributeName}let OrMapMarkerAsset=class extends o(s)(r){constructor(){super(),this.assetTypeAsIcon=!0,this.visible=!1}set type(t){let e,r=getMarkerConfigForAssetType(this.config,t);if(r&&r.attributeName&&this.asset&&this.asset.attributes&&this.asset.attributes[r.attributeName]&&r.colours){let t=this.asset.attributes[r.attributeName].value;e={markerConfig:r.colours,currentValue:t}}let i=n(t,e);if(!i){this.visible=!1;return}this.assetTypeAsIcon&&(this.icon=i.icon),this.markerColor=Array.isArray(i.color)?i.color[0].colour:i.color||void 0,this.updateColor(this.markerContainer),this.visible=!0}shouldUpdate(t){if(t.has("assetId")&&(this.lat=void 0,this.lng=void 0,this.type=void 0,this.direction=void 0,this.displayValue=void 0,this.assetIds=this.assetId&&this.assetId.length>0?[this.assetId]:void 0,1===Object.keys(t).length))return!1;if(t.has("asset"))try{this.onAssetChanged(this.asset)}catch(t){console.error(t)}return super.shouldUpdate(t)}_onEvent(t){if("attribute"===t.eventType)return"location"===t.ref.name?void this._updateLocation(t.value):void(this.asset&&(this.asset=a.updateAsset(this.asset,t),this.requestUpdate()));if("asset"===t.eventType)switch(t.cause){case"READ":case"CREATE":case"UPDATE":this.onAssetChanged(t.asset);break;case"DELETE":this.onAssetChanged(void 0)}}onAssetChanged(t){return __awaiter(this,void 0,void 0,function*(){if(t){this.direction=void 0,this.displayValue=void 0;let e=t.attributes?t.attributes.location:void 0;this._updateLocation(e?e.value:null);let r=getMarkerConfigForAssetType(this.config,t.type),s=!r||!r.hideDirection,o=r&&!0===r.showLabel&&!!r.attributeName,n=!!(r&&!1!==r.showUnits);if(o&&t.attributes&&t.attributes[null==r?void 0:r.attributeName]){let e=t.attributes[r.attributeName],s=i.getAttributeAndValueDescriptors(t.type,e.name,e);this.displayValue=a.getAttributeValueAsString(e,s[0],t.type,n,"-")}if(s&&t.attributes&&t.attributes.direction){let e=t.attributes.direction.value;null!=e&&(this.direction=e.toString())}this.type=t.type}else this.lat=void 0,this.lng=void 0})}_updateLocation(t){this.lat=t&&t.coordinates?t.coordinates[1]:void 0,this.lng=t&&t.coordinates?t.coordinates[0]:void 0}getColor(){return this.markerColor&&!this.color?"#"+this.markerColor:super.getColor()}getActiveColor(){return this.markerColor&&!this.activeColor?"#"+this.markerColor:super.getActiveColor()}};__decorate([e({type:String,reflect:!0,attribute:!0})],OrMapMarkerAsset.prototype,"assetId",void 0),__decorate([e({type:Object,attribute:!0})],OrMapMarkerAsset.prototype,"asset",void 0),__decorate([e()],OrMapMarkerAsset.prototype,"config",void 0),OrMapMarkerAsset=__decorate([t("or-map-marker-asset")],OrMapMarkerAsset);export{OrMapMarkerAsset};