/*! grafana - v3.1.0 - 2016-07-18
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

System.register(["lodash","app/core/utils/kbn","app/plugins/sdk"],function(a){var b,c,d,e,f=this&&this.__extends||function(a,b){function c(){this.constructor=a}for(var d in b)b.hasOwnProperty(d)&&(a[d]=b[d]);a.prototype=null===b?Object.create(b):(c.prototype=b.prototype,new c)};return{setters:[function(a){b=a},function(a){c=a},function(a){d=a}],execute:function(){e=function(a){function d(b,c){var d=this;a.call(this,b,c),this.errors=this.validateTarget(),this.aggregators=["avg","sum","min","max","dev","zimsum","mimmin","mimmax"],this.fillPolicies=["none","nan","null","zero"],this.filterTypes=["wildcard","iliteral_or","not_iliteral_or","not_literal_or","iwildcard","literal_or","regexp"],this.tsdbVersion=this.datasource.tsdbVersion,this.target.aggregator||(this.target.aggregator="sum"),this.target.downsampleAggregator||(this.target.downsampleAggregator="avg"),this.target.downsampleFillPolicy||(this.target.downsampleFillPolicy="none"),this.datasource.getAggregators().then(function(a){0!==a.length&&(d.aggregators=a)}),this.datasource.getFilterTypes().then(function(a){0!==a.length&&(d.filterTypes=a)}),this.suggestMetrics=function(a,b){d.datasource.metricFindQuery("metrics("+a+")").then(d.getTextValues).then(b)},this.suggestTagKeys=function(a,b){d.datasource.suggestTagKeys(d.target.metric).then(b)},this.suggestTagValues=function(a,b){d.datasource.metricFindQuery("suggest_tagv("+a+")").then(d.getTextValues).then(b)}}return f(d,a),d.$inject=["$scope","$injector"],d.prototype.targetBlur=function(){this.errors=this.validateTarget(),this.refresh()},d.prototype.getTextValues=function(a){return b["default"].map(a,function(a){return a.text})},d.prototype.addTag=function(){return this.target.filters&&this.target.filters.length>0&&(this.errors.tags="Please remove filters to use tags, tags and filters are mutually exclusive."),this.addTagMode?(this.target.tags||(this.target.tags={}),this.errors=this.validateTarget(),this.errors.tags||(this.target.tags[this.target.currentTagKey]=this.target.currentTagValue,this.target.currentTagKey="",this.target.currentTagValue="",this.targetBlur()),void(this.addTagMode=!1)):void(this.addTagMode=!0)},d.prototype.removeTag=function(a){delete this.target.tags[a],this.targetBlur()},d.prototype.editTag=function(a,b){this.removeTag(a),this.target.currentTagKey=a,this.target.currentTagValue=b,this.addTag()},d.prototype.closeAddTagMode=function(){this.addTagMode=!1},d.prototype.addFilter=function(){if(this.target.tags&&b["default"].size(this.target.tags)>0&&(this.errors.filters="Please remove tags to use filters, tags and filters are mutually exclusive."),!this.addFilterMode)return void(this.addFilterMode=!0);if(this.target.filters||(this.target.filters=[]),this.target.currentFilterType||(this.target.currentFilterType="iliteral_or"),this.target.currentFilterGroupBy||(this.target.currentFilterGroupBy=!1),this.errors=this.validateTarget(),!this.errors.filters){var a={type:this.target.currentFilterType,tagk:this.target.currentFilterKey,filter:this.target.currentFilterValue,groupBy:this.target.currentFilterGroupBy};this.target.filters.push(a),this.target.currentFilterType="literal_or",this.target.currentFilterKey="",this.target.currentFilterValue="",this.target.currentFilterGroupBy=!1,this.targetBlur()}this.addFilterMode=!1},d.prototype.removeFilter=function(a){this.target.filters.splice(a,1),this.targetBlur()},d.prototype.editFilter=function(a,b){this.removeFilter(b),this.target.currentFilterKey=a.tagk,this.target.currentFilterValue=a.filter,this.target.currentFilterType=a.type,this.target.currentFilterGroupBy=a.groupBy,this.addFilter()},d.prototype.closeAddFilterMode=function(){this.addFilterMode=!1},d.prototype.validateTarget=function(){var a={};if(this.target.shouldDownsample)try{this.target.downsampleInterval?c["default"].describe_interval(this.target.downsampleInterval):a.downsampleInterval="You must supply a downsample interval (e.g. '1m' or '1h')."}catch(d){a.downsampleInterval=d.message}return this.target.tags&&b["default"].has(this.target.tags,this.target.currentTagKey)&&(a.tags="Duplicate tag key '"+this.target.currentTagKey+"'."),a},d.templateUrl="partials/query.editor.html",d}(d.QueryCtrl),a("OpenTsQueryCtrl",e)}}});