// 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/on dojo/sniff dojo/Evented dojo/Deferred dojo/_base/lang dojo/_base/array dojo/_base/declare jimu/utils jimu/symbolUtils jimu/SelectionManager jimu/LayerInfos/LayerInfos esri/graphic esri/tasks/query esri/tasks/QueryTask esri/layers/FeatureLayer esri/symbols/jsonUtils esri/geometry/geometryEngine".split(" "),function(q,w,x,r,d,m,y,t,u,z,A,B,n,v,g,C,D){return y([x],{baseClass:"jimu-featureset-chooser-core",_middleFeatureLayer:null,_isLoading:!1,_def:null,_isDestroyed:!1,_handles:null,
selectionManager:null,layerInfosObj:null,layerInfo:null,map:null,featureLayer:null,drawBox:null,updateSelection:!1,fullyWithin:!1,constructor:function(a){d.mixin(this,a);this.layerInfosObj=A.getInstanceSync();this.selectionManager=z.getInstance();this.featureLayer.getSelectionSymbol()||this.selectionManager.setSelectionSymbol(this.featureLayer);a=t.getFeatureLayerDefinition(this.featureLayer);delete a.id;this._middleFeatureLayer=new g({layerDefinition:a,featureSet:null},{id:"featureLayer_"+t.getRandomString()});
a=null;var b=this._middleFeatureLayer.geometryType;"esriGeometryPoint"===b?a=u.getDefaultMarkerSymbol():"esriGeometryPolyline"===b?a=u.getDefaultLineSymbol():"esriGeometryPolygon"===b&&(a=C.fromJson({style:"esriSFSSolid",color:[79,129,189,77],type:"esriSFS",outline:{style:"esriSLSSolid",color:[54,93,141,255],width:1.5,type:"esriSLS"}}));this._middleFeatureLayer.setSelectionSymbol(a);a=q(this.drawBox,"user-clear",d.hitch(this,this._onDrawBoxUserClear));b=q(this.drawBox,"draw-end",d.hitch(this,this._onDrawEnd));
this._handles=[a,b];if(this.layerInfo=this.layerInfosObj.getLayerInfoById(this.featureLayer.id))a=q(this.layerInfo,"filterChanged",d.hitch(this,this._handleFilterChange)),this._handles.push(a)},hideMiddleFeatureLayer:function(){if(this._middleFeatureLayer){this._middleFeatureLayer.hide();var a=this.selectionManager.getDisplayLayer(this._middleFeatureLayer.id);a&&a.hide()}},showMiddleFeatureLayer:function(){if(this._middleFeatureLayer){this._middleFeatureLayer.show();var a=this.selectionManager.getDisplayLayer(this._middleFeatureLayer.id);
a&&a.show()}},clear:function(a){this.drawBox.clear();this._clearMiddleFeatureLayer();a&&this.selectionManager.clearSelection(this.featureLayer)},getFeatures:function(){var a=new r,b=d.hitch(this,function(){var e=this.syncGetFeatures();a.resolve(e)}),c=d.hitch(this,function(e){a.reject(e)});1===this._getDeferredStatus(this._def)?this._def.then(b,c):b();return a},syncGetFeatures:function(){return(this.updateSelection?this.featureLayer:this._middleFeatureLayer).getSelectedFeatures()},isLoading:function(){return 1===
this._getDeferredStatus(this._def)},_onLoading:function(){this.drawBox.deactivate();this.emit("loading")},_onUnloading:function(){this.emit("unloading")},_getDeferredStatus:function(a){var b=0;return b=a?a.isResolved()?2:a.isRejected()?-1:1:0},_onDrawEnd:function(a,b,c,e,f,h){if(this.isLoading())throw"should not draw when loading";if(this.featureLayer.visible){var k=new r;this._def=k;var p=g.SELECTION_NEW;e&&(p=g.SELECTION_ADD);w("mac")?h&&(p=g.SELECTION_SUBTRACT):f&&(p=g.SELECTION_SUBTRACT);this.emit("loading");
this._getFeaturesByGeometry(a.geometry).then(d.hitch(this,function(l){this.selectionManager.updateSelectionByFeatures(this.updateSelection?this.featureLayer:this._middleFeatureLayer,l,p);this._onUnloading();k.resolve(l)}),d.hitch(this,function(l){console.error(l);this._onUnloading();k.reject(l)}))}},_addTolerance:function(a){var b=2.54*this.map.getScale()/9600;return D.buffer(a,10*b,"meters")},_getFeaturesByGeometry:function(a){"point"!==a.type&&"polyline"!==a.type||"esriGeometryPoint"!==this.featureLayer.geometryType||
(a=this._addTolerance(a));var b=new r,c=[];if(this.featureLayer.getMap()&&this.featureLayer.mode!==g.MODE_SELECTION)a=this.selectionManager.getClientFeaturesByGeometry(this.featureLayer,a,this.fullyWithin),0<a.length&&(c=m.map(a,d.hitch(this,function(f){return new B(f.toJson())}))),b.resolve(c);else{c=new n;c.geometry=a;c.outSpatialReference=this.map.spatialReference;c.returnGeometry=!0;c.spatialRelationship=this.fullyWithin?n.SPATIAL_REL_CONTAINS:n.SPATIAL_REL_INTERSECTS;(a=this.featureLayer.getDefinitionExpression())||
(a="1\x3d1");if(this.layerInfo){var e=this.layerInfo.getFilter();e&&(a="("+a+") AND ("+e+")")}a&&(c.where=a);c.outFields=["*"];(new v(this.featureLayer.url)).execute(c).then(d.hitch(this,function(f){b.resolve(f.features)}),d.hitch(this,function(f){b.reject(f)}))}return b},_onDrawBoxUserClear:function(){this.clear()},_clearMiddleFeatureLayer:function(){this._middleFeatureLayer&&(this._middleFeatureLayer.clear(),this.selectionManager.clearSelection(this._middleFeatureLayer))},_handleFilterChange:function(){var a=
this.layerInfo.getFilter()||"1\x3d1",b=this.featureLayer.getSelectedFeatures(),c=this.featureLayer.objectIdField;if(b&&0<b.length){var e=m.map(b,function(h){return h.attributes[c]}),f=new n;f.where=c+" in ("+e.join(",")+") AND "+a;(new v(this.featureLayer.url)).executeForIds(f).then(d.hitch(this,function(h){m.forEach(b,function(k){0<=h.indexOf(k.attributes[c])?k.show():k.hide()});this.selectionManager._isLayerNeedDisplayLayer(this.featureLayer)&&this.selectionManager._updateDisplayLayer(this.featureLayer,
b,g.SELECTION_NEW);this.featureLayer.emit("selection-complete",{features:this.featureLayer.getSelectedFeatures()})}),d.hitch(this,function(h){console.error(h)}))}},destroy:function(){this._isDestroyed||(m.forEach(this._handles,d.hitch(this,function(a){a.remove()})),this._handles=null,this.clear());this._isDestroyed=!0}})});