(()=>{function e(a,i,t,r,n,o,d,s,e){var l=this,c=(l.datePickerOptionsFrom={showWeeks:!0},l.datePickerOptionsTo={showWeeks:!0},a.refreshLoading=!1,[]),f=!0,u=_.throttle(v,100);function m(e,t){a.events&&!a.refreshLoading&&!t||(a.events=[],a.refreshLoading=!1),a.ordering="".concat(f?"-":"","dateTime"),a.events=_.uniqBy(a.events.concat(e),"id"),_.forEach(a.events,function(e){e.dateTime=new Date(e.time)}),a.paging=e.paging,L(a.events.length),a.onEventLoaded({qty:a.events.length})}function g(){e=a.realtimeChannel,k(),n.getStatus(a.$id,e)&&e!==a.realtimeChannel&&(b(),n.stop(a.$id,e),O());var e;a.refreshLoading=!0,h(),L()}function h(e){i=(e=e)||{},_.assign(i,a.filters),_.forOwn(i,function(e,t){e||delete i[t]}),(i.source||i.deviceId)&&(i.source=i.source||i.deviceId,delete i.deviceId,i.withSourceDevices=!0,i.withSourceAssets=!0),_.isUndefined(i.pageSize)&&(i.pageSize=10),e=a.timeRangeUnit||"day",i.dateFrom&&(i.dateFrom=moment(i.dateFrom).startOf(e).format(r.dateFullFormat)),i.dateTo&&(i.dateTo=moment(i.dateTo).endOf(e).format(r.dateFullFormat));var i,e=i;return t.list(e).then(m)}function v(){a.refreshLoading=!0,f?(f=!1,F().finally(function(){a.refreshLoading=!1})):a.paging.refresh().then(_.partialRight(m,!0))}function y(e){return c.indexOf(e)}function p(e){return-1<y(e)}function F(){f=!f,a.recentFirstActive=f;var e=a.paging.statistics.totalPages,e=f?void 0:{currentPage:e};return a.refreshLoading=!0,h(e)}function T(e,t,i){var n={};e&&(n.dateFrom=moment(e).format(r.dateFullFormat)),t&&(n.dateTo=moment(t).format(r.dateFullFormat)),i?n.type=i:(delete n.type,delete a.filters.type),_.merge(a.filters,n)}function $(e){a.dateTo=void 0,a.dateFrom=void 0,l.dateTo=a.dateTo,l.dateFrom=a.dateFrom,a.filters.dateTo=void 0,a.filters.dateFrom=void 0,l.eventType=void 0,T(a.dateFrom,a.dateTo,e)}function L(e){a.filters.source||(a.title=d("Events"),a.subtitle=e?s.getString("{{qty}} loaded",{qty:e}):""),a.showPageTitles&&!a.filters.deviceId&&o.changeTitle({title:d("Events"),subtitle:e?s.getString("{{qty}} loaded",{qty:e}):""})}function w(e,t){var i;_.some(a.events,{id:t.id})||(t.dateTime=new Date(t.time),i=t.dateTime,a.filters.dateFrom&&i<moment(a.filters.dateFrom).startOf("day"))||a.filters.dateTo&&i>moment(a.filters.dateTo).endOf("day")||a.filters.type&&a.filters.type!==t.type||a.events.push(t)}function S(e,t){_.remove(a.events,{id:t.id})}function C(e,t){var i=_.findIndex(a.events,{id:t.id});a.events[i]=t}function E(){a.refreshLoading=!0,h(),L()}function O(){n.start(a.$id,a.realtimeChannel)}function b(){n.addListener(a.$id,a.realtimeChannel,"".concat(a.$id,"-subscribed"),E),n.addListener(a.$id,a.realtimeChannel,"CREATE",w),n.addListener(a.$id,a.realtimeChannel,"UPDATE",C),n.addListener(a.$id,a.realtimeChannel,"DELETE",S)}function k(){var e=a.filters.deviceId||a.filters.source;a.realtimeChannel="/eventsWithChildren/".concat(e||"*")}function D(){return r.checkIfModulesExist(["c8y.eventsBinary"])}a.datepickerFormat=e.getDateFormat({useUppercaseFormat:!1}),a.$watch(function(){return l.dateFrom},function(e){l.datePickerOptionsTo={minDate:e,showWeeks:!0}}),a.$watch(function(){return l.dateTo},function(e){l.datePickerOptionsFrom={maxDate:e,showWeeks:!0}}),a.$watch("refreshLoading",function(e){a.onLoadingStateChange({refreshLoading:e})}),l.subscriberId=a.$id,l.filters=a.filters,l.filter=T,l.clearFilter=$,l.showOverviewBreadcrumbs=!(a.filters&&a.filters.deviceId),a.isEventsBinaryModuleAvailable=D,a.displayEventsBinary=function(e){return D()&&e.c8y_IsBinary},a.refresh=v,a.loadNext=function(e){a.paging.loading=!0,e?(a.refreshLoading=!0,h({pageSize:e}).finally(function(){a.refreshLoading=!1})):(e=f?"next":"prev",a.paging[e]().then(m).finally(function(){a.paging.loading=!1}))},a.isOpen=p,a.toggle=function(e){p(e)?c.splice(y(e),1):c.push(e)},a.filters=a.filters||{},a.getStandardKeys=t.getStandardKeys,a.getNonStandardKeys=function(e){return _.without(t.getNonStandardKeys(e),"dateTime")},a.recentFirst=F,a.filter=T,a.clearFilter=$;var I={};a.absolute=function(e){var t=I[e.id];return t||(t=i("absoluteDate")(e.time),I[e.id]=t),t},a.$watch("realtime",function(){a.realtime!==n.getStatus(a.$id,a.realtimeChannel)&&n.switchRealtime(a.$id,a.realtimeChannel)}),a.$on("eventListRefresh",u),a.$on("$destroy",function(){n.stop(a.$id,a.realtimeChannel)}),a.$watch("filters",g,!0),k(),b(),a.initialRealtimeState=(e=[a.initialRealtimeState,a.realtime],_.head(_.reject(e,_.isUndefined))),a.initialRealtimeState&&(a.realtime=a.initialRealtimeState,O())}e.$inject=["$scope","$filter","c8yEvents","c8yBase","c8yRealtime","c8yTitle","gettext","gettextCatalog","c8yDateFormatService"],angular.module("c8y.ui").controller("eventList",e)})();