import{r as e,f as t}from"./p-23450e8b.js";class l{async forward(e,t={}){let l={format:"jsonv2",addressdetails:t.addressdetails||"1"};if(t.countrycodes!=undefined)l["countrycodes"]=t.countrycodes;if(typeof e.address==="string"){l["q"]=e.address}else{Object.assign(l,e.address)}if(e.bbox!=undefined)l["viewbox"]=e.bbox.join(",");if(e.bounded)l["bounded"]="1";if(e.limit!=undefined)l["limit"]=e.limit;let n=await fetch(this.addParams(e.url,l),{mode:t.mode||"cors"});let s=await n.json();if(s.error)return s;return this.formatResponse(s)}async reverse(e,t={}){let l={format:"jsonv2",addressdetails:t.addressdetails||"1",zoom:t.zoom||"18"};l["lon"]=e.location.lon.toString();l["lat"]=e.location.lat.toString();let n=await fetch(this.addParams(e.url,l),{mode:t.mode||"cors"});let s=await n.json();if(s.error)return s;return this.formatResponseItem(s)}formatResponse(e){return e.map((e=>this.formatResponseItem(e)))}formatResponseItem(e){let t=null;if(e.boundingbox){let l=e.boundingbox.map((e=>parseFloat(e)));t=[l[2],l[0],l[3],l[1]]}let l={address:{name:e.address[e.type]||e.address[e.category]||null,housenumber:e.address.house_number||null,street:e.address.road||null,city:e.address.city||null,county:e.address.county||null,state:e.address.state||null,country:e.address.country||null,countrycode:e.address.country_code||null,postalcode:e.address.postcode||null},bbox:t,client:{osm_id:e.osm_id||null,osm_type:e.osm_type||null,category:e.category||null,type:e.type||null,importance:e.importance||null,place_id:e.place_id||null,place_rank:e.place_rank||null},display:e.display_name||null,location:{lon:parseFloat(e.lon),lat:parseFloat(e.lat)},polygon:e.geojson||null};return l}addParams(e,t){let l=new URL(e);Object.keys(t).forEach((e=>l.searchParams.append(e,t[e])));return l.toString()}}const n=class{constructor(l){e(this,l);this.glForwardGeocode=t(this,"glForwardGeocode",7);this.glReverseGeocode=t(this,"glReverseGeocode",7);this.defaultClient="nominatim"}async forward(e,t){let l=this.getClient(e.client);let n=await l.forward(e,t);this.glForwardGeocode.emit({options:e,response:n});return n}async reverse(e,t){let l=this.getClient(e.client);let n=await l.reverse(e,t);this.glReverseGeocode.emit({options:e,response:n});return n}getClient(e){return n.clients[e||this.defaultClient]}static async addClient(e,t){n.clients[e]=t}};n.clients={};n.addClient("nominatim",new l);export{n as gl_geocode_controller};
//# sourceMappingURL=p-0f687692.entry.js.map