/*! grafana - v4.2.0 - 2017-03-24
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

define(["lodash"],function(a){"use strict";function b(a,b){this.target=a,this.database=b}function c(a,b){var c="",d=a.operator,e=a.value;return b>0&&(c=(a.condition||"AND")+" "),d||(d=/^\/.*\/$/.test(a.value)?"=~":"="),"=~"!==d&&"!~"!==d&&isNaN(+e)&&(e="'"+e+"'"),c+'"'+a.key+'" '+d+" "+e}var d=b.prototype;return d.build=function(){return this.target.rawQuery?this._modifyRawQuery():this._buildQuery()},d.buildExploreQuery=function(b,d,e){var f,g;if("TAG_KEYS"===b)f="SHOW TAG KEYS",g=this.target.measurement;else if("TAG_VALUES"===b)f="SHOW TAG VALUES",g=this.target.measurement;else if("MEASUREMENTS"===b)f="SHOW MEASUREMENTS",e&&(f+=" WITH MEASUREMENT =~ /"+e+"/");else{if("FIELDS"===b)return this.target.measurement.match("^/.*/")?"SHOW FIELD KEYS FROM "+this.target.measurement:'SHOW FIELD KEYS FROM "'+this.target.measurement+'"';if("RETENTION POLICIES"===b)return f='SHOW RETENTION POLICIES on "'+this.database+'"'}if(g&&(g.match("^/.*/")||g.match(/^merge\(.*\)/)||(g='"'+g+'"'),f+=" FROM "+g),d&&(f+=' WITH KEY = "'+d+'"'),this.target.tags&&this.target.tags.length>0){var h=a.reduce(this.target.tags,function(a,b){return b.key===d?a:(a.push(c(b,a.length)),a)},[]);h.length>0&&(f+=" WHERE "+h.join(" "))}return f},b});