// 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/array dojo/_base/declare dojo/_base/lang dojo/Deferred dojo/DeferredList esri/geometry/geometryEngineAsync esri/tasks/BufferParameters esri/tasks/GeometryService esri/tasks/query esri/tasks/QueryTask".split(" "),function(v,w,l,n,x,r,t,p,y,z){return w([],{constructor:function(a,e){this.map=a;this._geometryService=new p(e)},createUnionOfGeometries:function(a){var e=new n;if(!Array.isArray(a))return e.reject("No geometries to buffer"),e;r.union(a).then(l.hitch(this,function(f){f?e.resolve(f):
e.reject("Unioning failed")}),l.hitch(this,function(){e.reject("Unioning failed")}));return e},bufferGeometries:function(a,e){var f=new n;if(!Array.isArray(a))return f.reject("No geometries to buffer"),f;var c=new t;c.bufferSpatialReference=this.map.spatialReference;c.distances=e;c.unionResults=!0;c.geodesic=!0;c.geometries=a;c.outSpatialReference=this.map.spatialReference;c.unit=p.UNIT_METER;this._geometryService.buffer(c,function(g){Array.isArray(g)&&0<g.length?f.resolve(g[0]):f.reject("Buffering failed")},
function(g){f.reject("Buffering failed: "+g)});return f},groupGeometriesByType:function(a){var e=[],f=[],c=[],g=[],h=[],b=new n;0<a.length?(v.forEach(a,l.hitch(this,function(d){"point"===d.type?(e.push(d),g.push(.2)):"polyline"===d.type?(f.push(d),h.push(.2)):"polygon"===d.type&&c.push(d)})),a=[],0<e.length&&a.push(this.bufferGeometries(e,g)),0<f.length&&a.push(this.bufferGeometries(f,h)),0<a.length?(new x(a)).then(l.hitch(this,function(d){if(0<d.length){for(var k=0;k<d.length;k++)c.push(d[k][1]);
b.resolve(c)}}),function(d){b.reject("Buffering point/line geometires failed: "+d)}):b.resolve(c)):b.resolve(a);return b},createBufferFromGeometries:function(a,e){var f,c;var g=new n;if(!Array.isArray(a)||0>=a.length)return g.reject("No geometries to buffer"),g;this.groupGeometriesByType(a).then(l.hitch(this,function(h){f=e;r.union(h).then(l.hitch(this,function(b){b?0<f||"polygon"===b.type?(0===f&&(f=-.1),c=new t,c.bufferSpatialReference=this.map.spatialReference,c.distances=[f],c.geodesic=!0,c.geometries=
[b],c.outSpatialReference=this.map.spatialReference,c.unit=p.UNIT_METER,this._geometryService.buffer(c,function(d){Array.isArray(d)&&0<d.length?g.resolve(d[0]):g.reject("Buffering failed")},function(d){g.reject("Buffering failed: "+d)})):g.resolve(b):g.reject("Unioning failed")}),function(b){g.reject(b)})}));return g},findIntersectingFeatures:function(a,e,f,c,g){var h=new n,b={};e=new z(e);var d=new y;a&&(d.geometry=a);d.returnGeometry=!0;d.outFields=f;c&&(d.where=c);g&&(d.outSpatialReference=g);
e.execute(d,l.hitch(this,function(k){k&&Array.isArray(k.features)&&0<k.features.length?r.union(this._getFeatureGeometries(k.features)).then(l.hitch(this,function(q){if(Array.isArray(q.rings)&&0<q.rings.length){var m=new t;m.bufferSpatialReference=this.map.spatialReference;m.distances=[-.1];m.geodesic=!0;m.geometries=[q];m.outSpatialReference=this.map.spatialReference;m.unit=p.UNIT_METER;this._geometryService.buffer(m,function(u){Array.isArray(u)&&0<u.length?(b.intersectGeometry=a,b.features=k.features,
b.highlight=u[0],h.resolve(b)):h.reject("Buffering union failed")})}else b.intersectGeometry=a,b.features=k.features,b.highlight=q,h.resolve(b)})):(b.intersectGeometry=a,b.features=[],b.highlight=null,h.resolve(b))}),function(k){h.reject(k)});return h},find:function(a,e,f,c,g){var h=new n;this.findIntersectingFeatures(a,e,f,g).then(l.hitch(this,function(b){console.log(b.features.length+" "+c+" features found");h.resolve(b)}),function(b){h.reject(b)});return h},_getFeatureGeometries:function(a){return v.map(a,
l.hitch(this,function(e){return e.geometry}))}})});