angular.module("c8y.core.measurements2").directive("c8yTimeInterval",["INTERVAL_CONSTANTS",function(l){function s(t){return t.selInterval===l.custom}function d(t){var e=t.selInterval;return e&&e.unit?(t.dateTo=moment().add(1,"seconds").toDate(),t.dateFrom=moment().subtract(e.qty,e.unit),-1===["hours","minutes"].indexOf(e.unit)&&t.dateFrom.startOf("day"),t.dateFrom=t.dateFrom.toDate()):(t.dateFrom=t.dateFrom&&moment(t.dateFrom).startOf("day").toDate(),t.dateTo=t.dateTo&&moment(t.dateTo).endOf("day").toDate()),!!e.unit}return{scope:{dateFrom:"=dateFrom",dateTo:"=dateTo",hideSelector:"=?",interval:"=?",intervalDescription:"=?",onDatepickerOpen:"&"},restrict:"EA",templateUrl:":::PLUGIN_PATH:::/views/timeInterval.html",controller:["$scope","$timeout",function(e,n){var a,r=_.bind(e.$emit,e,"search"),i=_.bind(e.$emit,e,"changeFilters");function t(t){e.interval=e.selInterval&&e.selInterval.unit,n(i,0),(d(e)||t)&&n(r,0)}function o(t){e.onDatepickerOpen({isOpen:t})}a=n(function(){e.selInterval=_.find(l.intervals,{unit:_.isUndefined(e.interval)?"hours":e.interval}),e.intervalDescription=e.selInterval.txt,t()},300),e.timeIntervals=l.intervals,e.isCustom=_.partial(s,e),e.onChange=t,e.search=t,e.$on("updateInterval",_.partial(d,e)),e.$watch("interval",function(t){_.isUndefined(t)||(a&&(n.cancel(a),a=null),e.selInterval=_.find(l.intervals,{unit:t}),e.intervalDescription=e.selInterval.txt,d(e))}),e.$watch("openFrom",o),e.$watch("openTo",o)}]}}]);