(()=>{function t(n,a,e,r,i,u){function c(t){return _.map(t.matchKpi,function(t){return t.c8y_Kpi})}function o(t){return o.cache[t]=o.cache[t]||e.listKpiForDevice(t).then(c),o.cache[t].catch(function(t){var e=t.data||{},e=u.getString(e.message);return i.add({type:"danger",text:e,detailedData:t}),n.reject(t)})}function s(t){var e=t.values,a=_.map(t.series,function(t){return t.fragment=t.type,t.values=[],t.timelist=[],t.dataPoints=[],t}),r=[];return _.forEach(e,function(n,t){var i=moment(t).toDate();r.push(i),_.forEach(a,function(t,e){e=n[e];!_.isObjectLike(e)||_.isUndefined(e.min)||_.isUndefined(e.max)||(t.values.push(e),t.timelist.push(i),t.dataPoints.push({val:e,time:i}))})}),a=_.filter(a,function(t){return t.values.length}),(a=_.map(a,f)).truncated=t.truncated||!1,a}function f(t){return _.assign(t,{getNearIndex:function(t){return t=t,e=(e=this).timelist,n=d3.bisect(e,t,1,e.length-1),i=e[n-1],e=e[n],a=n,_.isUndefined(i)||(_.isUndefined(e)||t-i<e-t)&&(a=n-1),a;var e,n,i,a}})}function m(t,e,n){return _.find(n,{fragment:t,series:e})}return o.cache={},{getKpi:o,getSeries:function(t,u,e,n,i){return t={source:t,dateFrom:e&&moment(e).format(r.dateFullFormat),dateTo:n&&moment(n).format(r.dateFullFormat),aggregationType:"NONE"!==i?i:void 0},a.listSeries(t).then(s).then(function(t){var e,n=_.isString(u),i=_.isArray(u),a=t.truncated,r=t;return n?r=_.filter(t,{type:u}):i&&(e=u,r=_.filter(t,function(t){return _.find(e,{fragment:t.fragment,series:t.name})}),_.forEach(r,function(t){t.kpi=m(t.type,t.name,e)})),r.truncated=a,r})},getMaxTimeFromSeries:function(t){return d3.max(_.flattenDeep(_.map(t,"timelist")))},getKpiOfSerie:function(t,e){return m(t.fragment,t.name,e)},appendMeasurement:function(t,e,n){var r=_.isString(t),u=r?[t]:_.uniq(_.map(n,"fragment")),c=!1,o=_.isArray(e)?e:[e];return _.forEach(o,function(t){var i=moment(t.time).toDate(),a=_.flattenDeep(_.map(u,function(t){return a=t,t=o,r=_.reduce(t,function(n,t){return _.forEach(t[a],function(t,e){n[e]||(n[e]={unit:t.unit}),n[e].unit||(n[e].unit=t.unit)}),n},{}),e=_.keys(r),u=[],c=[],_.forEach(t,function(t){var n=t[a],i=moment(t.time).toDate();_.forEach(e,function(t,e){u[e]=u[e]||{fragment:a,name:t,unit:r[t].unit,values:[],timelist:[],dataPoints:[]};e=u[e];n[t]&&(t=n[t].value,e.values.push(t={min:t,max:t}),e.timelist.push(i),e.dataPoints.push({time:i,val:t}))}),c.push(i)}),u;var a,r,e,u,c}));c=c||a.length,_.forEach(n,function(t){var e,n=_.find(a,{fragment:t.fragment,name:t.name});n&&(e=d3.bisect(t.timelist,i),t.timelist.splice(e,0,i),t.values.splice(e,0,_.head(n.values)),t.dataPoints.splice(e,0,_.head(n.dataPoints)),n.found=!0)}),r&&_.forEach(_.filter(a,function(t){return!t.found}),function(t){n.push(t)})}),{hasNew:c,series:n}},getDOMId:function(t,e){return e=[t.kpi&&t.kpi.__target?"target".concat(t.kpi.__target.id):"",t.fragment,e,t.name].join(""),"id".concat(window.btoa(e).replace(/[/=]/gi,"_"))}}}t.$inject=["$q","c8yMeasurements","c8yKpi","c8yBase","c8yAlert","gettextCatalog"],angular.module("c8y.core.measurements2").service("c8yLineChartSvc",t)})();