/*
 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/dijit/geoenrichment/InfographicsOptions",["../../declare","dojo/_base/lang","dojo/Deferred","dojo/string","../../tasks/geoenrichment/studyAreaOptionsFromJson","../../tasks/geoenrichment/GeoenrichmentTask","./lang","./config"],function(_1,_2,_3,_4,_5,_6,_7,_8){var _9=function(_a,_b){this.type=_a;this.variables=_b;this.title=null;this.datasetID=null;this.index=null;this.isVisible=true;};var _c=_1("esri.dijit.geoenrichment.InfographicsOptions",null,{_items:null,_loaded:null,studyAreaOptions:null,theme:null,constructor:function(_d){this._loaded={};this.studyAreaOptions=_5(_d&&(_d.buffer||_d.studyAreaOptions));this._items={};if(_d){_e(_d.reports||_d.items,this._items);this.theme=_d.theme;}else{this.theme=null;}},toJson:function(){var _f={};_e(this._items,_f);return {studyAreaOptions:this.studyAreaOptions.toJson(),items:_f,theme:this.theme};},getItems:function(_10){var _11=new _3();if(this._loaded[_10]){_11.resolve(this._items[_10]);}else{var _12=new _6(_8.server);_12.token=_8.token;var _13=this;_12.getDataCollections(_10).then(_2.hitch(this,this._mergeItems,_10,_11),function(_14){_11.reject(_14);});}return _11.promise;},_mergeItems:function(_15,_16,_17){try{var _18=[];for(var i=0;i<_17.length;i++){var _19=_17[i].metadata.infographics;if(!_19){continue;}var _1a;var _1b=_17[i].metadata.datasets;if(_1b){_1a=_4.trim(_1b.split(",")[0]);}var _1c=JSON.parse(_19);for(var id in _1c){var _1d=new _9(id,[_17[i].id+".*"]);_1d.datasetID=_1a;for(var _1e in _1c[id]){_1d[_1e]=_1c[id][_1e];}var _1f=_20(_18,_1d);if(_1f){_18[_1f.index]=_1d;}else{_18.push(_1d);}}}var _21;var _22;function _23(){_21={};_22={};for(var i=0;i<_17.length;i++){_22[_17[i].id]=_17[i];for(var j=0;j<_17[i].variables.length;j++){_21[_17[i].id+"."+_17[i].variables[j].id]=_17[i].variables[j];}}};function _24(_25){if(!_21){_23();}return _21[_25];};function _26(id){if(!_22){_23();}return _22[id];};var _27=this._items[_15];if(!_27){_27=[];_27.push(new _9("OneVar",["KeyGlobalFacts.AVGHHSZ"]));this._items[_15]=_27;}for(var i=_27.length-1;i>=0;i--){var _1f=_20(_18,_27[i]);if(!_1f){if(_27[i].type=="OneVar"&&_27[i].variables.length==1){var _28=_27[i].variables[0];var _29=_24(_28);if(_29){_27[i].title=_29.alias;_27[i].datasetID=_4.trim(_26(_28.split(".")[0]).metadata.datasets.split(",")[0]);continue;}}_27.splice(i,1);i--;}else{_2a(_27[i],_1f.report);_27[i]=_1f.report;_18.splice(_1f.index,1);}}for(var i=0;i<_18.length;i++){_27.push(_18[i]);}_27.sort(_2b);this._loaded[_15]=true;_16.resolve(_27);}catch(error){_16.reject(error);}}});function _2b(_2c,_2d){var _2e=parseFloat(_2c.index);var _2f=parseFloat(_2d.index);if(isNaN(_2e)&&isNaN(_2f)){return 0;}if(isNaN(_2e)){return 1;}if(isNaN(_2f)){return -1;}return _2e-_2f;};function _20(_30,_31){for(var i=0;i<_30.length;i++){var _32=_30[i];if(_32.type==_31.type&&_7.arraysEqual(_32.variables,_31.variables)){return {report:_32,index:i};}}return null;};function _e(_33,_34){if(!_33){return;}for(var _35 in _33){_34[_35]=[];for(var i=0;i<_33[_35].length;i++){var _36=_33[_35][i];var _37={};_2a(_36,_37);_34[_35].push(_37);}}};function _2a(_38,_39){_39.type=_38.type||(_38.report=="OneVarMultiComparison"?"OneVar":_38.report);if(_38.dataCollection){if(_38.vars){_39.variables=[];for(var i=0;i<_38.vars.length;i++){_39.variables.push(_38.dataCollection+"."+_38.vars[i]);}}else{_39.variables=[_38.dataCollection+".*"];}}else{_39.variables=_38.variables;}if(_7.isBoolean(_38.isVisible)){_39.isVisible=_38.isVisible;}else{if(_7.isBoolean(_38.checked)){_39.isVisible=_38.checked;}}};_c.Item=_9;return _c;});