import{LngLat as t,LngLatBounds as e}from"maplibre-gl";import{AssetModelUtil as r}from"@openremote/model";import{Util as n}from"@openremote/core";export function getLngLat(e){if(e){if(e instanceof t)return e;if(e.lat)return e.lon?{lng:e.lon,lat:e.lat}:{lng:e.lng,lat:e.lat};if(Array.isArray(e))return{lng:e[0],lat:e[1]};if(e.coordinates&&Array.isArray(e.coordinates)&&e.coordinates.length>=2)return getLngLat(e.coordinates);if(e.attributes){let t=e.attributes.location;return t&&t.value?getLngLat(t.value):void 0}if(e.value)return getLngLat(e.value)}}export function getGeoJSONPoint(t){if(t)return Array.isArray(t)?{type:"Point",coordinates:t}:{type:"Point",coordinates:[t.hasOwnProperty("lng")?t.lng:t.lon,t.lat]}}export function getLngLatBounds(t){if(t){if(t instanceof e)return t;if(2===t.length){let r=getLngLat(t[0]),n=getLngLat(t[1]);if(r&&n)return new e(r,n)}if(4===t.length)return new e([t[0],t[1],t[2],t[3]])}}export function getMarkerIconAndColorFromAssetType(t,e){if(!t)return;let n="string"==typeof t?r.getAssetDescriptor(t):t,o=n&&n.icon?n.icon:"help-circle",i=n&&n.colour?n.colour:void 0;if(e&&e.markerConfig){let t=e.markerConfig,r=e.currentValue;if("range"===t.type&&t.ranges&&"number"==typeof r){let e=t.ranges;i=(e.sort((t,e)=>e.min-t.min).find(t=>r>=t.min)||e.reduce((t,e)=>e.min>t.min?t:e)).colour||void 0}else"boolean"===t.type?i=t[!!r+""]:"string"===t.type&&(i=t[r+""])}return{color:i,icon:o}}export function isWebglSupported(){if(window.WebGLRenderingContext){let t=document.createElement("canvas");try{let e=t.getContext("webgl2")||t.getContext("webgl");if(e&&"function"==typeof e.getParameter)return!0}catch(t){}}return!1}export function isAssetWithLocation(t){if(!t.attributes)return!1;let e=t.attributes.location;return!!e.value&&(!e.meta||!e.meta.hasOwnProperty("showOnDashboard")||!!n.getMetaValue("showOnDashboard",e))}