function _toConsumableArray(r){return _arrayWithoutHoles(r)||_iterableToArray(r)||_unsupportedIterableToArray(r)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(r,e){var t;if(r)return"string"==typeof r?_arrayLikeToArray(r,e):"Map"===(t="Object"===(t={}.toString.call(r).slice(8,-1))&&r.constructor?r.constructor.name:t)||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(r,e):void 0}function _iterableToArray(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _arrayWithoutHoles(r){if(Array.isArray(r))return _arrayLikeToArray(r)}function _arrayLikeToArray(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}angular.module("c8y.core").factory("c8yCounter",["$q","c8yBase","c8yRealtime",function(w,A,b){function n(r){var t,r=_.cloneDeep(r),n=this;if(this.properties=null,this.filterFn=null,this.dependencies=null,_.isArray(r)){if(!(r=>{var e=r.length,t=_.last(r);return _.isArray(t)?(n.dependencies=t,n.filterFn=r[e-2],n.properties=r.slice(0,e-2),_.isFunction(n.filterFn)&&_.every(n.properties,String)):_.isFunction(t)&&(n.dependencies=_.initial(r),n.filterFn=t,n.properties=n.dependencies,_.every(n.properties,String))})(r))throw new Error("property map is not valid")}else{if(!_.isString(r))throw new Error("parameter is invalid");t=r,n.properties=[t],n.filterFn=function(r,e){return _.isEqual(r,e[t])},n.dependencies=[t]}}function E(r,e){var t=this;this.pickList=null;this.queryParams=r,this.propertyMapList=e&&_.map(e,function(r){return new n(r)})||[],r=_.pickBy(r,function(r){return void 0!==r}),_.forEach(_.keys(r),function(e){_.some(t.propertyMapList,function(r){return _.some(r.properties,function(r){return r===e})})||t.propertyMapList.push(new n(e))}),(e=_.reduce(t.propertyMapList,function(e,r){return _.forEach(r.dependencies,function(r){e[r]=!0}),e},{})).id=!0,t.pickList=_.keys(e)}return n.prototype.matches=function(r,e){var t=_.cloneDeep(r),r=(_.forEach(this.properties,function(r){t[r]=t[r]||void 0}),_.values(_.pick(t,this.properties)));return r.push(e),this.filterFn.apply(this,_toConsumableArray(r))},E.prototype.matches=function(e){var t=this;return _.every(this.propertyMapList,function(r){return r.matches(t.queryParams,e)})},{Counter:function(e,t){if(!_.isFunction(e))throw new Error("list function must be provided");var n,i,o,a,s=this,u=!1,c=!1,p={pageSize:1},r=!1,f=!1,l={CREATE:function(r){c?v(r)&&n.unshift(m(r)):i+=1},UPDATE:function(r){{var e;c&&(-1===(e=_.findIndex(n,{id:r.id}))&&v(r)?n.unshift(m(r)):-1===e||v(r)||n.splice(e,1))}},DELETE:function(r){c?-1!==(r=_.findIndex(n,{id:r.id}))&&n.splice(r,1):--i}};function d(){var r;return t?(r=b.realtimeActions(),_.forEach(r,function(r){b.addListener(s.__realtimeId,t,r,h)}),w.when(b.start(s.__realtimeId,t))):w.resolve()}function h(r,e){var t;u||(t=s.count,l[r.name||r](e),y(),r=s.count,o&&r!==t&&o(r,t))}function y(){s.count=n&&n.length||i||0}function m(r){return _.pick(r,a.pickList)}function v(r){return a.matches(r)}A.createLocalId(this,"__realtimeId"),this.start=function(){if(r)throw new Error("Cannot restart a counter instance, please create a new instance");return r=!0,this.refresh().then(d)},this.stop=function(){if(!r)throw new Error("Cannot stop counter instance without starting it first");if(f)throw new Error("Cannot stop an already stopped counter instance");f=!0,t&&b.removeSubscriber(s.__realtimeId,t)},this.onNotification=function(r){o=r},this.refresh=function(){u=!0;var r=c?a.queryParams:p;return w.when(e(r)).then(function(r){c?n=_(r).filter(v).map(m).value():i=r.statistics.totalPages,y(),u=!1})},this.filter=function(r,e){if(r||e){if(c)throw new Error("filter can be set only once");c=!0,a=new E(r,e)}}},defaultPropertyMaps:{date:["dateFrom","dateTo",function(r,e,t){t=t&&(t.date||t.time);return!!t&&!(r&&moment(r).isAfter(t)||e&&moment(e).isBefore(t))},["date","time"]],source:["source",function(r,e){return(e&&e.source&&e.source.id)===r},["source"]]}}}]);