/*
 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/editing/SelectionHelper",["dojo/_base/declare","dojo/_base/lang","dojo/_base/connect","dojo/_base/array","dojo/has","dojo/DeferredList","esri/geometry/Extent","esri/geometry/Point","esri/geometry/ScreenPoint","esri/layers/FeatureLayer","esri/tasks/query","esri/kernel"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c){var SH=_1(null,{declaredClass:"esri.dijit.editing.SelectionHelper",constructor:function(_d){this._settings=_d||{};this._sConnects=[];this._mapServiceCount=0;this._map=this._settings.map;this._tolerance=this._settings.singleSelectionTolerance;this._initMapServiceInfos(this._settings.layers);},destroy:function(){var _e;for(_e in this._sConnects){if(this._sConnects.hasOwnProperty(_e)){_e.disconnect(this._sConnects[_e]);}}},selectFeatures:function(_f,_10,_11,_12){if(_11===_a.SELECTION_NEW){this._resetMapServiceInfos();this.getSelection(_f);}var _13=[];_4.forEach(_f,function(_14){if(_14.visible===true&&_14._isMapAtVisibleScale()===true){var _15=_11;if(_14._isSelOnly&&_15===_a.SELECTION_NEW){_15=_a.SELECTION_ADD;}_13.push(_14.selectFeatures(_10,_15));}});var _16=new _6(_13);_16.addCallback(_2.hitch(this,function(_17){var _18=[];_4.forEach(_17,function(set,idx){_4.forEach(set[1],function(_19){var _1a=_19.attributes[_f[idx].objectIdField];_19=_f[idx]._mode._getFeature(_1a)||null;if(_19){_18.push(_19);}},this);},this);if(!this._mapServiceCount){_12(_18);return;}var _1b=_11===_a.SELECTION_SUBTRACT;if(_1b){this._resetMapServiceInfos();this._createLayerDefs(this._getLayerInfosFromSelection(_f));}else{this._createLayerDefs(this._getLayerInfosFromFeatures(_18));}this._updateLayerDefs(this._mapServiceInfos,false,!((_18&&_18.length)||_1b),_2.hitch(this,_12,_18));}));},selectFeaturesByGeometry:function(_1c,_1d,_1e,_1f){var _20=_1d;if(_1d.declaredClass.indexOf("Extent")!==-1){if(_1d.xmax===_1d.xmin&&_1d.ymax===_1d.ymin){_20=new _8(_1d.xmax,_1d.ymax,_1d.spatialReference);}}_20=(_20.declaredClass.indexOf("Point")!==-1)?this._extentFromPoint(_20):_20;var _21=new _b();_21.geometry=_20;this.selectFeatures(_1c,_21,_1e,_1f);},clearSelection:function(_22){var _23=this._nonSelOnlyLayers;_4.forEach(_23,function(_24){if(_24.clearSelection){_24.clearSelection();}});if(!this._mapServiceCount){return;}this._resetMapServiceInfos();var _25=this._getLayerInfosFromSelection(this._settings.layers);var _26=_4.some(_25,function(_27){return _27.oids&&_27.oids.length;});if(_26){this._createLayerDefs(_25);this._updateLayerDefs(this._mapServiceInfos,true,_22||false);}},findMapService:function(_28){var map=this._map,_29=map.layerIds,_2a=(_28&&_28._url)?_28._url.path.toLowerCase():"",_2b,_2c,_2d;for(_2c in _29){if(_29.hasOwnProperty(_2c)){_2b=map.getLayer(_29[_2c]);_2d=_2b._url?_2b._url.path.toLowerCase().replace("mapserver","featureserver"):"";if(_2a.substr(0,_2d.length)===_2d&&_2b.declaredClass==="esri.layers.ArcGISDynamicMapServiceLayer"){return _2b;}}}},getSelection:function(_2e){var _2f=[];_4.forEach(_2e,function(_30){if(_30._isSelOnly){_2f.push(this._createLayerInfo(_30));}},this);_4.forEach(_2f,function(_31){var _32=this._createMapServiceInfo(this.findMapService(_31.layer));if(_32){_32.layerInfos[_31.layer.layerId]=_31;}},this);},_initMapServiceInfos:function(_33){this._nonSelOnlyLayers=[];this._mapServiceInfos=[];_4.forEach(_33,function(_34){var _35=this.findMapService(_34);if(_35){this._mapServiceCount++;this._createMapServiceInfo(_35);if(_35){_35.setDisableClientCaching(true);}}else{this._nonSelOnlyLayers.push(_34);}},this);},_createMapServiceInfo:function(_36){if(!_36){return null;}var _37=this._mapServiceInfos;var _38=_37[_36.id];if(!_38){_38=_37[_36.id]={mapService:_36,layerInfos:[],layerDefs:_2.mixin([],_36.layerDefinitions||[]),origLayerDefs:_2.mixin([],_36.layerDefinitions||[])};}return _38;},_resetMapServiceInfo:function(_39){_4.forEach(_39.layerInfos,this._resetLayerInfo);_39.layerDefs=_2.mixin([],_39.origLayerDefs||[]);},_resetMapServiceInfos:function(){var _3a=this._mapServiceInfos,_3b;for(_3b in _3a){if(_3a.hasOwnProperty(_3b)){this._resetMapServiceInfo(_3a[_3b]);}}},_createLayerInfo:function(_3c,_3d){var _3e=_3c.objectIdField;var _3f=_3d?[]:_3c.getSelectedFeatures();return {layer:_3c,selectedFeatures:_3f||[],oids:_4.map(_3f,function(_40){return _40.attributes[_3e];})};},_resetLayerInfo:function(_41){if(!_41){return;}_41.selectedFeatures=[];_41.oids=[];},_updateLayerDefs:function(_42,_43,_44,_45){var _46;for(_46 in _42){if(_42.hasOwnProperty(_46)){var _47=_42[_46];var _48=_47.mapService;var _49=_47.layerDefs=(_43?_2.mixin([],_47.origLayerDefs||[]):_47.layerDefs);if(_49){if(!_44){this._sConnects[_48.id]=(_3.connect(_48,"onUpdateEnd",_2.hitch(this,"_onMapServiceUpdate",_47,_43,_45)));}else{if(_45){_45();}}_48.setLayerDefinitions(_49,_44||false);}else{if(_45){_45();}}}}},_onMapServiceUpdate:function(_4a,_4b,_4c){_3.disconnect(this._sConnects[_4a.mapService.id]);_4.forEach(_4a.layerInfos,function(_4d){if(_4b){if(_4d){_4d.layer.clearSelection();}}else{var _4e=new _b();_4e.objectIds=_4d?_4d.oids:[];if(_4e.objectIds.length){_4d.layer.selectFeatures(_4e,_a.SELECTION_SUBTRACT);}}},this);if(_4b){this._resetMapServiceInfo(_4a);}if(_4c){_4c();}},_createLayerDefs:function(_4f){_4.forEach(_4f,function(_50){var _51=_50.layer;var _52=this._createMapServiceInfo(this.findMapService(_50.layer));if(!_52){return;}var _53=_52.mapService;var _54=_52.layerDefs;var _55=_51.objectIdField;var _56=_51.layerId;var _57="(\""+_55+"\" NOT IN (";var _58=_50.oids;if(_58&&_58.length){_4.forEach(_50.oids,function(oid,idx){_58=true;if(idx){_57+=",";}_57+="'"+oid+"'";});_57+="))";if(_54.length&&(_54[_56]&&_54[_56].length)){_54[_56]+=" AND"+_57;}else{_54[_56]=_57;}}},this);},_getLayerInfosFromFeatures:function(_59){var _5a=[];_4.forEach(_59,function(_5b){var _5c=_5b.getLayer();if(_5c&&_5c._isSelOnly){if(!_5a[_5c.id]){_5a[_5c.id]=this._createLayerInfo(_5c,true);}_5a[_5c.id].selectedFeatures.push(_5b);_5a[_5c.id].oids.push(_5b.attributes[_5c.objectIdField]);}},this);var _5d=[],_5e;for(_5e in _5a){if(_5a.hasOwnProperty(_5e)){_5d.push(_5a[_5e]);}}return _5d;},_getLayerInfosFromSelection:function(_5f){var _60=[];_4.forEach(_5f,function(_61){if(_61._isSelOnly){_60.push(this._createLayerInfo(_61,false));}},this);return _60;},_extentFromPoint:function(_62){var _63=this._tolerance;var map=this._map;var _64=map.toScreen(_62);var _65=new _9(_64.x-_63,_64.y+_63);var _66=new _9(_64.x+_63,_64.y-_63);var _67=map.toMap(_65);var _68=map.toMap(_66);return new _7(_67.x,_67.y,_68.x,_68.y,map.spatialReference);}});if(_5("extend-esri")){_2.setObject("dijit.editing.SelectionHelper",SH,_c);}return SH;});