// 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/_base/lang dojo/_base/array dojo/promise/all dojo/Deferred jimu/utils jimu/ServiceDefinitionManager esri/tasks/query esri/tasks/QueryTask esri/tasks/FeatureSet".split(" "),function(r,e,n,t,p,q,u,k,l,v){var g=r(null,{queryType:0,nextPageIndex:1,pageSizeOption:null,layerDefinition:null,layerDefinitionDef:null,type1CountDef:null,type2ObjectIdsDef:null,type3QueryDef:null,sdf:null,url:null,query:null,pageSize:1E3,constructor:function(a){this.sdf=u.getInstance();this.url=
a.url;this.query=a.query;this.pageSizeOption=a.pageSize;this.query.outFields&&0!==this.query.outFields.length||(this.query.outFields=["*"]);this.pageSize=0<a.pageSize?a.pageSize:1E3;this.getFeatureCount()},getFeatureCount:function(){return this._getQueryType().then(e.hitch(this,function(a){return 1===a?this._getCountForQueryType1():2===a?this._getCountForQueryType2():this._getCountForQueryType3()}))},getPageCount:function(){return this.getFeatureCount().then(e.hitch(this,function(a){return 0===a?
0:Math.ceil(a/this.pageSize)}))},getAllFeatures:function(){return this.getPageCount().then(e.hitch(this,function(a){if(0<a){for(var b=[],c=1;c<=a;c++)b.push(this.queryByPage(c));return t(b).then(e.hitch(this,function(d){for(var h=[],f=0;f<d.length;f++)d[f].features&&0<d[f].features.length&&(h=h.concat(d[f].features));h=this._setOrderByFieldValue(h);d=d[0];d.features=h;return d}))}this._getEmptyFeatureSet().then(e.hitch(this,function(d){return d}))}))},_setOrderByFieldValue:function(a){if(2===this.queryType||
3===this.queryType){var b=this.query.outFields;a.sort(function(c,d){c=c.attributes[b];d=d.attributes[b];return"string"===typeof c&&"string"===typeof d?c.localeCompare(d):c<d?-1:c===d?0:1})}return a},_getEmptyFeatureSet:function(){var a=new p;this.layerDefinition&&this.layerDefinition.geometryType?a.resolve(this._getEmptyFeatureSetHandler()):this._getLayerDefinition().then(e.hitch(this,function(){a.resolve(this._getEmptyFeatureSetHandler())}));return a},_getEmptyFeatureSetHandler:function(){var a=
new v;a.features=[];a.geometryType=this.layerDefinition.geometryType;a.fields=[];var b=e.clone(this.layerDefinition.fields);0<=this.query.outFields.indexOf("*")?a.fields=b:a.fields=n.filter(b,e.hitch(this,function(c){return 0<=this.query.outFields.indexOf(c.name)}));return a},getCurrentPageIndex:function(){return this.nextPageIndex},queryNextPage:function(){var a=this.nextPageIndex;this.nextPageIndex++;return this.queryByPage(a)},queryByPage:function(a){var b=null;0>=a?(b=new p,this._getEmptyFeatureSet().then(e.hitch(this,
function(c){b.resolve(c)}))):b=this.getPageCount().then(e.hitch(this,function(c){a>c&&this._getEmptyFeatureSet().then(e.hitch(this,function(d){return d}));return 1===this.queryType?this._queryPageForType1(a):2===this.queryType?this._queryPageForType2(a):this._queryPageForType3(a)}));return b},_getQueryType:function(){var a=new p;if(0<this.queryType)a.resolve(this.queryType);else return this._getLayerDefinition().then(e.hitch(this,function(b){return this.queryType=g.getQueryType(b)}));return a},_getDefStatus:function(a){return a?
a.isFulfilled()?a.isResolved()?2:-1:1:0},_getLayerDefinition:function(){0>=this._getDefStatus(this.layerDefinitionDef)&&(this.layerDefinitionDef=this.sdf.getServiceDefinition(this.url).then(e.hitch(this,function(a){this.layerDefinition=a;var b=a.maxRecordCount;0<b&&(!(0<this.pageSizeOption)||this.pageSize>a.maxRecordCount)&&(this.pageSize=b);return this.layerDefinition})));return this.layerDefinitionDef},_getCountForQueryType1:function(){0>=this._getDefStatus(this.type1CountDef)&&(this.type1CountDef=
this._queryCount());return this.type1CountDef},_getCountForQueryType2:function(){return this._getObjectIdsForQueryType2().then(e.hitch(this,function(a){return a.length}))},_getObjectIdsForQueryType2:function(){0>=this._getDefStatus(this.type2ObjectIdsDef)&&(this.type2ObjectIdsDef=this._queryIds());return this.type2ObjectIdsDef},_getCountForQueryType3:function(){return this._doQueryForQueryType3().then(e.hitch(this,function(a){return(a.features||[]).length}))},_doQueryForQueryType3:function(){0>=this._getDefStatus(this.type3QueryDef)&&
(this.type3QueryDef=this._query());return this.type3QueryDef},_queryPageForType1:function(a){return this._queryWithPaginationAndOrder((a-1)*this.pageSize)},_queryPageForType2:function(a){return this._getObjectIdsForQueryType2().then(e.hitch(this,function(b){var c=(a-1)*this.pageSize;b=b.slice(c,c+this.pageSize);return this._queryByObjectIds(b)}))},_queryPageForType3:function(a){return this._doQueryForQueryType3().then(e.hitch(this,function(b){var c=e.mixin({},b);c.features=[];var d=(a-1)*this.pageSize;
c.features=(b.features||[]).slice(d,d+this.pageSize);return c}))},_tryLocaleNumber:function(a){var b=q.localizeNumber(a);if(null===b||void 0===b)b=a;return b},_tryLocaleDate:function(a){var b=q.localizeDate(a);b||(b=a.toLocaleDateString());return b},_getLayerIndexByLayerUrl:function(a){var b=a.lastIndexOf("/");a=a.slice(b+1,a.length);return parseInt(a,10)},_getServiceUrlByLayerUrl:function(a){var b=a.lastIndexOf("/");return a.slice(0,b)},_isImageServiceLayer:function(a){return-1<a.indexOf("/ImageServer")},
_isTable:function(a){return"Table"===a.type},_getObjectIdField:function(){return this.layerDefinition.objectIdField},_query:function(a){var b=new k;b.where=a||this.query.where;b.geometry=this.query.geometry;b.outSpatialReference=this.query.outSpatialReference;b.returnGeometry=this.query.returnGeometry;b.spatialRelationship=this.query.spatialRelationship;b.outFields=this.query.outFields;b.returnDistinctValues=this.query.returnDistinctValues;return(new l(this.url)).execute(b)},_queryIds:function(){var a=
new k;a.where=this.query.where;a.geometry=this.query.geometry;a.returnGeometry=!1;a.spatialRelationship=this.query.spatialRelationship;a.outSpatialReference=this.query.outSpatialReference;return(new l(this.url)).executeForIds(a).then(e.hitch(this,function(b){b||(b=[]);return b}))},_queryByObjectIds:function(a){var b=new p,c=new k;c.returnGeometry=this.query.returnGeometry;c.outSpatialReference=this.query.outSpatialReference;c.spatialRelationship=this.query.spatialRelationship;c.outFields=this.query.outFields;
c.objectIds=a;(new l(this.url)).execute(c).then(e.hitch(this,function(d){b.resolve(d)}),e.hitch(this,function(d){if(400===d.code){var h=this._getObjectIdField(),f="",w=a.length;n.forEach(a,e.hitch(this,function(m,x){f+=h+" \x3d "+m;x!==w-1&&(f+=" OR ")}));this._query(f).then(e.hitch(this,function(m){b.resolve(m)}),e.hitch(this,function(m){b.reject(m)}))}else b.reject(d)}));return b},_queryCount:function(){var a=new k;a.where=this.query.where;a.geometry=this.query.geometry;a.outSpatialReference=this.query.outSpatialReference;
a.spatialRelationship=this.query.spatialRelationship;a.returnGeometry=!1;return(new l(this.url)).executeForCount(a)},_queryWithPaginationAndOrder:function(a){var b=new k;b.where=this.query.where;b.geometry=this.query.geometry;b.outSpatialReference=this.query.outSpatialReference;b.returnGeometry=this.query.returnGeometry;b.spatialRelationship=this.query.spatialRelationship;b.outFields=this.query.outFields;b.returnDistinctValues=this.query.returnDistinctValues;b.start=a;b.num=this.pageSize;(a=this.query.orderByFields)&&
0<a.length&&(b.orderByFields=a,a=n.map(a,e.hitch(this,function(c){return c.split(" ")[0]})),n.forEach(a,e.hitch(this,function(c){0>b.outFields.indexOf(c)&&b.outFields.push(c)})));return(new l(this.url)).execute(b)}});g._isServiceSupportsOrderBy=function(a){var b=!1;a.advancedQueryCapabilities&&a.advancedQueryCapabilities.supportsOrderBy&&(b=!0);return b};g._isServiceSupportsPagination=function(a){var b=!1;a.advancedQueryCapabilities&&a.advancedQueryCapabilities.supportsPagination&&(b=!0);return b};
g._isSupportObjectIds=function(a){var b=0;a=a.currentVersion?a:a.toJson().layerDefinition;a.currentVersion&&(b=parseFloat(a.currentVersion));return 10<=b||a.hasOwnProperty("typeIdField")};g.getQueryType=function(a){var b=-1;return b=g._isServiceSupportsOrderBy(a)&&g._isServiceSupportsPagination(a)?1:g._isSupportObjectIds(a)?2:3};return g});