// 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/Deferred jimu/BaseWidgetSetting esri/tasks/query esri/tasks/QueryTask esri/geometry/Extent esri/geometry/geometryEngine esri/SpatialReference esri/geometry/webMercatorUtils dojo/_base/array".split(" "),function(I,u,z,J,K,L,M,C,H,N,A){return I([J],{baseClass:"jimu-widget-smartEditor-widgets-intersection",layerFieldsObj:{},queriedLayers:[],layersInvolvedInIgnoreLayerRanking:[],maxToleranceForlayer:{},maxToleranceForlayerInPx:{},completeFeatures:{},postCreate:function(){this.startup();
this.layerFieldsObj={};this.queriedLayers=[];this.maxToleranceForlayer={};this.maxToleranceForlayerInPx={};this.layersInvolvedInIgnoreLayerRanking=[];this.completeFeatures=[]},startup:function(){},_canGetGeodesicBuffers:function(a){var b=new H(4326);return a&&N.canProject(a,b)?!0:!1},_getbufferedGeometry:function(a,b){return(a=this._canGetGeodesicBuffers(a)?C.geodesicBuffer([a],[b],"meters",!0):C.buffer([a],[b],"meters",!0))&&0<a.length?a[0]:null},getToleranceValueInMeters:function(a){var b=0;a&&
(a.useDefault?(b=this.defaultToleranceSettings.value,a=this.defaultToleranceSettings.unit):(b=a.value,a=a.unit),b=this.convertToMeters(b,a));return b},setMaxToleranceForLayer:function(a,b){var c=this.getToleranceValueInMeters(b);if(b&&"px"===b.unit&&!b.useDefault){if(b=b.value,!this.maxToleranceForlayerInPx.hasOwnProperty(a)||this.maxToleranceForlayerInPx[a]<b)this.maxToleranceForlayerInPx[a]=b}else 0<c&&(!this.maxToleranceForlayer.hasOwnProperty(a)||this.maxToleranceForlayer[a]<c)?this.maxToleranceForlayer[a]=
c:0===c&&(b=this.defaultPixelsTolerance,!this.maxToleranceForlayerInPx.hasOwnProperty(a)||this.maxToleranceForlayerInPx[a]<b)&&(this.maxToleranceForlayerInPx[a]=b)},convertToMeters:function(a,b){var c=a;switch(b){case "meters":c=a;break;case "feet":c=.3048*a;break;case "kilometers":c=1E3*a;break;case "miles":c=1609.34*a}return c},getDistinctLayers:function(a,b){var c=new z;var d=new z;var f={};this.layerFieldsObj={};this.queriedLayers=[];this.ignoreLayerRanking=!1;this.drawnGeometry=b;if(a.fieldValues){for(var e in a.fieldValues){f[e]=
[];for(var l=0;l<a.fieldValues[e].length;l++){var h=a.fieldValues[e][l];if("Intersection"===h.actionName&&h.enabled){h.ignoreLayerRanking&&(this.ignoreLayerRanking=!0);for(var v=0;v<h.fields.length;v++){var g=h.fields[v];-1===f[e].indexOf(g.layerId)&&f[e].push(g.layerId);-1===this.layersInvolvedInIgnoreLayerRanking.indexOf(g.layerId)&&h.ignoreLayerRanking&&this.layersInvolvedInIgnoreLayerRanking.push(g.layerId);this.setMaxToleranceForLayer(g.layerId,g.toleranceSettings)}}}}this._getIntersectionsForEachField(f,
b,d)}else d.resolve({});d.then(u.hitch(this,function(){var m={},n;if(a.fieldValues)for(var w in a.fieldValues){m[w]={};for(var F=0;F<a.fieldValues[w].length;F++){var q=a.fieldValues[w][F];if("Intersection"===q.actionName&&q.enabled)if(q.ignoreLayerRanking){var r=this._getClosestFeature(q.fields);m[w]=r.attr;for(var B={},x=0;x<q.fields.length;x++){var k=q.fields[x];B.hasOwnProperty(k.layerId)||(B[k.layerId]=[]);-1===B[k.layerId].indexOf(k.field)&&B[k.layerId].push(k.field)}if(r.intersectingFeatures&&
1<r.intersectingFeatures.length){var t=[];A.forEach(r.intersectingFeatures,function(p){for(var G=B[p.layerId],D=0;D<G.length;D++)if(p.attributes&&p.attributes.hasOwnProperty(G[D])){var E=p.attributes[G[D]];null!==E&&""!==E&&0>t.indexOf(E)&&t.push(E)}});1<t.length&&(n||(n={}),n[w]=t)}}else for(k=0;k<q.fields.length;k++){var y=q.fields[k];x=y.layerId;if(!m[w].hasOwnProperty(x)&&(r=this._getProcessedFeature(x,y.toleranceSettings))&&r.attributes){q=r.attributes;k={};k[y.field]=q[y.field];m[w][x]=k;r.intersectingFeatures&&
1<r.intersectingFeatures.length&&"esriCTUseLayerName"!==y.field&&(t=[],A.forEach(r.intersectingFeatures,function(p){p=p.attributes[y.field];null!==p&&""!==p&&0>t.indexOf(p)&&t.push(p)}),1<t.length&&(n||(n={}),n[w]=t));break}}}}c.resolve({result:m,multipleValues:n})}));return c.promise},_getIntersectingFeatures:function(a,b){var c,d;var f=[];var e=0;if(d=this._jimuLayerInfos.getLayerOrTableInfoById(a))var l=d.layerObject;else return f;if(this.completeFeatures.hasOwnProperty(a)&&(d=this.completeFeatures[a],
b&&"px"!==b.unit&&(e=this.getToleranceValueInMeters(b)),0<d.length&&(e?c=this._getbufferedGeometry(this.drawnGeometry,e):this.maxToleranceForlayer.hasOwnProperty(a)&&this.maxToleranceForlayer[a]||this.maxToleranceForlayerInPx.hasOwnProperty(a)&&this.maxToleranceForlayerInPx[a]?c="point"===this.drawnGeometry.type?this.maxToleranceForlayerInPx.hasOwnProperty(a)?this.pointToExtent(this.map,this.drawnGeometry,this.maxToleranceForlayerInPx[a]):this.pointToExtent(this.map,this.drawnGeometry,this.defaultPixelsTolerance):
this.drawnGeometry:f=d,c)))for(a=0;a<d.length;a++)b=d[a],b.geometry&&C.intersects(c,b.geometry)&&(b.attributes.esriCTUseLayerName=l.name,f.push(b));return f},_getClosestFeature:function(a){var b,c,d=[];var f={};a=this._resetFieldsWithMapLayerOrder(a);for(b=0;b<a.length;b++){var e=[];var l=a[b].layerId;e=this._getIntersectingFeatures(l,a[b].toleranceSettings);for(c=0;c<e.length;c++)this.drawnGeometry&&e[c].geometry&&(e[c].distanceToLocation=C.distance(this.drawnGeometry,e[c].geometry,"meters")),e[c].layerId=
l,e[c].fields?e[c].fields.push(a[b].field):e[c].fields=[a[b].field];d=d.concat(e)}if(0<d.length){d.sort(function(g,m){return g.distanceToLocation<m.distanceToLocation?-1:g.distanceToLocation>m.distanceToLocation?1:0});var h=d[0].attributes;a=this._jimuLayerInfos.getLayerOrTableInfoById(d[0].layerId).layerObject;h.esriCTUseLayerName=a.name;var v={};A.forEach(d[0].fields,function(g){v[g]=h[g]});f[d[0].layerId]=v}return{attr:f,intersectingFeatures:d}},_resetFieldsWithMapLayerOrder:function(a){var b=
[];var c=this._jimuLayerInfos.getLayerInfoArray();A.forEach(c,u.hitch(this,function(d){A.forEach(a,u.hitch(this,function(f){d.id===f.layerId&&b.push(f)}))}));return b},_getProcessedFeature:function(a,b){var c=[];c=this._getIntersectingFeatures(a,b);if(0<c.length){var d=c[0].attributes;a=this._jimuLayerInfos.getLayerOrTableInfoById(a).layerObject;d.esriCTUseLayerName=a.name}return{attributes:d,intersectingFeatures:c}},_getIntersectionsForEachField:function(a,b,c){var d=Object.keys(a);var f=0;this.maxToleranceForlayer&&
(f=Object.keys(this.maxToleranceForlayer).length);if(0<d.length){var e=d[0];(this.ignoreLayerRanking||0<f?this._asyncIntersectionRequests(a[e],b):this._syncIntersectionRequests(a[e],b)).then(u.hitch(this,function(){this.ignoreLayerRanking||0<f?a[e]&&0===a[e].length&&delete a[e]:delete a[e];this._getIntersectionsForEachField(a,b,c)}))}else c.resolve()},_filterQueriedLayers:function(a){for(var b in this.layerFieldsObj){var c=a.indexOf(b);-1<c&&a.splice(c,1)}return a},_asyncIntersectionRequests:function(a,
b,c){c||(c=new z);a=this._filterQueriedLayers(a);0<a.length?this._getIntersectedFeatures(a[0],b).then(u.hitch(this,function(d){a.splice(0,1);0<a.length?(this._asyncIntersectionRequests(a,b,c),this._intersectionResult(d)):this._intersectionResult(d,c)})):c.resolve();return c.promise},_syncIntersectionRequests:function(a,b,c){c||(c=new z);a=this._filterQueriedLayers(a);0<a.length?this._getIntersectedFeatures(a[0],b).then(u.hitch(this,function(d){0<d.features.length?this._intersectionResult(d,c):(a.splice(0,
1),0<a.length?this._syncIntersectionRequests(a,b,c):this._intersectionResult(d,c))})):c.resolve();return c.promise},_intersectionResult:function(a,b){a.layerId&&(this.layerFieldsObj[a.layerId]={},a.features&&0<a.features.length&&(this.layerFieldsObj[a.layerId]=a.featuresAttributes));b&&b.resolve()},pointToExtent:function(a,b,c){if(0===c)return b;var d=a.extent.getWidth()/a.width;c*=d;return new M(b.x-c,b.y-c,b.x+c,b.y+c,a.spatialReference)},_getAppropriateGeometryForQuery:function(a,b){if(this.maxToleranceForlayer.hasOwnProperty(a))var c=
this._getbufferedGeometry(b,this.maxToleranceForlayer[a]);else c=b,"point"===b.type&&(c=this.maxToleranceForlayerInPx.hasOwnProperty(a)?this.pointToExtent(this.map,b,this.maxToleranceForlayerInPx[a]):this.pointToExtent(this.map,b,this.defaultPixelsTolerance));return c},_getIntersectedFeatures:function(a,b){var c;var d=new z;if(this._jimuLayerInfos.getLayerOrTableInfoById(a)){var f=this._jimuLayerInfos.getLayerOrTableInfoById(a);var e=this._jimuLayerInfos.getLayerOrTableInfoById(a).layerObject}else return d.resolve({layerId:a,
features:[],featuresAttributes:{}}),d.promise;var l=e.objectIdField||null;var h=new K;var v=new L(e.url);h.geometry=this._getAppropriateGeometryForQuery(a,b);h.outFields=["*"];if(f=f&&(f.subId||0===f.subId)&&f.parentLayerInfo&&f.parentLayerInfo.layerObject?this._getSubLayerDefinitionExpression(f.subId,f.parentLayerInfo):e&&e.getDefinitionExpression?e.getDefinitionExpression():"")h.where=f;-1<this.layersInvolvedInIgnoreLayerRanking.indexOf(a)||this.maxToleranceForlayer.hasOwnProperty(a)||this.maxToleranceForlayerInPx.hasOwnProperty(a)?
(h.returnGeometry=!0,h.outSpatialReference=new H(b.spatialReference.toJson())):h.returnGeometry=!1;v.execute(h,u.hitch(this,function(g){g&&g.features&&0<g.features.length?(l&&g.features.sort(function(m,n){m=parseInt(m.attributes[l],10);n=parseInt(n.attributes[l],10);return m>n?-1:1}),c=g.features[0].attributes):(g={features:[]},c={});this.completeFeatures[a]=g.features;d.resolve({layerId:a,features:g.features,featuresAttributes:c})}),u.hitch(this,function(){this.completeFeatures[a]=[];d.resolve({layerId:a,
features:[],featuresAttributes:{}})}));return d.promise},_getSubLayerDefinitionExpression:function(a,b){return b&&b.layerObject&&b.layerObject.layerDefinitions?b.layerObject.layerDefinitions[a]:b.parentLayerInfo?this._getSubLayerDefinitionExpression(a,b.parentLayerInfo):""}})});