/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","lodash","app/core/utils/kbn"],function(a,b,c){"use strict";var d=a.module("grafana.services");d.service("templateSrv",function(){function a(a){return a.replace(/([\!\*\+\-\=<>\s\&\|\(\)\[\]\{\}\^\~\?\:\\\/"])/g,"\\$1")}var d=this;this._regex=/\$(\w+)|\[\[([\s\S]+?)\]\]/g,this._index={},this._texts={},this._grafanaVariables={},this._builtIns={},this._builtIns.__interval={text:"1s",value:"1s"},this._builtIns.__interval_ms={text:"100",value:"100"},this.init=function(a){this.variables=a,this.updateTemplateData()},this.updateTemplateData=function(){this._index={},this._filters={};for(var a=0;a<this.variables.length;a++){var b=this.variables[a];b.current&&(b.current.isNone||b.current.value)&&(this._index[b.name]=b)}},this.variableInitialized=function(a){this._index[a.name]=a},this.getAdhocFilters=function(a){for(var b=[],c=0;c<this.variables.length;c++){var d=this.variables[c];"adhoc"===d.type&&(d.datasource===a&&(b=b.concat(d.filters)),0===d.datasource.indexOf("$")&&this.replace(d.datasource)===a&&(b=b.concat(d.filters)))}return b},this.luceneFormat=function(c){if("string"==typeof c)return a(c);var d=b.map(c,function(b){return'"'+a(b)+'"'});return"("+d.join(" OR ")+")"},this.formatValue=function(a,d,e){if(e=e||{},"function"==typeof d)return d(a,e,this.formatValue);switch(d){case"regex":if("string"==typeof a)return c.regexEscape(a);var f=b.map(a,c.regexEscape);return"("+f.join("|")+")";case"lucene":return this.luceneFormat(a,d,e);case"pipe":return"string"==typeof a?a:a.join("|");case"distributed":return"string"==typeof a?a:this.distributeVariable(a,e.name);default:return b.isArray(a)?"{"+a.join(",")+"}":a}},this.setGrafanaVariable=function(a,b){this._grafanaVariables[a]=b},this.getVariableName=function(a){this._regex.lastIndex=0;var b=this._regex.exec(a);return b?b[1]||b[2]:null},this.variableExists=function(a){var b=this.getVariableName(a);return b&&void 0!==d._index[b]},this.highlightVariablesAsHtml=function(a){return a&&b.isString(a)?(a=b.escape(a),this._regex.lastIndex=0,a.replace(this._regex,function(a,b,c){return d._index[b||c]||d._builtIns[b||c]?'<span class="template-variable">'+a+"</span>":a})):a},this.getAllValue=function(a){if(a.allValue)return a.allValue;for(var b=[],c=1;c<a.options.length;c++)b.push(a.options[c].value);return b},this.replace=function(a,b,c){if(!a)return a;var e,f,g;return this._regex.lastIndex=0,a.replace(this._regex,function(a,h,i){if(e=d._index[h||i],b&&(g=b[h||i]))return d.formatValue(g.value,c,e);if(!e)return a;if(f=d._grafanaVariables[e.current.value])return d.formatValue(f,c,e);if(g=e.current.value,d.isAllValue(g)&&(g=d.getAllValue(e),e.allValue))return g;var j=d.formatValue(g,c,e);return j})},this.isAllValue=function(a){return"$__all"===a||Array.isArray(a)&&"$__all"===a[0]},this.replaceWithText=function(a,b){if(!a)return a;var c;return this._regex.lastIndex=0,a.replace(this._regex,function(a,e,f){if(b){var g=b[e||f];if(g)return g.text}return c=d._index[e||f],c?d._grafanaVariables[c.current.value]||c.current.text:a})},this.fillVariableValuesForUrl=function(a,c){b.each(this.variables,function(b){c&&void 0!==c[b.name]?a["var-"+b.name]=c[b.name].value:a["var-"+b.name]=b.getValueForUrl()})},this.distributeVariable=function(a,c){return a=b.map(a,function(a,b){return 0!==b?c+"="+a:a}),a.join(",")}})});