/**
 * @license
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var QueryParser = {
  __proto__: QueryParserFactory(QIssue),

  stars: seq(literal_ic('stars:'), sym('number')),

  labelMatch: seq(sym('string'), alt(':', '='), sym('valueList')),

  summary: plus(notChar(' '))

}.addActions({
  stars: function(v) {
    return GTE({
      f: function(i) { return i.stars; },
      partialEval: function() {return this;},
      toSQL: function() { return 'stars > ' + v[1]; },
      toMQL: function() { return 'stars > ' + v[1]; }
    }, v[1]);
  },

  labelMatch: function(v) {
    var or = OR();
    var values = v[2];
    for ( var i = 0 ; i < values.length ; i++ ) {
      or.args.push(CONTAINS_IC(QIssue.LABELS, v[0] + '-' + values[i]));
    }
    return or;
  },

  summary: function(v) {
    return CONTAINS_IC(QIssue.SUMMARY, v.join(''));
  }
});


QueryParser.expr = alt(
  QueryParser.export('expr'),
  sym('stars'),
  sym('labelMatch'),
  sym('summary')
);

/*
function test(query) {
  var res = QueryParser.parseString(query);
  console.log('query: ', query, ' -> ', res && res.toSQL());
}

test('priority=0');
test('priority=0,1,2');
test('priority:0');
test('1234567');
test('status:Assigned');
test('status:Assigned priority:0');
test('Iteration:29');
test('Type:Bug');
test('');
test('priority=0 or priority=1');
*/

// label:Priority-High = Priority:High
// blockeon:NNN
// blocking:NNN
// is:blocked
// Priority:High,Medium = Priority:High OR Priority:Medium
// is:starred
// stars: 3  at least three users have starred
// "contains text"
// has:attachment
// attachment:screenshot or attachment:png

// consider
//  limit:# support
//  format:table/grid/csv/xml/json
//  orderBy:((-)field)+

@@ -621,16 +621,846 @@
 nse.%0A */
+%0A%0Avar DefaultQuery = FOAM(%7B%0A   model_: 'Model',%0A%0A   extendsModel: 'UNARY',%0A%0A   name: 'DefaultQuery',%0A%0A   properties: %5B%0A      %7B%0A        name:  'arg1',%0A        preSet: function(value) %7B%0A          // Escape Regex escape characters%0A          this.pattern_ = new RegExp(value.replace(/%5B-%5C/%5C%5C%5E$*+?.()%7C%5B%5C%5D%7B%7D%5D/g, '%5C%5C$&'), 'i');%0A          return value.toLowerCase();%0A        %7D%0A      %7D%0A   %5D,%0A%0A   methods: %7B%0A      // No different that the non IC-case%0A      toSQL: function() %7B return this.arg1; %7D,%0A      toMQL: function() %7B return this.arg1; %7D,%0A%0A      f: function(obj) %7B%0A        if ( this.pattern_.test(obj.summary) ) return true;%0A        if ( obj.owner.indexOf(this.arg1) != -1 ) return true;%0A        for ( var i = 0 ; i %3C obj.cc.length ; i++ ) if ( obj.cc%5Bi%5D.indexOf(this.arg1) != -1 ) return true;%0A        return false;%0A      %7D%0A   %7D%0A%7D);%0A%0A
 %0Avar Que
@@ -2196,35 +2196,34 @@
 urn 
-CONTAINS_IC(QIssue.SUMMARY,
+DefaultQuery.create(%7Barg1:
  v.j
@@ -2229,16 +2229,17 @@
 join('')
+%7D
 );%0A  %7D%0A%7D

Fix for Issue#198 Add owner: and cc: search to default query.