var __decorate=this&&this.__decorate||function(e,o,t,r){var a,n=arguments.length,s=n<3?o:null===r?r=Object.getOwnPropertyDescriptor(o,t):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,o,t,r);else for(var i=e.length-1;i>=0;i--)(a=e[i])&&(s=(n<3?a(s):n>3?a(o,t,s):a(o,t))||s);return n>3&&s&&Object.defineProperty(o,t,s),s},__awaiter=this&&this.__awaiter||function(e,o,t,r){return new(t||(t=Promise))(function(a,n){function s(e){try{l(r.next(e))}catch(e){n(e)}}function i(e){try{l(r.throw(e))}catch(e){n(e)}}function l(e){var o;e.done?a(e.value):((o=e.value)instanceof t?o:new t(function(e){e(o)})).then(s,i)}l((r=r.apply(e,o||[])).next())})};import e from"@openremote/core";import{html as o,LitElement as t}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 l}from"./mapwidget";import{style as p}from"./style";import{OrMapMarker as d,OrMapMarkerChangedEvent as c}from"./markers/or-map-marker";import"./markers/or-map-marker-asset";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 M from"lodash.debounce";import{CoordinatesControl as y,CoordinatesRegexPattern as A,getCoordinatesInputKeyHandler as b}from"./controls/coordinates";import{CenterControl as w}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,o=!1){super(OrMapClickedEvent.NAME,{detail:{doubleClick:o,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,t,r,a,n,l)=>{let p,d=!!(l&&l.disabled),c=!!(l&&l.readonly),M=!!(l&&l.compact),O=!!(l&&l.comfortable),C=new w,_=e=>{n&&(void 0!==e?n({value:e?m.getGeoJSONPoint(e):null}):n(void 0))},k=new y(d,_);return{templateFunction:(t,r,a,n,l,y)=>{let w,E;t&&(w=(p=m.getLngLat(t))?Object.values(p):void 0);let x=w?w.join(", "):void 0;C.pos=p||void 0,k.readonly=d||c||n||a,k.value=x;let z=g(e),B=e=>{if(!c&&!d)if(p=e?m.getLngLat(e):null,E){let e=E.shadowRoot.getElementById("geo-json-point-marker");e.lng=p?p.lng:void 0,e.lat=p?p.lat:void 0,k.value=(w=p?Object.values(p):void 0)?w.join(", "):void 0}else _(p)},j=[[C,"bottom-left"],[k,"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=o`
            <or-map id="geo-json-point-map" class="or-map" @or-map-long-press="${e=>{B(e.detail.lngLat)}}" .center="${w}" .controls="${j}" .showGeoCodingControl=${!c}>
                <or-map-marker id="geo-json-point-marker" active .lng="${p?p.lng:void 0}" .lat="${p?p.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(M){let e=U;U=o`
                <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=>b(_)(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(o`
                            <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),_(p)}},{actionName:"ok",content:"ok",action:()=>{_(p)}},{default:!0,actionName:"cancel",content:"cancel"}]))}}"></or-mwc-input>
                </div>
            `}return U},supportsHelperText:!1,supportsLabel:!1,supportsSendButton:!1,validator:()=>!p||void 0!==p.lat&&null!==p.lat&&void 0!==p.lng&&null!==p.lng}};let OrMap=class extends t{constructor(){super(),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 o,t,r,a,n;let s=null==(o=null==e?void 0:e.attributes)?void 0:o.location.value;(null==s?void 0:s.coordinates)&&(null==(t=this._map)||t.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(o){super.firstUpdated(o),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 o`
            <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 o;super.updated(e),(e.has("center")||e.has("zoom"))&&this.flyTo(this.center,this.zoom),e.has("boundary")&&this.showBoundaryBoxControl&&(null==(o=this._map)||o.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 l(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,o;null==(e=this._resizeObserver)||e.disconnect(),this._resizeObserver=new ResizeObserver(M(()=>{this.resize()},200));let t=null==(o=this._mapContainer)?void 0:o.parentElement;t&&this._resizeObserver.observe(t),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,o){this._map&&this._map.flyTo(e,o)}_onMarkerChangedEvent(e){this._map&&this._map.onMarkerChanged(e.detail.marker,e.detail.property)}};OrMap.styles=p,__decorate([a({type:Object})],OrMap.prototype,"cluster",void 0),__decorate([a({type:String,converter:{fromAttribute(e,o){if(!e)return;let t=e.split(",");if(2===t.length)return new s(Number(t[0]),Number(t[1]))},toAttribute(e,o){let t=m.getLngLat(e);return t?""+t.lng+","+t.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};