var equal      = require('deep-equal'),
    gengisKhan = exports;

function $all(objectField, subQuery) {

}

var dispatchTable = {
  '$all': $all,
};

function parseSpecial(objectField, subQuery) {
  var matches = true;
  Object.keys(subQuery).forEach(function(subQueryKey) {
    if(dispatchTable[subQueryKey]) {
      if(!dispatchTable[subQueryKey](objectField, subQuery[subQueryKey]))
        matches = false;
      return;
    }
    throw new Error('Unknown symbol ' + subQueryKey);
  });

  return matches;
}

function filterWithQuery(query) {
  return function(object) {
    var matches = true;

    Object.keys(query).forEach(function(queryKey) {
      var queryValue = query[queryKey];

      if(typeof value === 'object') {
        if(!parseSpecial(object[queryKey], queryValue))
          matches = false;
        return;
      }

      if(!equal(query[queryKey], object[queryKey]))
        matches = false;
    });

    return matches;
  };
}

gengisKhan.match = function(objects, query) {
  if(typeof query === 'string') {
    /*
     * XXX: Yes, it's dumb. No, no way around it.
     *      Keep hatin' ◕ ◡ ◔
     */
    eval('query = ' + query);
  }

  return objects.map(filterWithQuery(query));
};


var equal      = require('deep-equal'),
    _          = require('lodash'),
    gengisKhan = exports;

function $all(objectField, subQuery) {

}

var dispatchTable = {
  '$all': $all,
};

function parseSpecial(objectField, subQuery) {
  return _.every(subQuery, function(subQueryValue, subQueryKey) {
    if(dispatchTable[subQueryKey])
      return dispatchTable[subQueryKey](objectField, subQuery[subQueryKey]);

    throw new Error('Unknown symbol ' + subQueryKey);
  });
}

function filterWithQuery(query) {
  return function(object) {
    var matches = true;

    return _.every(query, function(queryValue, queryKey) {

      if(typeof value === 'object') {
        return parseSpecial(object[queryKey], queryValue);
      }

      return equal(query[queryKey], object[queryKey]);
    });
  };
}

gengisKhan.match = function(objects, query) {
  if(typeof query === 'string') {
    /*
     * XXX: Yes, it's dumb. No, no way around it.
     *      Keep hatin' ◕ ◡ ◔
     */
    eval('query = ' + query);
  }

  return objects.map(filterWithQuery(query));
};


move to lodash