(()=>{const{registerPlugin:a}=wp.plugins,{PluginSidebar:e}=wp.editor,{__:t}=wp.i18n,{Component:o}=wp.element,s={"margin-bottom":"200px"};a("mapsvg-sidebar",{icon:"location",render:class extends o{render(){return React.createElement(e,{title:t("Location","textdomain")},React.createElement("div",null,React.createElement("div",{id:"mapsvg"})),React.createElement("div",{id:"mapsvg-filters",style:s}))}loadMap(){if(null!==document.getElementById("mapsvg")){window.mapsvgAdmin={getData:()=>({options:window.mapsvgBackendParams})};var a={markerImages:window.mapsvgBackendParams.mapsvgMarkerImages,googleApiKey:window.mapsvgBackendParams.googleApiKey,prefetch:!1},e={source:window.mapsvgBackendParams.googleApiKey?window.mapsvgBackendParams.mapsPath+"/geo-calibrated/empty.svg":window.mapsvgBackendParams.mapsPath+"/geo-calibrated/world.svg",filters:{on:!1},fitMarkers:!0,defaultMarkerImage:mapsvg.routes.root+"markers/_pin_default.png",containers:{header:{on:!1}},googleMaps:{on:!!window.mapsvgBackendParams.googleApiKey,apiKey:window.mapsvgBackendParams.googleApiKey},database:{on:!1,regionsTableName:"regions_0",objectsTableName:"objects_0"},events:{afterInit:a=>{const{map:e}=a;this.mapsvg=e,this.mapsvg.setMarkersEditMode(!0);var t=wp.data.select("core/editor").getEditedPostAttribute("meta");t||(t=[]);var o={};if(t.mapsvg_location&&"null"!==t.mapsvg_location){var s=JSON.parse(t.mapsvg_location),i=new mapsvg.location(s),n=new mapsvg.marker({location:i,mapsvg:e});e.markerAdd(n),o.location=i.getData(),this.mapsvg.options.googleMaps.on&&this.mapsvg.events.on("googleMapsLoaded",(()=>{var a={lat:i.geoPoint.lat,lng:i.geoPoint.lng};this.mapsvg.googleMaps.map.setCenter(a),this.mapsvg.googleMaps.map.setZoom(10)}))}var r=new mapsvg.formBuilder({container:jQuery("#mapsvg-filters")[0],schema:new mapsvg.schema({fields:[{type:"location",name:"Location",label:"",parameterNameShort:"location"}]}),showNames:!1,editMode:!1,filtersMode:!0,mapsvg:e,mediaUploader:null,data:o,admin:null,closeOnSave:!1,events:{init:a=>{const{formElement:e,name:t,value:o}=a.data;this.mapsvg.hideMarkers();const s=r.getFormElementByType("location");s&&s.value&&(this.locationCopy=this.createLocationCopy(s.value),this.locationCopy&&(this.watchMarkerChanges(s,this.locationCopy),this.mapsvg.setEditingMarker(this.locationCopy.marker))),this.mapsvg.setMarkerEditHandler((a=>{this.locationCopy&&this.mapsvg.markerDelete(this.locationCopy.marker),this.locationCopy=a,this.mapsvg.setEditingMarker(this.locationCopy.marker),r.getData(),s.setValue(a.getData()),s.triggerChanged(),this.watchMarkerChanges(s,this.locationCopy)}))},"delete.formElement":a=>{const{formElement:e,name:t,value:o}=a.data;"location"===e.type&&this.locationCopy&&(this.mapsvg.markerDelete(this.locationCopy.marker),delete this.locationCopy)},"change.formElement":a=>{const{formElement:e,name:t,value:o}=a.data;if("location"===e.type){const a=JSON.stringify(o);if(wp.data.dispatch("core/editor").editPost({meta:{mapsvg_location:a}}),this.locationCopy){if(o&&(this.locationCopy.geoPoint.lat!=o.geoPoint.lat||this.locationCopy.geoPoint.lng!=o.geoPoint.lng)&&this.mapsvg.options.googleMaps.on){var s={lat:o.geoPoint.lat,lng:o.geoPoint.lng};this.mapsvg.googleMaps.map.setCenter(s),this.mapsvg.googleMaps.map.setZoom(17)}this.mapsvg.markerDelete(this.locationCopy.marker)}if(o){this.locationCopy=new mapsvg.location(o);const a=new mapsvg.marker({location:this.locationCopy,mapsvg:this.mapsvg});this.mapsvg.markerAdd(this.locationCopy.marker),this.mapsvg.setEditingMarker(a),this.watchMarkerChanges(e,this.locationCopy)}}}}});r.init()}}};new mapsvg.map("mapsvg",{options:e},a)}}createLocationCopy(a){if(a){let e=new mapsvg.location(a),t=new mapsvg.marker({location:e,mapsvg:this.mapsvg});return this.mapsvg.markerAdd(t),e}}watchMarkerChanges(a,e){e&&e.marker&&e.marker.events.on("change",(()=>{if(e.marker.isMoving())return!1;a.setValue(e.getData()),a.triggerChanged()}))}componentDidMount(){var a=this;setTimeout((function(){a.loadMap(),jQuery('button[aria-label="Location"]').on("click",(function(){setTimeout((function(){a.loadMap()}),500)}))}),1e3)}componentWillUnmount(){this.mapsvg&&(this.mapsvg.destroy(),delete this.locationCopy,this.mapsvg=null)}}})})();