var __decorate=this&&this.__decorate||function(e,t,o,r){var a,n=arguments.length,s=n<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,o):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,o,r);else for(var i=e.length-1;i>=0;i--)(a=e[i])&&(s=(n<3?a(s):n>3?a(t,o,s):a(t,o))||s);return n>3&&s&&Object.defineProperty(t,o,s),s},__awaiter=this&&this.__awaiter||function(e,t,o,r){return new(o||(o=Promise))(function(a,n){function s(e){try{p(r.next(e))}catch(e){n(e)}}function i(e){try{p(r.throw(e))}catch(e){n(e)}}function p(e){var t;e.done?a(e.value):((t=e.value)instanceof o?t:new o(function(e){e(t)})).then(s,i)}p((r=r.apply(e,t||[])).next())})};import e from"@openremote/core";import{html as t,LitElement as o}from"lit";import{customElement as r,property as a,query as n}from"lit/decorators.js";import{LngLat as s,GeolocateControl as i}from"maplibre-gl";import{MapWidget as p}from"./mapwidget";import{style as l}from"./style";import"./markers/or-map-marker";import"./markers/or-map-marker-asset";import{OrMapMarker as d,OrMapMarkerChangedEvent as c}from"./markers/or-map-marker";import*as m from"./util";import{InputType as u}from"@openremote/or-mwc-components/or-mwc-input";import{OrMwcDialog as h,showDialog as v}from"@openremote/or-mwc-components/or-mwc-dialog";import{getMarkerIconAndColorFromAssetType as g}from"./util";import{i18next as f}from"@openremote/or-translate";import{debounce as y}from"lodash";import{CoordinatesControl as M,CoordinatesRegexPattern as A,getCoordinatesInputKeyHandler as w}from"./controls/coordinates";import{CenterControl as b}from"./controls/center";export*from"./markers/or-map-marker";export*from"./markers/or-map-marker-asset";export*from"./markers/or-cluster-marker";export*from"./or-map-asset-card";export*from"./or-map-legend";export class OrMapLoadedEvent extends CustomEvent{constructor(){super(OrMapLoadedEvent.NAME,{bubbles:!0,composed:!0})}}OrMapLoadedEvent.NAME="or-map-loaded";export class OrMapClickedEvent extends CustomEvent{constructor(e,t=!1){super(OrMapClickedEvent.NAME,{detail:{doubleClick:t,lngLat:e},bubbles:!0,composed:!0})}}OrMapClickedEvent.NAME="or-map-clicked";export class OrMapLongPressEvent extends CustomEvent{constructor(e){super(OrMapLongPressEvent.NAME,{detail:{doubleClick:!1,lngLat:e},bubbles:!0,composed:!0})}}OrMapLongPressEvent.NAME="or-map-long-press";export class OrMapGeocoderChangeEvent extends CustomEvent{constructor(e){super(OrMapGeocoderChangeEvent.NAME,{detail:{geocode:e},bubbles:!0,composed:!0})}}OrMapGeocoderChangeEvent.NAME="or-map-geocoder-change";export class OrMapMarkersChangedEvent extends CustomEvent{constructor(e){super(OrMapMarkersChangedEvent.NAME,{detail:e,bubbles:!0,composed:!0})}}OrMapMarkersChangedEvent.NAME="or-map-markers-changed";export const geoJsonPointInputTemplateProvider=(e,o,r,a,n,p)=>{let l,d=!!(p&&p.disabled),c=!!(p&&p.readonly),y=!!(p&&p.compact),O=!!(p&&p.comfortable),_=new b,k=e=>{n&&(void 0!==e?n({value:e?m.getGeoJSONPoint(e):null}):n(void 0))},C=new M(d,k);return{templateFunction:(o,r,a,n,p,M)=>{let b,E;o&&(b=(l=m.getLngLat(o))?Object.values(l):void 0);let x=b?b.join(", "):void 0;_.pos=l||void 0,C.readonly=d||c||n||a,C.value=x;let z=g(e),B=e=>{if(!c&&!d)if(l=e?m.getLngLat(e):null,E){let e=E.shadowRoot.getElementById("geo-json-point-marker");e.lng=l?l.lng:void 0,e.lat=l?l.lat:void 0,C.value=(b=l?Object.values(l):void 0)?b.join(", "):void 0}else k(l)},j=[[_,"bottom-left"],[C,"top-right"]];if(!c){let e=new i({positionOptions:{enableHighAccuracy:!0},showAccuracyCircle:!1,showUserLocation:!1});e.on("geolocate",e=>{B(new s(e.coords.longitude,e.coords.latitude))}),e.on("outofmaxbounds",e=>{B(new s(e.coords.longitude,e.coords.latitude))}),j.push([e,"bottom-left"])}let U=t`
            <or-map id="geo-json-point-map" class="or-map" @or-map-long-press="${e=>{B(e.detail.lngLat)}}" .center="${b}" .controls="${j}" .showGeoCodingControl=${!c}>
                <or-map-marker id="geo-json-point-marker" active .lng="${l?l.lng:void 0}" .lat="${l?l.lat:void 0}" .icon="${z?z.icon:void 0}" .activeColor="${z?"#"+z.color:void 0}" .colour="${z?"#"+z.color:void 0}"></or-map-marker>
            </or-map>
            <span class="long-press-msg">${f.t("longPressSetLoc")}</span>
        `;if(y){let e=U;U=t`
                <style>
                    #geo-json-point-input-compact-wrapper {
                        display: table-cell;
                    }
                    #geo-json-point-input-compact-wrapper > * {
                        vertical-align: middle;
                    }
                </style>
                <div id="geo-json-point-input-compact-wrapper">
                    <or-mwc-input style="width: auto;" .comfortable="${O}" .type="${u.TEXT}" .value="${x}" .pattern="${A}" @keyup="${e=>w(k)(e)}"></or-mwc-input>
                    <or-mwc-input style="width: auto;" .type="${u.BUTTON}" compact icon="crosshairs-gps" @or-mwc-input-changed="${()=>{E=v(new h().setContent(e).setStyles(t`
                            <style>
                                .dialog-container {
                                    flex-direction: column !important;
                                }
                                .dialog-container .long-press-msg {
                                    display: block;
                                    text-align: center;
                                }
                                or-map {
                                    width: 600px !important;
                                    min-height: 600px !important;
                                }
                            </style>
                        `).setActions([{actionName:"none",content:"none",action:()=>{B(null),k(l)}},{actionName:"ok",content:"ok",action:()=>{k(l)}},{default:!0,actionName:"cancel",content:"cancel"}]))}}"></or-mwc-input>
                </div>
            `}return U},supportsHelperText:!1,supportsLabel:!1,supportsSendButton:!1,validator:()=>!l||void 0!==l.lat&&null!==l.lat&&void 0!==l.lng&&null!==l.lng}};let OrMap=class extends o{constructor(){super(),this.type=e.mapType,this.showGeoCodingControl=!1,this.showBoundaryBoxControl=!1,this.useZoomControl=!0,this.showGeoJson=!0,this.boundary=[],this._loaded=!1,this.addEventListener(c.NAME,this._onMarkerChangedEvent)}addAssetMarker(e){var t,o,r,a,n;let s=null==(t=null==e?void 0:e.attributes)?void 0:t.location.value;(null==s?void 0:s.coordinates)&&(null==(o=this._map)||o.addAssetMarker(null!=(r=e.id)?r:"",null!=(a=e.name)?a:"",null!=(n=e.type)?n:"",s.coordinates[0],s.coordinates[1],e))}cleanUpAssetMarkers(){var e;null==(e=this._map)||e.cleanUpAssetMarkers()}reload(){return __awaiter(this,void 0,void 0,function*(){var e;yield null==(e=this._map)?void 0:e.load()})}firstUpdated(t){super.firstUpdated(t),e.ready&&this.loadMap()}connectedCallback(){super.connectedCallback()}disconnectedCallback(){var e;super.disconnectedCallback(),this._resizeObserver&&this._resizeObserver.disconnect(),null==(e=this._map)||e.unload()}render(){return t`
            <div id="container">
                <div id="map"></div>
                <slot></slot>
                <a id="openremote" href="https://openremote.io/" target="_blank">
                    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAABGdBTUEAAYagMeiWXwAAAAJiS0dEAP+Hj8y/AAAESUlEQVRIx5XVbWzdZRnH8c//f07POe36sK7SrYXZjYGzbBokOqd4QtKATdnMDFFSkoFzmWGRQOAFSoYnsBSzxBdmGsN4Y7LEFwZkUdHpDoja/AnJjBvp1sm2upW5PtAV+zBS+7Tz//tiLe0emPF+d9/39fvmunJf9+8K3HBF1drFzurVn5+5XkTwPwBNOt1s0pCjDnnT+Xzy/wOa5jaXnLLfLwzlF0WENwaYckyvUTHS1tnjl+6JFqk+JoMOqqy2JqjPVpXP1k7U1+Ty8paBPk97NX/pYwEdgZUetNkdlirDrHGnyv6y5r3lm4M2WVzwpJfz8XUBHTntnrJO6qqLWE9u/125zBNq0WebN/PXAjqq/cBOVWDasCEsd5MsmEzt/3xP+S6fwNsezPdfBejI6fCEDMa95oAT/t31pVxDQ6p6oy2WYSb18w0D2V3Klez2w3y8CNAR2G6vShxXUCxMkXLvS7Y6E/5+3emaJ92JqezzG+8M2nHW/flTi59xladU4phtfluYgnsDWUt8Nv7+8UfO73UUuenvDLxuCKu0RQt90BFo14wxzzpamD8uExtHSsu5HSP7XMCtZ5uTPyO0SdVCBlXahHjNG4WFrGY9Y4tXzODL7zb7NYJS64eHzWK92xYAa9yBKa8Wphf0xaQ4XOz0qF9JhMnXh//mIm4dnDSMOusWALepwQUnrm2t4pi9hrGyP+ccloxV6kOZFemoWi2mOpclaQycqGlt9R8XHS/GixinnVWvbDpjDEEpNpdnWrtd+HvZWzMQT9xjj1iXzUau6MPS9X9NKOeTmqzPpZWwfMkEKnza2ivimqxCKZjQa9BMmFI2D+gxibql4z7EiobYOSy1o7V6Xt1aYacGvD/7lse1+GZ9t0Zc8kHaGcOa1K6o+FePL1iy7K7wYHy70FZa9+qVWOm7tgslfpecKcy46GS0xXKM6g6d14VU+RfTnRJ8Y223w8j4tkMOOeR1j6nAMT8tzkCUcvlbn3ImbJn0RyWC+1af1Iv6ukcbf+aIRKDR3b5ipVCiy+PenaupWRsSfzCWim0ftUmdiqrJwWLpbmk3196UfXG0X6NKIWKDXva0I0UQZT2nRaDfc/mhgCj0PS9ImZzefbg5fliIvuTA++/0ZaYDTDqqpzhn6lHoW36iSuLHnslfCiBqdMBGDI6/0LUhfkgGiWFbC29c+epRaJMX3YJuD+R75l15wG4DaKh5dsPJsj0GXLaawavkWY/MyUcU/JOPHCkK7fAjNZiIf/PeX/vWx1814muF0Y/EKWs95mFVuKhgX352EYAoY5vnNSDRF/9p/MgHfQ2dldNIqbPeJm2aBBix20vzg26RpUUpLfb43FxZU4YMmEJGoxXKQeIfCg4uzMkrTDVitZ0ecst1B05i0Cv26Vk8H68JjFKabXa/Zkul5w5Lxp120EHdlyu/AQCiQI1P+YxaGcwY1+20kasnM/wXCa5/Ik1hKTEAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTktMDgtMjBUMTU6MTc6NTUrMDA6MDCwJSdSAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE5LTA4LTIwVDE1OjE3OjU1KzAwOjAwwXif7gAAAABJRU5ErkJggg==" />
                </a>
            </div>
        `}updated(e){var t;super.updated(e),(e.has("center")||e.has("zoom"))&&this.flyTo(this.center,this.zoom),e.has("boundary")&&this.showBoundaryBoxControl&&(null==(t=this._map)||t.createBoundaryBox(this.boundary))}refresh(){this._map&&this._map.loadViewSettings().then(()=>{this._map&&(this._map.setCenter(),this._map.flyTo())})}loadMap(){this._loaded||(this._mapContainer&&this._slotElement&&(this._map=new p(this.type,this.shadowRoot,this._mapContainer,this.showGeoCodingControl,this.showBoundaryBoxControl,this.useZoomControl,this.showGeoJson,this.cluster).setCenter(this.center).setZoom(this.zoom).setControls(this.controls).setGeoJson(this.geoJson),this._map.build().then(()=>{var e,t;null==(e=this._resizeObserver)||e.disconnect(),this._resizeObserver=new ResizeObserver(y(()=>{this.resize()},200));let o=null==(t=this._mapContainer)?void 0:t.parentElement;o&&this._resizeObserver.observe(o),this._slotElement.assignedNodes({flatten:!0}).forEach(e=>{e instanceof d&&this._map.addMarker(e)})})),this._loaded=!0)}resize(){this._map&&this._map.resize()}flyTo(e,t){this._map&&this._map.flyTo(e,t)}_onMarkerChangedEvent(e){this._map&&this._map.onMarkerChanged(e.detail.marker,e.detail.property)}};OrMap.styles=l,__decorate([a({type:String})],OrMap.prototype,"type",void 0),__decorate([a({type:Object})],OrMap.prototype,"cluster",void 0),__decorate([a({type:String,converter:{fromAttribute(e,t){if(!e)return;let o=e.split(",");if(2===o.length)return new s(Number(o[0]),Number(o[1]))},toAttribute(e,t){let o=m.getLngLat(e);return o?""+o.lng+","+o.lat:""}}})],OrMap.prototype,"center",void 0),__decorate([a({type:Number})],OrMap.prototype,"zoom",void 0),__decorate([a({type:Boolean})],OrMap.prototype,"showGeoCodingControl",void 0),__decorate([a({type:Boolean})],OrMap.prototype,"showBoundaryBoxControl",void 0),__decorate([a({type:Boolean})],OrMap.prototype,"useZoomControl",void 0),__decorate([a({type:Object})],OrMap.prototype,"geoJson",void 0),__decorate([a({type:Boolean})],OrMap.prototype,"showGeoJson",void 0),__decorate([a({type:Array})],OrMap.prototype,"boundary",void 0),__decorate([n("#map")],OrMap.prototype,"_mapContainer",void 0),__decorate([n("slot")],OrMap.prototype,"_slotElement",void 0),OrMap=__decorate([r("or-map")],OrMap);export{m as Util,s as LngLat,OrMap};