// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://@sbaseurl@/jsapi/jsapi/esri/copyright.txt and http://www.arcgis.com/apps/webappbuilder/copyright.txt for details.

//>>built
define("dojo/_base/declare dojo/Deferred dojo/_base/lang dojo/promise/all esri/tasks/query esri/tasks/QueryTask jimu/LayerInfos/LayerInfos jimu/DataSourceManager jimu/utils ./utils".split(" "),function(n,f,k,m,p,q,r,t,u,h){return n([],{constructor:function(a){this.appConfig=a.appConfig;this.map=a.map;this.layerInfosObj=r.getInstanceSync();this.dataSourceManager=t.getInstance();this.cacheData={}},destroy:function(){this.cacheData=this.dataSourceManager=this.layerInfosObj=this.map=this.appConfig=null},
preprocessingDataSource:function(a){var b=new f;if(!a)return b.resolve(),b;var c=this._tryGetLayerIdDataSchema(a),d=c.dataSchema;c=this._tryGetLayerObject(c.layerId);a=this._tryGetLayerForFrameWork(d,a);m([c,a]).then(function(e){var g=e[0];b.resolve({layerObject:g&&g.layerObject,popupInfo:g&&g.popupInfo,featureLayerForFrameWork:e[1]})},function(e){b.reject(e)});return b},getFeaturesForSetting:function(a){var b=new f;if(!a)return b.resolve(),b;this.getFeaturesByDataSource(a).then(function(c){c=this._cutFeaturesForSetting(c);
b.resolve(c)}.bind(this),function(c){b.reject(c)});return b},preprocessingDataSourceForSetting:function(a){var b=new f;if(!a)return b.resolve(),b;var c=this._tryGetLayerIdDataSchema(a),d=c.layerId,e=c.dataSchema;c=this._tryGetLayerObject(d);e=this._tryGetLayerForFrameWork(e,a);a=this.getLayerDefinitionByDataSource(a,d);m([c,e,a]).then(function(g){var l=g[0];b.resolve({layerObject:l&&l.layerObject,popupInfo:l&&l.popupInfo,featureLayerForFrameWork:g[1],definition:g[2]})}.bind(this),function(g){b.reject(g)});
return b},preprocessingDataSourceForRange:function(a){var b=new f;if(!a)return b.resolve(),b;var c=this._tryGetLayerIdDataSchema(a).layerId;c=this.getLayerDefinitionByDataSource(a,c);a=this.getFeaturesByDataSource(a);m([c,a]).then(function(d){var e=d[0];d=d[1];d=this._cutFeaturesForSetting(d);b.resolve({definition:e,features:d})}.bind(this),function(d){b.reject(d)});return b},_tryGetLayerIdDataSchema:function(a){var b={layerId:"",dataSchema:null};if(a.layerId)this.dsType="CLIENT_FEATURES",b.layerId=
a.layerId;else if(a.frameWorkDsId){a=(a=h.getDsTypeInfoMeta(a.frameWorkDsId,this.appConfig))&&a.dsMeta;if(!a)return b;"Features"===a.type?this.dsType="FRAMEWORK_FEATURES":"FeatureStatistics"===a.type&&(this.dsType="FRAMEWORK_STATISTICS");b.dataSchema=a.dataSchema;a=h.parseDataSourceId(a.id);b.layerId=a&&a.layerId}return b},_tryGetLayerObject:function(a){var b=new f;if(!a)return b.resolve(),b;a=this._tryGetLayerInfo(a);if(!a)return console.error("Invalid data source"),b.resolve(),b;var c=a.getPopupInfo();
a.getLayerObject().then(function(d){b.resolve({layerObject:d,popupInfo:c})}.bind(this),function(d){b.reject(d)});return b},_tryGetLayerForFrameWork:function(a,b){var c=new f;if(!a)return c.resolve(),c;var d=a;"FRAMEWORK_STATISTICS"===this._getDataSourceType(b)&&(d=h.mockLayerDefinitionForSTD(a));h.getLoadedLayer(d).then(function(e){c.resolve(e)}.bind(this),function(e){c.reject(e)});return c},_tryGetLayerInfo:function(a){var b=this.layerInfosObj.getLayerInfoById(a);b||(b=this.layerInfosObj.getTableInfoById(a));
return b},getLayerDefinitionByDataSource:function(a,b){var c=new f;if(!a)return console.error("Invalid data source"),c.resolve(),c;if(a.layerId){b=this._tryGetLayerInfo(a.layerId);if(!b)return c.reject("Invalid data source"),c;var d=b.getPopupInfo();this._getServiceDefinitionByLayerInfo(b).then(function(e){h.addAliasForLayerDefinition(e,d);c.resolve(e)}.bind(this),function(e){c.reject(e)});return c}if(a.frameWorkDsId){b&&(b=this._tryGetLayerInfo(b))&&(d=b.getPopupInfo());b=null;a=(this.appConfig.dataSource&&
this.appConfig.dataSource.dataSources)[a.frameWorkDsId];if("Features"===a.type)return b=k.clone(a.dataSchema),h.addAliasForLayerDefinition(b,d),c.resolve(b),c;if("FeatureStatistics"===a.type)return b={type:"Table",fields:[]},a=k.clone(a.dataSchema),b.fields=a.fields,a.groupByFields&&a.groupByFields[0]&&(b.groupByFields=k.clone(a.groupByFields)),h.addAliasForLayerDefinition(b,d),c.resolve(b),c}},_getDataSourceType:function(a){if(a){if(a.layerId)return"CLIENT_FEATURES";if(a.frameWorkDsId&&(a=(a=h.getDsTypeInfoMeta(a.frameWorkDsId,
this.appConfig))&&a.dsMeta)){if("Features"===a.type)return"FRAMEWORK_FEATURES";if("FeatureStatistics"===a.type)return"FRAMEWORK_STATISTICS"}}},_getServiceDefinitionByLayerInfo:function(a){var b=new f;a.getServiceDefinition().then(k.hitch(this,function(c){c?b.resolve(c):a.getLayerObject().then(function(d){d?(d=u.getFeatureLayerDefinition(d),b.resolve(d)):b.resolve(null)},function(d){b.reject(d)})}));return b},getFeaturesByDataSource:function(a){var b=new f,c=null;if("DATA_SOURCE_FEATURE_LAYER_FROM_MAP"===
a.dataSourceType){a=a.layerId;if(!a)return b.resolve(),b;this._getFeaturesForMapDS(a).then(function(d){c=d.features;b.resolve(c)},function(d){b.reject(d)})}else{a=a.frameWorkDsId;if(!a)return b.resolve(),b;this.getFeaturesForFrameDS(a).then(function(d){c=d.features;b.resolve(c)},function(d){b.reject(d)})}return b},_cutFeaturesForSetting:function(a){a&&1E3<a.length&&(a=a.slice(0,1E3));return a},_getFeaturesForMapDS:function(a){var b=new f,c=this._tryGetLayerInfo(a);if(!c)return b.reject("Can not get layerInfo by the layer id of this data source."),
b;a=c.getUrl();var d=c.getFilter();if(!a)return console.warn("Invalid layer url of data source, use client features."),c.getLayerObject().then(function(e){b.resolve({features:e.graphics})}),b;c=new p;c.outSpatialReference=this.map.spatialReference;c.where=d||"1\x3d1";c.outFields=["*"];c.returnGeometry=!1;return(new q(a)).execute(c)},getFeaturesForFrameDS:function(a){var b=new f;if(this.cacheData[a])return b.resolve(this.cacheData[a]),b;if("widget"===h.parseDataSourceId(a).from)return b.resolve({}),
b;var c=this.dataSourceManager.getDataSourceConfig(a);c=k.clone(c);if(!c)return b.reject("Can not get vaild data source config by the frameWorkDsId of this data source."),b;c.filterByExtent=!1;this.dataSourceManager.doQuery(c).then(function(d){this.cacheData[a]=d;b.resolve(d)}.bind(this),function(d){b.reject(d)});return b}})});