// 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/dijit/geoenrichment/Geoenrichment","../../declare dojo/_base/lang dojo/on dojo/string dojo/promise/all ./bufferTitle ../../geometry/Polygon ../../units ./DataProvider ../../tasks/geoenrichment/GeoenrichmentTask ../../tasks/geoenrichment/EnrichParameters ../../tasks/geoenrichment/RingBuffer ../../tasks/geoenrichment/DriveBuffer ../../tasks/geoenrichment/GeographyLevel ../../tasks/geoenrichment/GeometryStudyArea ../../tasks/geoenrichment/AddressStudyArea ../../tasks/geoenrichment/studyAreaFromJson ./config ./lang ./_Invoke dojo/when ../../tasks/locator ../../tasks/FeatureSet ../../graphic ../../geometry/jsonUtils".split(" "),
function(f,h,d,w,x,y,e,O,z,A,B,p,C,q,D,E,r,l,t,F,G,H,u,I,J){function K(a){a=a.toJson();delete a.returnGeometry;delete a.comparisonLevels;delete a.attributes;return a=JSON.stringify(a)}var k=f(null,{_keys:null,_values:null,_capacity:5,constructor:function(a){this._keys=[];this._values={};a&&(this._capacity=a)},getValue:function(a){return this._values[a]},setValue:function(a,c){this._keys.push(a);this._values[a]=c;this._removeOverflow()},hasValue:function(a){return this._values.hasOwnProperty(a)},_removeOverflow:function(){if(this._keys.length>
this._capacity)for(var a=this._keys.splice(0,this._keys.length-this._capacity),c=0;c<a.length;c++)delete this._values[a[c]]},setCapacity:function(a){this._capacity=a;this._removeOverflow()},toJson:function(){var a={},c;for(c in this._values){var b=this._values[c];void 0===b||null===b||"number"==typeof b&&isNaN(b)||("string"==typeof b||"boolean"==typeof b||"number"==typeof b?a[c]=b:"object"==typeof b&&b.toJson&&(a[c]=b.toJson()))}return{keys:this._keys.slice(),values:a,capacity:this._capacity}},fromJson:function(a,
c){if(a){this._keys=a.keys;this._capacity=a.capacity;this._values={};for(var b in a.values)this._values[b]=c?c(a.values[b]):a.values[b]}}});e=f(null,{_values:null,constructor:function(a){this._values=new k(a)},getValue:function(a){var c=this.keyToString(a);if(this._values.hasValue(c))return this._values.getValue(c);var b=this;return this.keyToValue(a).then(function(a){b._values.setValue(c,a);return a})},keyToString:function(a){return JSON.stringify(a)},keyToValue:function(a){throw"Not implemented";
},setCapacity:function(a){this._values.setCapacity(a)},toJson:function(){return this._values.toJson()},fromJson:function(a,c){a&&this._values.fromJson(a,c)}});var L=f([e],{keyToString:function(a){return JSON.stringify(a.toJson())},keyToValue:function(a){return(new H(l.locatorUrl)).locationToAddress(a).then(function(a){return w.substitute(l.addressFormat,a.address,function(a){return a||""})},function(a){return""})}}),M=f([e],{_countryValues:null,_geometries:null,constructor:function(){this._countryValues=
new k;this._geometries=new k(3)},keyToValue:function(a){var c=this,b=r(a.studyArea),f=b.returnGeometry,n,m;f&&(m=K(b),n=this._geometries.hasValue(m));var h=f&&!n,v=new A(l.server);v.token=l.token;for(var d=null,g=b.comparisonGeographyLevels.length-1;0<=g;g--)"Admin1"==b.comparisonGeographyLevels[g].layerID&&(d=b.comparisonGeographyLevels.splice(g,1)[0]);var e,k;d&&(e=JSON.stringify({variables:a.variables,country:a.country}),(k=this._countryValues.hasValue(e))||b.comparisonGeographyLevels.push(d));
g=new B;g.forStorage=!1;g.countryID=a.country;g.variables=a.variables;if(b.returnGeometry=h)g.outSR=b.geometry?b.geometry.spatialReference:a.outSR;g.studyAreas.push(b);return v.enrich(g).then(function(a){var b=a.featureSets[0].features;d&&(k?b.push(c._countryValues.getValue(e)):c._countryValues.setValue(e,b[b.length-1]));f&&(n?b[0].geometry=c._geometries.getValue(m):c._geometries.setValue(m,b[0].geometry));return a.featureSets[0]})},setCapacity:function(a){this.inherited(arguments);this._countryValues.setCapacity(a)},
toJson:function(){var a=this.inherited(arguments);a.countryValues=this._countryValues.toJson();a.geometries=this._geometries.toJson();return a},fromJson:function(a){a&&(this._countryValues.fromJson(a.countryValues,function(a){return new I(a)}),this._geometries.fromJson(a.geometries,function(a){return J.fromJson(a)}),this.inherited(arguments,[a,function(a){return new u(a)}]))}}),N=f([e],{metadata:null,_enrichCache:null,_addressCache:null,constructor:function(a){this._enrichCache=new M(a);this._addressCache=
new L(3)},keyToValue:function(a){var c=this,b=[],f=a.returnAddress;delete a.returnAddress;b.push(this._enrichCache.getValue(a));var d=r(a.studyArea);f&&b.push(this._addressCache.getValue(d.geometry));return x(b).then(function(a){var b=a[0],e=b.features[0];e.attributes[c.metadata.name]||(e.attributes[c.metadata.name]=y(d.getGeomType(),d.options),f?e.attributes[c.metadata.address]=a[1]:d instanceof E&&(e.attributes[c.metadata.address]=d.address.text));return b})},setCapacity:function(a){this.inherited(arguments);
this._enrichCache.setCapacity(a)},toJson:function(){var a=this.inherited(arguments);a.metadata=h.clone(this.metadata);a.enrichCache=this._enrichCache.toJson();a.addressCache=this._addressCache.toJson();return a},fromJson:function(a){a&&(this.metadata=a.metadata,this._enrichCache.fromJson(a.enrichCache),this._addressCache.fromJson(a.addressCache),this.inherited(arguments,[a,function(a){return new u(a)}]))}});return f("esri.dijit.geoenrichment.Geoenrichment",[z,F],{country:null,returnGeometry:!1,returnAddress:!1,
returnData:!0,studyArea:null,outSR:null,buffer:null,variables:null,levels:null,highestLevel:null,data:null,restartOnDone:!1,requests:null,started:!1,cache:null,constructor:function(){this.buffer=new p;this.cache=new N;this.cache.metadata=this.metadata},handleResponse:function(a){try{this.data=a,this.onDone(null)}catch(c){this.onDone(c)}},handleError:function(a){this.onDone(a)},onDone:function(a){this.requests=null;a?"CancelError"!==a.name&&(console.log(a),d.emit(this,"error",a)):d.emit(this,"data");
this.restartOnDone?(this.invalidate(),this.restartOnDone=!1):(d.emit(this,"end"),this.started=!1)},requestData:function(){if(this.studyArea&&this.variables&&this.buffer){this.requests=[];this.started||(d.emit(this,"start"),this.started=!0);var a,c=this.buffer;a=!1;if(this.studyArea instanceof D)switch(this.studyArea.geometry.type){case "point":a=this.returnAddress;break;case "polyline":this.buffer instanceof C&&(c=new p);break;case "polygon":c=null}var b=h.clone(this.studyArea);!b.options&&c&&(b.options=
c);if(this.levels)for(c=0;c<this.levels.length;c++)b.comparisonGeographyLevels.push(new q({layerID:this.levels[c]}));this.highestLevel&&b.comparisonGeographyLevels.push(new q({layerID:this.highestLevel}));b.returnGeometry=this.returnGeometry;a=G(this.cache.getValue({country:this.country,variables:this.variables,returnData:this.returnData,studyArea:b.toJson(),outSR:this.outSR,returnAddress:a}));this.requests.push(a);a.then(h.hitch(this,this.handleResponse),h.hitch(this,this.handleError))}},invalidate:function(){this.pendingInvoke("requestData")||
(this.requests?this.restartOnDone=!0:(this.geometry=null,this.invoke("requestData")))},setStudyArea:function(a){this.studyArea=a;this.invalidate()},setBuffer:function(a){this.buffer=a;this.invalidate()},getBuffer:function(){return this.buffer},invalidateData:function(){this.data=null;this.invalidate()},setVariables:function(a){t.arraysEqual(this.variables,a)||(this.variables=a,this.invalidateData())},setGeoLevels:function(a,c){t.arraysEqual(this.levels,a)&&this.highestLevel==c||(this.levels=a,this.highestLevel=
c,this.invalidateData())},setCacheLimit:function(a){this.cache.setCapacity(a)},getData:function(){return this.data},getGeometry:function(){return this.data.features[0].geometry},isBusy:function(){return this.pendingInvoke("requestData")||this.requests||this.restartOnDone},stop:function(){this.restartOnDone=!1;this.cancelInvoke("requestData");if(this.requests)for(var a=this.requests.slice(0),c=0;c<a.length;c++)a[c].cancel()},setReturnAddress:function(a){this.returnAddress!=a&&(this.returnAddress=a)&&
this.invalidateData()}})});