(()=>{function t(i,r,o,a,s){function t(t,e){var n=this;n.scope=t,n.filter=e||{dateFrom:moment().subtract(1,"days").startOf("day").format(r.dateFullFormat),dateTo:moment().format(r.dateFullFormat)},n.active={},n.events={},n.loadMore={},n._paths={},n.canceler=i.defer(),Object.defineProperty(n,"state",{get:function(){return this.realtimeStarted}})}return t.prototype.abortRequests=function(){this.canceler.resolve(),this.canceler=i.defer()},t.prototype.onPaths=function(t){this.callback=t},t.prototype._fetchAllEvents=function(){var t=this,e=_.keys(_.pickBy(t.active,_.identity)),e=_.map(e,_.bind(t._fetchEvents,t));return i.all(e).then(_.bind(t._notify,t,!0))},t.prototype._notify=function(t){var e=this;e.callback&&e.callback(L.polyline(_.values(e._paths),{color:"darkblue"}),t)},t.prototype._fetchEvents=function(e){var n=this,t={source:e,type:"c8y_LocationUpdate",pageSize:1e3,withTotalPages:!0};return _.assign(t,n.filter),n._fetching=a.list(t,{timeout:n.canceler.promise}).then(function(t){t=n._filterEvents(t);return n._buildEventListCallback(e,!0)(t)}),n._fetching},t.prototype._filterEvents=function(t){var e=this.trackingOptions||{},n=_.filter(t,function(t){return"c8y_LocationUpdate"===t.type&&_.isUndefined(e.gps)}),i=_.filter(_.keys(t),function(t){return _.isNaN(_.toNumber(t))});return _.assign(n,_.pick(t,i)),n},t.prototype._buildLoadMore=function(t,e){var n=this;return function(){return n._fetching=e().then(n._filterEvents.bind(n)).then(n._buildEventListCallback(t,!1)).then(function(t){t.length&&n._notify(!0)}),n._fetching}},t.prototype._buildEventListCallback=function(n,i){var r=this;return function(t){var e=t.paging;return r.events[n]=i?t:(r.events[n]||[]).concat(t),r._paths[n]=_(r.events[n]).filter(s.getLocationFragment).map(function(t){var e=s.getLocationFragment(t),e=L.latLng(e.lat,e.lng);return e.time=t.time,e}).value(),r.loadMore[n]=e.next&&r._buildLoadMore(n,e.next),r._fetching=!1,r._paths[n]}},t.prototype.changeInterval=function(t){var e=this;return e.filter=t,e.abortRequests(),e._fetchAllEvents()},t.prototype.refresh=function(){return this.abortRequests(),this._fetchAllEvents()},t.prototype.activate=function(t,e){var n=this;return n.filter.dateTo=moment().format(r.dateFullFormat),n.active[t]=!0,n.trackingOptions=e,n.setupRealtime(),n._fetchEvents(t).then(function(t){t.length&&n._notify(!0)})},t.prototype.getActiveDeviceIds=function(){return _.keys(this.active)},t.prototype.deactivate=function(t){var e=this;delete e.active[t],delete e._paths[t],delete e.events[t],e._notify(),e.setupRealtime()},t.prototype.deactivateAll=function(){var e=this;_.forEach(_.keys(e.active),function(t){delete e.active[t],delete e._paths[t],delete e.events[t]}),e._notify(),e.setupRealtime()},t.prototype._onNotification=function(t,e){var n,r=this;function i(t,e){var n=r.filter.revert?1:-1,i=_.sortedIndexBy(t,e,function(t){return n*moment(t.time).valueOf()});t.splice(i,0,e)}r.active[e.source.id]&&(n=s.getLocationFragment(e))&&r._filterEvents([e]).length&&moment(e.time).isAfter(r.filter.dateFrom)&&(r._paths[e.source.id]=r._paths[e.source.id]||[],n=_.assign(L.latLng(n.lat,n.lng),{time:e.time}),i(r._paths[e.source.id],n),r.events[e.source.id]=r.events[e.source.id]||[],i(r.events[e.source.id],e),r._notify(!0))},t.prototype.realtimeId=function(){return"c8yTracker-".concat(this.scope.$id)},t.prototype.setupRealtime=function(){var e,t,n,i,r;this.realtimeStarted&&(e=_.bind(this._onNotification,this),i=_.keys(this.active).map(function(t){return"/events/".concat(t)}),n=this.subscribedChannels,t=_.difference(n,i),n=_.difference(n,t),i=_.difference(i,n),r=this.realtimeId(),this.subscribedChannels=_.union(n,i),_.forEach(i,function(t){o.addListener(r,t,o.realtimeActions().CREATE,e),o.start(r,t)}),_.forEach(t,function(t){o.removeSubscriber(r,t)}))},t.prototype.cleanRealtime=function(){var t=this.subscribedChannels,e=this.realtimeId();_.forEach(t,function(t){o.removeSubscriber(e,t)}),this.subscribedChannels=[]},t.prototype.start=function(){var t=this;if(t.realtimeStarted)throw new Error("Tracker: Realtime is already started.");t.realtimeStarted=!0,t.scopeDestroyer&&t.scopeDestroyer(),t.scopeDestroyer=t.scope.$on("$destroy",function(){t.cleanRealtime()}),t.setupRealtime()},t.prototype.stop=function(){if(!this.realtimeStarted)throw new Error("Tracker: Cannot stop realtime, it has not been started.");this.realtimeStarted=!1,this.scopeDestroyer(),this.cleanRealtime()},{Tracker:t}}t.$inject=["$q","c8yBase","c8yRealtime","c8yEvents","c8yGeo"],angular.module("c8y.core").factory("c8yTracker",t)})();