(()=>{function t(o,t,a,n,e){var i,d,r,c,l,s,m=_.debounce(D,300,{leading:!1,trailing:!0}),u=12e4,f=!1,e=e("Realtime is active. Deactivate it to edit date and time.");function D(){d&&r.dateFrom&&r.dateTo&&(f=!0,o.onLoading({$loading:f}),d.loadData(r.dateFrom,r.dateTo,o.aggregation).then(p).finally(function(){f=!1,o.onLoading({$loading:f}),o.onDataChange({chart:d})}))}function g(t){d.realtimeMeasurement(t)&&(h(),p(),o.selectable())&&($(i),o.selected)&&d.setSelectPoint(o.selected)}function p(){i=moment()}function h(){!f&&moment().diff(i)>u&&D()}function $(t,e){var e=e||{},t=moment(t),a=moment.duration(moment(r.dateTo).diff(r.dateFrom)),a=moment(t).subtract(a),a=(r.dateFrom=a.toDate(),r.dateTo=t.toDate(),r),t=a.dateFrom;e.notrigger&&(s=!0),d.axisX.updateDates(t,a.dateTo,!1),e.loadData&&D()}var T={dateFrom:!1,dateTo:!1,aggregation:!1,datapoints:!1};function v(t){t&&(T[t]=!0),_.every(T,function(t){return!0===t})&&m()}function w(){h(),moment(d.axisX._to).isBefore(moment())&&!d.allowSelectFocusTime&&$(moment(),{notrigger:1})}function F(){return _.map(o.datapoints,function(t,e){return{id:e,name:t.label,datapoint:t,datapointActive:t.__active}})}function S(t){o.seriesSelected=t;t=_.map(o.seriesSelected,"datapoint");d.updateSeries(t,!0)}t.$on("c8yRealtime::connected",function(){D()}),this.init=function(t){d=t,o.chart=d,r={},o.internalDates=r,l=n(w),d.axisX.onUpdateDates=function(t,e,a){c=a,r.dateFrom=t,r.dateTo=e},o.$watch("dateFrom",function(t){moment(o.internalDates.dateFrom).isSame(t)||(o.internalDates.dateFrom=new Date(t),d.axisX.updateDates(t,o.dateTo,!1),v("dateFrom"))}),o.$watch("dateTo",function(t){moment(o.internalDates.dateTo).isSame(t)||(o.internalDates.dateTo=new Date(t),d.axisX.updateDates(o.dateFrom,t,!1),v("dateTo"))}),o.$watch("internalDates",function(t,e){var a=t.dateFrom&&moment(t.dateFrom).toDate(),n=t.dateTo&&moment(t.dateTo).toDate();o.datesValid=a&&n&&a<=n,o.datesValid&&(o.onUpdateDisplayedDates&&o.onUpdateDisplayedDates({$dateFrom:a,$dateTo:n}),s?s=!1:t!==e&&o.onUpdateDates&&(o.onUpdateDates({$dateFrom:a,$dateTo:n,$updateTrigger:c}),c=null,d.axisX.updateDates(a,n,!1),o.realtime&&new Date(e.dateTo).valueOf()-new Date(e.dateFrom).valueOf()==n.valueOf()-a.valueOf()||m()))},!0),o.$watch("aggregation",function(){return v("aggregation")}),o.$watch("datapoints",function(){o.seriesAvailable=F(),S(o.seriesSelected?_.filter(o.seriesAvailable,"datapointActive"):(()=>{var t=_.filter(F(),"datapointActive"),e=o.datapointsInitialDisplayLimit;return _.isUndefined(e)?t:_.take(t,e)})())},!0),o.$watchCollection("datapoints",function(){return v("datapoints")}),o.$watch("realtime",function(t){!t||o.selectable()?l.cancel():($(moment(),{notrigger:1,loadData:1}),l.start())}),o.$watch("selected",function(t){t?d.setSelectPoint(t):d.clearSelectPoint()}),d.onSelectPoint=function(t){a(function(){o.selected=t})},o.$watch(function(){return o.selectable()},function(t){(d.allowSelectFocusTime=t)&&l.cancel()}),o.$watch("chart.chartBox",function(t,e){_.isEqual(t,e)||o.onBoxChanged({box:t})});var e=_.throttle(d.render.bind(d),500);o.$on("dashboardResize",function(){return e()}),o.$watch("showTime()",function(t){t?d.showDateSelection():d.hideDateSelection()}),o.onData=g,o.$on("$destroy",function(){return l.cancel()}),o.onSeriesSelectionChanged=S},o.data={},o.realtimeActiveInfo=e}t.$inject=["$scope","$rootScope","$timeout","RTAutoMove","gettext"],angular.module("c8y.core.measurements2").controller("c8yChartCtrl",t)})();