var {EventEmitter} = require('fbemitter');
export var emitter = new EventEmitter();

class ImmutableQuery {
  constructor() {
    this.shouldArray = [];
    this.filterArray = [];
    this.config = [];
  }
  setConfig(config) {
    this.config = config.appbase;
  }
  addShouldClause(key, value) {
    this.shouldArray.push(this.getKeyValueObject(key, value));
    return this.buildQuery();
  }
  removeShouldClause(key, value) {
    var index = this.getShouldArrayIndex(key, value);
    this.shouldArray.splice(index, 1);
    return this.buildQuery();
  }
  buildQuery(geo) {
    this.query = {
      type: this.config.type,
      body: {
        "size": 100,
        "query": {
          "bool": {
            "must": this.shouldArray,
            "filter": this.filterArray
          }
        }
      }
    };
    if (!geo)
      emitter.emit('change', this.query);
    return this.query;
  }
  getKeyValueObject(key, value) {
    var term = JSON.parse(`{"${key}":` + JSON.stringify(value) + '}');
    return { term };
  }
  getShouldArrayIndex(key, value) {
    var array = this.shouldArray
    var encode64 = btoa(JSON.stringify(this.getKeyValueObject(key, value)));
    for (var i = 0; i < array.length; i++) {
      if (btoa(JSON.stringify(array[i])) === encode64) {
        return i;
      }
    }
    return -1;
  }
}

export const queryObject = new ImmutableQuery();
@@ -388,16 +388,255 @@
 ();%0A  %7D%0A
+  updateGeoFilter(key, boundingBoxCoordinates) %7B%0A    var geoObject = JSON.parse(%60%7B%22$%7Bkey%7D%22:%60 + JSON.stringify(boundingBoxCoordinates) + '%7D');%0A    this.filterArray%5B0%5D = %7B geo_bounding_box: geoObject %7D;%0A    return this.buildQuery(true);%0A  %7D%0A
   remove

Implement geo filter