// All material copyright ESRI, All Rights Reserved, unless otherwise specified.
// See http://js.arcgis.com/3.23/esri/copyright.txt for details.
//>>built
define("esri/dijit/editing/SelectionHelper","dojo/_base/declare dojo/_base/lang dojo/_base/connect dojo/_base/array dojo/has dojo/DeferredList ../../geometry/Extent ../../geometry/Point ../../geometry/ScreenPoint ../../layers/FeatureLayer ../../tasks/query ../../kernel".split(" "),function(k,g,l,f,q,r,t,u,n,h,p,v){k=k(null,{declaredClass:"esri.dijit.editing.SelectionHelper",constructor:function(a){this._settings=a||{};this._sConnects=[];this._mapServiceCount=0;this._map=this._settings.map;this._tolerance=
this._settings.singleSelectionTolerance;this._initMapServiceInfos(this._settings.layers)},destroy:function(){for(var a in this._sConnects)this._sConnects.hasOwnProperty(a)&&l.disconnect(this._sConnects[a])},selectFeatures:function(a,b,d,c){d===h.SELECTION_NEW&&(this._resetMapServiceInfos(),this.getSelection(a));var e=[];f.forEach(a,function(a){if(!0===a.visible&&!0===a._isMapAtVisibleScale()){var c=d;a._isSelOnly&&c===h.SELECTION_NEW&&(c=h.SELECTION_ADD);e.push(a.selectFeatures(b,c))}});(new r(e)).addCallback(g.hitch(this,
function(b){var e=[];f.forEach(b,function(b,d){f.forEach(b[1],function(b){(b=a[d]._mode._getFeature(b.attributes[a[d].objectIdField])||null)&&e.push(b)},this)},this);this._mapServiceCount?((b=d===h.SELECTION_SUBTRACT)?(this._resetMapServiceInfos(),this._createLayerDefs(this._getLayerInfosFromSelection(a))):this._createLayerDefs(this._getLayerInfosFromFeatures(e)),this._updateLayerDefs(this._mapServiceInfos,!1,!(e&&e.length||b),g.hitch(this,c,e))):c(e)}))},selectFeaturesByGeometry:function(a,b,d,c){var e=
b;-1!==b.declaredClass.indexOf("Extent")&&b.xmax===b.xmin&&b.ymax===b.ymin&&(e=new u(b.xmax,b.ymax,b.spatialReference));e=-1!==e.declaredClass.indexOf("Point")?this._extentFromPoint(e):e;b=new p;b.geometry=e;this.selectFeatures(a,b,d,c)},clearSelection:function(a){f.forEach(this._nonSelOnlyLayers,function(a){a.clearSelection&&a.clearSelection()});if(this._mapServiceCount){this._resetMapServiceInfos();var b=this._getLayerInfosFromSelection(this._settings.layers);f.some(b,function(a){return a.oids&&
a.oids.length})&&(this._createLayerDefs(b),this._updateLayerDefs(this._mapServiceInfos,!0,a||!1))}},findMapService:function(a){var b=this._map,d=b.layerIds;a=a&&a._url&&a._url.path?a._url.path.toLowerCase():"";var c,e,f;for(e in d)if(d.hasOwnProperty(e)&&(c=b.getLayer(d[e]),f=c._url?c._url.path?c._url.path.toLowerCase().replace("mapserver","featureserver"):c._url.toLowerCase().replace("mapserver","featureserver"):"",a.substr(0,f.length)===f&&"esri.layers.ArcGISDynamicMapServiceLayer"===c.declaredClass))return c},
getSelection:function(a){var b=[];f.forEach(a,function(a){a._isSelOnly&&b.push(this._createLayerInfo(a))},this);f.forEach(b,function(a){var b=this._createMapServiceInfo(this.findMapService(a.layer));b&&(b.layerInfos[a.layer.layerId]=a)},this)},_initMapServiceInfos:function(a){this._nonSelOnlyLayers=[];this._mapServiceInfos=[];f.forEach(a,function(a){var b=this.findMapService(a);b?(this._mapServiceCount++,this._createMapServiceInfo(b),b&&b.setDisableClientCaching(!0)):this._nonSelOnlyLayers.push(a)},
this)},_createMapServiceInfo:function(a){if(!a)return null;var b=this._mapServiceInfos,d=b[a.id];d||(d=b[a.id]={mapService:a,layerInfos:[],layerDefs:g.mixin([],a.layerDefinitions||[]),origLayerDefs:g.mixin([],a.layerDefinitions||[])});return d},_resetMapServiceInfo:function(a){f.forEach(a.layerInfos,this._resetLayerInfo);a.layerDefs=g.mixin([],a.origLayerDefs||[])},_resetMapServiceInfos:function(){var a=this._mapServiceInfos,b;for(b in a)a.hasOwnProperty(b)&&this._resetMapServiceInfo(a[b])},_createLayerInfo:function(a,
b){var d=a.objectIdField,c=b?[]:a.getSelectedFeatures();return{layer:a,selectedFeatures:c||[],oids:f.map(c,function(a){return a.attributes[d]})}},_resetLayerInfo:function(a){a&&(a.selectedFeatures=[],a.oids=[])},_updateLayerDefs:function(a,b,d,c){for(var e in a)if(a.hasOwnProperty(e)){var f=a[e],m=f.mapService,h=f.layerDefs=b?g.mixin([],f.origLayerDefs||[]):f.layerDefs;h?(d?c&&c():this._sConnects[m.id]=l.connect(m,"onUpdateEnd",g.hitch(this,"_onMapServiceUpdate",f,b,c)),m.setLayerDefinitions(h,d||
!1)):c&&c()}},_onMapServiceUpdate:function(a,b,d){l.disconnect(this._sConnects[a.mapService.id]);f.forEach(a.layerInfos,function(a){if(b)a&&a.layer.clearSelection();else{var c=new p;c.objectIds=a?a.oids:[];c.objectIds.length&&a.layer.selectFeatures(c,h.SELECTION_SUBTRACT)}},this);b&&this._resetMapServiceInfo(a);d&&d()},_createLayerDefs:function(a){f.forEach(a,function(a){var b=a.layer,c=this._createMapServiceInfo(this.findMapService(a.layer));if(c){var c=c.layerDefs,e=b.layerId,g='("'+b.objectIdField+
'" NOT IN (',h=a.oids;h&&h.length&&(f.forEach(a.oids,function(a,b){h=!0;b&&(g+=",");g+="'"+a+"'"}),g+="))",c[e]=c.length&&c[e]&&c[e].length?c[e]+(" AND"+g):g)}},this)},_getLayerInfosFromFeatures:function(a){var b=[];f.forEach(a,function(a){var c=a.getLayer();c&&c._isSelOnly&&(b[c.id]||(b[c.id]=this._createLayerInfo(c,!0)),b[c.id].selectedFeatures.push(a),b[c.id].oids.push(a.attributes[c.objectIdField]))},this);a=[];for(var d in b)b.hasOwnProperty(d)&&a.push(b[d]);return a},_getLayerInfosFromSelection:function(a){var b=
[];f.forEach(a,function(a){a._isSelOnly&&b.push(this._createLayerInfo(a,!1))},this);return b},_extentFromPoint:function(a){var b=this._tolerance,d=this._map,c=d.toScreen(a);a=new n(c.x-b,c.y+b);b=new n(c.x+b,c.y-b);a=d.toMap(a);b=d.toMap(b);return new t(a.x,a.y,b.x,b.y,d.spatialReference)}});q("extend-esri")&&g.setObject("dijit.editing.SelectionHelper",k,v);return k});