let _mapsElementDict=[],_placesElementDict=[],_mapsMarkers=[],_loadGoogleMaps=!1,_loadGoogleSearchPlaces=!1;export function init(e,t,n,i,l,o){if(!e||!t||!n)return;_loadGoogleMaps=!0,storeElementIdWithDotnetRef(_mapsElementDict,t,n,i,l);let a="https://maps.googleapis.com/maps/api/js?key=",d=!1;if(document.querySelectorAll("head > script").forEach(e=>{if(e){let t=e.getAttribute("src");if(t&&t.startsWith(a)){d=!0;return}}}),d){window.google&&window.initGoogleMaps();return}let r="";!0==o&&(r="places"),a=a+e+"&callback=initGoogleMaps&libraries="+r;let m=document.createElement("script");m.src=a,m.defer=!0,document.head.appendChild(m)}export function initAutocomplete(e,t,n,i,l){if(!e||!t||!n||!i||!l)return;_loadGoogleSearchPlaces=!0,storeSearchElementIdWithDotnetRef(_placesElementDict,t,n,i,l);let o="https://maps.googleapis.com/maps/api/js?key=",a=!1;if(document.querySelectorAll("head > script").forEach(e=>{if(e){let t=e.getAttribute("src");if(t&&t.startsWith(o)){a=!0;return}}}),a){window.google&&window.initGoogleMaps();return}o=o+e+"&callback=initGoogleMaps&libraries=places&v=weekly";let d=document.createElement("script");d.src=o,d.defer=!0,document.head.appendChild(d)}function initializeSearchBoxes(){for(let e=0;e<_placesElementDict.length;e++){let t=_placesElementDict[e].key,n=_placesElementDict[e].value,i=document.getElementById(t);if(i){let l=new google.maps.places.Autocomplete(i,{componentRestrictions:{country:n.countryRestriction}});l.setFields(["geometry","formatted_address","name"]),google.maps.event.addListener(l,"place_changed",function(){var n=l.getPlace();n&&0!=n.length&&_placesElementDict[e].value.ref.invokeMethodAsync("PlacesChanged",n,t)}),_placesElementDict[e].value.ref.invokeMethodAsync("PlacesInitialized",t)}}}function initializeGoogleMaps(){for(let e=0;e<_mapsElementDict.length;e++){let t=_mapsElementDict[e].key,n=_mapsElementDict[e].value;if(_mapsElementDict[e].value.map)continue;let i=new google.maps.Map(document.getElementById(t),{backgroundColor:n.bgColor,controlSize:n.ctrSize});function l(e,t){if(i&&i.elementId&&e){let n=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(n){let l=e.latLng.toJSON(),o={Latitude:l.lat,Longitude:l.lng};n.ref.invokeMethodAsync(t,o)}}}i.elementId=t,_mapsElementDict[e].value.map=i,i.addListener("click",e=>{l(e,"MapClicked")}),i.addListener("dblclick",e=>{l(e,"MapDoubleClicked")}),i.addListener("contextmenu",e=>{l(e,"MapContextMenu")}),i.addListener("mouseup",e=>{l(e,"MapMouseUp")}),i.addListener("mousedown",e=>{l(e,"MapMouseDown")}),i.addListener("mousemove",e=>{l(e,"MapMouseMove")}),i.addListener("mouseover",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapMouseOver")}}),i.addListener("mouseout",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapMouseOut")}}),i.addListener("center_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(e&&i.getCenter()){let t=i.getCenter().toJSON(),n={Latitude:t.lat,Longitude:t.lng};e.ref.invokeMethodAsync("MapCenterChanged",n)}}}),i.addListener("zoom_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapZoomChanged",i.getZoom())}}),i.addListener("maptypeid_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapTypeIdChanged",i.getMapTypeId())}}),i.addListener("heading_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapHeadingChanged",i.getHeading())}}),i.addListener("tilt_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapTiltChanged",i.getTilt())}}),i.addListener("bounds_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapBoundsChanged")}}),i.addListener("projection_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapProjectionChanged")}}),i.addListener("draggable_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapDraggableChanged")}}),i.addListener("streetview_changed",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapStreetviewChanged")}}),i.addListener("drag",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(e&&i.getCenter()){let t=i.getCenter().toJSON(),n={Latitude:t.lat,Longitude:t.lng};e.ref.invokeMethodAsync("MapDrag",n)}}}),i.addListener("dragend",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(e&&i.getCenter()){let t=i.getCenter().toJSON(),n={Latitude:t.lat,Longitude:t.lng};e.ref.invokeMethodAsync("MapDragEnd",n)}}}),i.addListener("dragstart",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(e&&i.getCenter()){let t=i.getCenter().toJSON(),n={Latitude:t.lat,Longitude:t.lng};e.ref.invokeMethodAsync("MapDragStart",n)}}}),i.addListener("resize",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);if(e){let t={Width:i.getDiv().offsetWidth,Height:i.getDiv().offsetHeight};e.ref.invokeMethodAsync("MapResized",t)}}}),i.addListener("tilesloaded",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapTilesLoaded")}}),i.addListener("idle",()=>{if(i&&i.elementId){let e=getElementIdWithDotnetRef(_mapsElementDict,i.elementId);e&&e.ref.invokeMethodAsync("MapIdle")}}),_mapsElementDict[e].value.ref.invokeMethodAsync("MapInitialized",t)}}function storeSearchElementIdWithDotnetRef(e,t,n,i,l){for(let o=0;o<e.length;o++)if(e[o].key===n)return;e.push({key:n,value:{ref:i,searchBoxId:t,elementId:n,countryRestriction:l}})}function storeElementIdWithDotnetRef(e,t,n,i,l){for(let o=0;o<e.length;o++)if(e[o].key===t)return;e.push({key:t,value:{ref:n,map:null,bgColor:i,ctrSize:l}})}function removeElementIdWithDotnetRef(e,t){for(let n=0;n<e.length;n++)if(e[n].key===t){e.splice(n,1);break}}function getElementIdWithDotnetRef(e,t){for(let n=0;n<e.length;n++)if(e[n].key===t)return e[n].value}window.initGoogleMaps=()=>{_loadGoogleMaps&&initializeGoogleMaps(),_loadGoogleSearchPlaces&&initializeSearchBoxes()};export function setCenterCoords(e,t,n){if(e){let i=getElementIdWithDotnetRef(_mapsElementDict,e);i&&i.map&&i.map.setCenter({lat:t,lng:n})}}export function setCenterAddress(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&geocodeAddress(t,function(e){e&&n.map.setCenter(e[0].geometry.location)})}}export function panToCoords(e,t,n){if(e){let i=getElementIdWithDotnetRef(_mapsElementDict,e);i&&i.map&&i.map.panTo({lat:t,lng:n})}}export function panToAddress(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&geocodeAddress(t,function(e){e&&n.map.panTo(e[0].geometry.location)})}}export function setZoom(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setZoom(t)}}export function setMapType(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setMapTypeId(t)}}export function setHeading(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setHeading(t)}}export function setTilt(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setTilt(t)}}export function setClickableIcons(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setClickableIcons(t)}}export function setOptions(e,t){if(e){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.map.setOptions(t)}}export function resizeMap(e){if(e){let t=getElementIdWithDotnetRef(_mapsElementDict,e);t&&t.map&&google.maps.event.trigger(t.map,"resize")}}export function createCustomControls(e,t){if(e&&t){let n=getElementIdWithDotnetRef(_mapsElementDict,e);if(n&&n.map)for(var i=0;i<t.length;i++){let l=t[i],o=document.createElement("div");o.innerHTML=l.content,n.map.controls[l.controlPosition].push(o);let a=l.id,d=n.ref;o.addEventListener("click",()=>{d.invokeMethodAsync("CustomControlClicked",a)})}}}export function createMarkers(e,t){if(e&&t&&t.length){let n=getElementIdWithDotnetRef(_mapsElementDict,e);if(n&&n.map)for(var i=0;i<t.length;i++){let l=t[i],o=new google.maps.Marker({id:l.id,crossOnDrag:l.crossOnDrag,optimized:l.optimized});if(o.setMap(n.map),setMarkerData(l,o),_mapsMarkers.push(o),l.clickable){let a=null;l.infoWindow&&(a=new google.maps.InfoWindow({content:l.infoWindow.content,maxWidth:l.infoWindow.maxWidth})),o.addListener("click",()=>{n.ref.invokeMethodAsync("MarkerClicked",l.id),a&&a.open(n.map,o)})}if(l.draggable){function d(e,t,i){let l={Latitude:i.lat,Longitude:i.lng};n.ref.invokeMethodAsync(e,t,l)}o.addListener("drag",()=>{d("MarkerDrag",l.id,o.getPosition().toJSON())}),o.addListener("dragend",()=>{d("MarkerDragEnd",l.id,o.getPosition().toJSON())}),o.addListener("dragstart",()=>{d("MarkerDragStart",l.id,o.getPosition().toJSON())})}}}}export function removeMarkers(e,t){if(e&&t&&t.length){let n=getElementIdWithDotnetRef(_mapsElementDict,e);if(n&&n.map)for(var i=0;i<t.length;i++){let l=t[i];_mapsMarkers.forEach((e,t)=>{if(l.id==e.id){e.setMap(null),_mapsMarkers.splice(t,1);return}})}}}function setMarkerData(e,t){t&&e&&(t.setPosition({lat:e.position.latitude,lng:e.position.longitude}),t.anchorPoint=e.anchorPoint?{x:e.anchorPoint.x,y:e.anchorPoint.y}:null,t.setAnimation(e.animation),t.setClickable(e.clickable),t.crossOnDrag=e.crossOnDrag,t.setCursor(e.cursor),t.setDraggable(e.draggable),t.setIcon(e.icon),t.setLabel(e.label),t.setOpacity(e.opacity),t.optimized=e.optimized,t.setShape(e.shape),t.setTitle(e.title),t.setVisible(e.visible),t.setZIndex(e.zIndex))}export function getAddressCoordinates(e,t){geocodeAddress(t,function(t){if(t){let n=getElementIdWithDotnetRef(_mapsElementDict,e);n&&n.map&&n.ref.invokeMethodAsync("AddressSearch",t)}})}function geocodeAddress(e,t){new google.maps.Geocoder().geocode({address:e},function(e,n){n==google.maps.GeocoderStatus.OK&&t(e)})}export function dispose(e){if(e){let t=getElementIdWithDotnetRef(_mapsElementDict,e);t.map=null,t.ref=null,removeElementIdWithDotnetRef(_mapsElementDict,e)}}export function disposeAutocomplete(e){if(e){let t=_placesElementDict.findIndex(t=>t.key===e);if(-1!==t){let n=_placesElementDict[t],i=document.getElementById(e);i&&google.maps.event.clearInstanceListeners(i),n.ref=null,_placesElementDict.splice(t,1)}}}