// 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/layers/TrackManager","dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/has dojo/dom-construct dojox/gfx ../kernel ../graphic ../geometry/Polyline ./GraphicsLayer".split(" "),function(l,m,d,n,p,q,r,t,u,v){var w=-1!==q.renderer.toLowerCase().indexOf("canvas");l=l(null,{declaredClass:"esri.layers._TrackManager",constructor:function(a){this.layer=a;this.trackMap={};this.trackLineMap={}},initialize:function(a){this.map=a;var b=this.layer,c=b._getRenderer(),c=c&&c.trackRenderer;if("esriGeometryPoint"===
b.geometryType){var f=this.container=new v._GraphicsLayer({id:b.id+"_tracks",_child:!0,visible:b.visible,minScale:b.minScale,maxScale:b.maxScale});f.loaded=!0;f.onLoad(f);f._setMap(a,b._div);if(!w){a=f._div.getNode();var e=b._div.getNode();a&&e&&p.place(a,e,"first")}f.setRenderer(c);b.on("visibility-change",m.hitch(this,function(b){this.container.setVisibility(b.visible);this.container.evaluateSuspension()}));b.on("scale-range-change",m.hitch(this,function(){this.container.setScaleRange(this.layer.minScale,
this.layer.maxScale)}))}},addFeatures:function(a){var b=this.trackMap,c=this.layer,f=c._trackIdField,e=[];d.forEach(a,function(a){var c=a.attributes[f];(b[c]=b[c]||[]).push(a);-1===d.indexOf(e,c)&&e.push(c)});var g=c._startTimeField,k=c.objectIdField,h=function(a,b){var c=a.attributes[g],e=b.attributes[g];return c===e?a.attributes[k]<b.attributes[k]?-1:1:c<e?-1:1};d.forEach(e,function(a){b[a].sort(h)})},trimTracks:function(a){function b(a){for(a=c[a]||[];a.length>f;)e.push(a.shift())}var c=this.trackMap,
f=this.layer.maximumTrackPoints||0,e=[],g;if(!f)return e;if(a)d.forEach(a,function(a){b(a)});else for(g in c)c.hasOwnProperty(g)&&b(g);return e},drawTracks:function(a){function b(a){var b=e[a],h,d,l;d=c.trackLineMap[a];f.remove(d);delete c.trackLineMap[a];d=null;if(!b||2>b.length)return!1;d=[];for(h=b.length-1;0<=h;h--)(l=b[h].geometry)&&d.push([l.x,l.y]);b={};b[k]=a;1<d.length&&(d=new t(new u({paths:[d],spatialReference:g}),null,b),f.add(d),c.trackLineMap[a]=d)}var c=this,f=this.container,e,g,k,
h;if(f)if(e=this.trackMap,g=this.map.spatialReference,k=this.layer._trackIdField,a)d.forEach(a,function(a){b(a)});else for(h in e)e.hasOwnProperty(h)&&b(h)},refreshTracks:function(a){function b(a){var b,d;c.drawTracks([a]);if(g&&g.latestObservationRenderer)for(a=f[a]||[],b=a.length,d=0;d<b;d++)e._repaint(a[d],null,!0)}var c=this,f=this.trackMap,e=this.layer,g=e._getRenderer(),k;if(a)d.forEach(a,function(a){b(a)});else for(k in f)f.hasOwnProperty(k)&&b(k);this.moveLatestToFront()},moveLatestToFront:function(a){d.forEach(this.getLatestObservations(a),
function(a){var b=a._shape;b&&b._moveToFront();this._repaint(a,null,!0)},this.layer)},getLatestObservations:function(a){function b(a){a=e[a];return a[a.length-1]}var c=[],f=this.layer._getRenderer(),e=this.trackMap,g;if(!f.latestObservationRenderer)return c;if(a)d.forEach(a,function(a){c.push(b(a))});else for(g in e)e.hasOwnProperty(g)&&c.push(b(g));return c},clearTracks:function(a){var b=this.getLatestObservations(a),c=this.container,f;a?d.forEach(a,function(a){delete this.trackMap[a];c&&(f=this.trackLineMap[a],
c.remove(f),delete this.trackLineMap[a])},this):(this.trackMap={},this.trackLineMap={},c&&c.clear());d.forEach(b,function(a){this._repaint(a,null,!0)},this.layer)},isLatestObservation:function(a){var b=this.trackMap[a.attributes[this.layer._trackIdField]];return b?b[b.length-1]===a:!1},destroy:function(){var a=this.container;a&&(a.clear(),a._unsetMap(this.map,this.layer._div));this.map=this.layer=this.trackMap=this.container=null}});n("extend-esri")&&m.setObject("layers._TrackManager",l,r);return l});