// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/tasks/locator","dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/_base/Deferred dojo/_base/json dojo/has ../kernel ../request ../deferredUtils ./Task ./AddressCandidate".split(" "),function(p,l,y,t,q,z,A,u,v,B,r){p=p(B,{declaredClass:"esri.tasks.Locator",_eventMap:{"address-to-locations-complete":["addresses"],"addresses-to-locations-complete":["addresses"],"location-to-address-complete":["address"],"suggest-locations-complete":["suggestions"]},constructor:function(a){this._geocodeHandler=
l.hitch(this,this._geocodeHandler);this._geocodeAddressesHandler=l.hitch(this,this._geocodeAddressesHandler);this._reverseGeocodeHandler=l.hitch(this,this._reverseGeocodeHandler);this.registerConnectEvents()},outSpatialReference:null,setOutSpatialReference:function(a){this.outSpatialReference=a},_geocodeHandler:function(a,b,h,g,c){try{var f=a.candidates,d;b=[];var e,m=f.length,k=a.spatialReference,n;for(e=0;e<m;e++){d=f[e];if(n=d.location)n.spatialReference=k;b[e]=new r(d)}this._successHandler([b],
"onAddressToLocationsComplete",h,c)}catch(w){this._errorHandler(w,g,c)}},_geocodeAddressesHandler:function(a,b,h,g,c){try{var f=a.locations;b=[];var d,e=f.length,m=a.spatialReference,k;for(d=0;d<e;d++){if(k=f[d].location)k.spatialReference=m;b[d]=new r(f[d])}this._successHandler([b],"onAddressesToLocationsComplete",h,c)}catch(n){this._errorHandler(n,g,c)}},addressToLocations:function(a,b,h,g,c){var f,d,e,m,k,n,w;a.address&&(g=h,h=b,b=a.outFields,c=a.searchExtent,w=a.countryCode,f=a.magicKey,d=a.distance,
n=a.categories,a.location&&this.normalization&&(e=a.location.normalize()),m=a.maxLocations,k=a.forStorage,a=a.address);c&&(c=c.shiftCentralMeridian());var p=this.outSpatialReference;a=this._encode(l.mixin({},this._url.query,a,{f:"json",outSR:p&&q.toJson(p.toJson()),outFields:b&&b.join(",")||null,searchExtent:c&&q.toJson(c.toJson()),category:n&&n.join(",")||null,countryCode:w||null,magicKey:f||null,distance:d||null,location:e||null,maxLocations:m||null,forStorage:k||null}));var r=this._geocodeHandler,
y=this._errorHandler,x=new t(v._dfdCanceller);x._pendingDfd=u({url:this._url.path+"/findAddressCandidates",content:a,callbackParamName:"callback",load:function(a,b){r(a,b,h,g,x)},error:function(a){y(a,g,x)}});return x},suggestLocations:function(a){var b;b=new t(v._dfdCanceller);a.hasOwnProperty("location")&&this.normalization&&(a.location=a.location.normalize());a.searchExtent&&(a.searchExtent=a.searchExtent.shiftCentralMeridian());a=this._encode(l.mixin({},this._url.query,{f:"json",text:a.text,maxSuggestions:a.maxSuggestions,
searchExtent:a.searchExtent&&q.toJson(a.searchExtent.toJson()),category:a.categories&&a.categories.join(",")||null,countryCode:a.countryCode||null,location:a.location||null,distance:a.distance||null},{f:"json"}));a=u({url:this._url.path+"/suggest",content:a,callbackParamName:"callback"});b._pendingDfd=a;a.then(l.hitch(this,function(a){a=a.suggestions||[];this.onSuggestLocationsComplete(a);b.resolve(a)}),l.hitch(this,function(a){this._errorHandler(a);b.reject(a)}));return b},addressesToLocations:function(a,
b,h){var g=this.outSpatialReference,c=[],f=a.categories,d=a.countryCode;y.forEach(a.addresses,function(a,b){c.push({attributes:a})});a=this._encode(l.mixin({},this._url.query,{category:f&&f.join(",")||null,sourceCountry:d||null},{addresses:q.toJson({records:c})},{f:"json",outSR:g&&q.toJson(g.toJson())}));var e=this._geocodeAddressesHandler,m=this._errorHandler,k=new t(v._dfdCanceller);k._pendingDfd=u({url:this._url.path+"/geocodeAddresses",content:a,callbackParamName:"callback",load:function(a,c){e(a,
c,b,h,k)},error:function(a){m(a,h,k)}});return k},_reverseGeocodeHandler:function(a,b,h,g,c){try{var f=new r({address:a.address,location:a.location,score:100});this._successHandler([f],"onLocationToAddressComplete",h,c)}catch(d){this._errorHandler(d,g,c)}},locationToAddress:function(a,b,h,g){a&&this.normalization&&(a=a.normalize());var c=this.outSpatialReference;a=this._encode(l.mixin({},this._url.query,{outSR:c&&q.toJson(c.toJson()),location:a&&q.toJson(a.toJson()),distance:b,f:"json"}));var f=this._reverseGeocodeHandler,
d=this._errorHandler,e=new t(v._dfdCanceller);e._pendingDfd=u({url:this._url.path+"/reverseGeocode",content:a,callbackParamName:"callback",load:function(a,b){f(a,b,h,g,e)},error:function(a){d(a,g,e)}});return e},onSuggestLocationsComplete:function(){},onAddressToLocationsComplete:function(){},onAddressesToLocationsComplete:function(){},onLocationToAddressComplete:function(){}});z("extend-esri")&&l.setObject("tasks.Locator",p,A);return p});