/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/tasks/geoenrichment/GeoenrichmentTask",["../../declare","dojo/_base/array","dojo/dom-construct","./taskHelper","esri/tasks/FeatureSet","./EnrichParameters","./ReportParameters","esri/IdentityManager","esri/geometry/Point"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9){function _a(_b,_c){return _b.getAvailableCountries().then(function(_d){for(var i=0;i<_d.length;i++){if(_d[i].id==_c){return _d[i].name;}}});};return _1("esri.tasks.geoenrichment.GeoenrichmentTask",null,{token:null,url:null,constructor:function(_e){this.url=_e||location.protocol+"//geoenrich.arcgis.com/arcgis/rest/services/World/GeoenrichmentServer";},enrich:function(_f){return _4.invokeMethod(this,"/Geoenrichment/enrich",function createParams(){if(!(_f instanceof _6)){_f=new _6(_f);}return _4.jsonToRest(_f.toJson());},function readResponse(_10){if(!_10.results||_10.results.length<1||!_10.results[0].value||!_10.results[0].value.FeatureSet||_10.results[0].value.FeatureSet.length<1){_4.throwEmptyResponse();}var _11={featureSets:[],messages:_10.messages};var _12=_10.results[0].value.FeatureSet;for(var i=0;i<_12.length;i++){_11.featureSets.push(new _5(_12[i]));}return _11;},"onEnrichComplete","onError");},getAvailableCountries:function(){return _4.invokeMethod(this,"/Geoenrichment/Countries",null,function readResponse(_13){if(_13.error){throw _13.error;}var _14=_13.countries;for(var i=0;i<_14.length;i++){var _15=_14[i].datasets;delete _14[i].datasets;_14[i].datasetIDs=_15;}return _14;},"onGetAvailableCountriesComplete","onError");},getDataCollections:function(_16,_17){var url;if(_16||_17){url="/GetDataCollections/execute";}else{url="/Geoenrichment/DataCollections";}return _4.invokeMethod(this,url,function createParams(){var _18={};if(_16){_18["sourcecountry"]=_16;}if(_17){_18["searchtext"]="id:"+_17;}return _18;},function readResponse(_19){if(_19.error){throw _19.error;}var _1a=_19.results||_19.dataCollections||_19.DataCollections;for(var i=0;i<_1a.length;i++){_1a[i]={id:_1a[i].dataCollectionID,metadata:_1a[i].metadata,variables:_1a[i].data};}return _1a;},"onGetDataCollectionsComplete","onError");},createReport:function(_1b){var _1c=this;_8.getCredential(this.url).then(function(_1d){try{var _1e=_3.create("form",{target:"_blank",action:_1c.url+"/Geoenrichment/CreateReport",method:"post"});if(!(_1b instanceof _7)){_1b=new _7(_1b);}var _1f=_4.jsonToRest(_1b.toJson());_1f.f="bin";_1f.token=_1d.token;for(var arg in _1f){_3.create("input",{type:"hidden",name:arg,value:_1f[arg]},_1e);}_3.place(_1e,document.body);_1e.submit();_3.destroy(_1e);}catch(e){_1c.onError(e);}},function(_20){_1c.onError(_20);});},getReports:function(_21){var _22=this;return _a(this,_21).then(function(_23){return _4.invokeMethod(_22,"/Geoenrichment/Reports/"+_23,null,function readResponse(_24){for(var i=0;i<_24.reports.length;i++){var id=_24.reports[i].reportID;delete _24.reports[i].reportID;_24.reports[i].id=id;}return _24.reports;},"onGetReportsComplete","onError");});},getStandardGeographyLevels:function(_25){var _26=this;function _27(url){return _4.invokeMethod(_26,url,null,function readResponse(_28){var _29=_28.geographyLevels;for(var i=0;i<_29.length;i++){var _2a=_29[i];_2a.id=_2a.countryID;delete _2a.countryID;_2a.name=_2a.countryName;delete _2a.countryName;var _2b=_2a.datasets;for(var j=0;j<_2b.length;j++){var _2c=_2b[j];_2c.id=_2c.datasetID;delete _2c.datasetID;_2c.geographyLayers=_2c.levels;delete _2c.levels;}}return _29;},"onGetStandardGeographyLevelsComplete","onError");};if(_25){return _a(this,_25).then(function(_2d){return _27("/Geoenrichment/StandardGeographyLevels/"+_2d);});}else{return _27("/Geoenrichment/StandardGeographyLevels");}},getServiceLimits:function(){return _4.invokeMethod(this,"/Geoenrichment/ServiceLimits",null,function readResponse(_2e){return _2e.serviceLimits.value;},"onGetServiceLimitsComplete","onError");},getCountries:function(_2f){var _30;if(_2f.type=="point"){_30=_2f;}else{var _31=_2f.getExtent();_30=new _9((_31.xmin+_31.xmax)/2,(_31.ymin+_31.ymax)/2,_31.spatialReference);}return this.enrich({variables:["GlobalIntersect.*"],studyAreas:[{geometry:_30}],forStorage:false}).then(function(_32){var _33=[];var _34=_32.featureSets[0].features;for(var i=0;i<_34.length;i++){var _35=_34[i].attributes["sourceCountry"];if(_2.indexOf(_33,_35)<0){_33.push(_35);}}return _33;});},onEnrichComplete:function(_36){},onGetAvailableCountriesComplete:function(_37){},onGetDataCollectionsComplete:function(_38){},onCreateReportComplete:function(_39){},onGetReportsComplete:function(_3a){},onGetStandardGeographyLevelsComplete:function(_3b){},onGetServiceLimitsComplete:function(_3c){},onError:function(_3d){}});});