// Environment agnostic search query handler
// Ensures that provided values are coherent,
// and if all is fine produces normalized query objects

'use strict';

var ensureArray      = require('es5-ext/array/valid-array')
  , customError      = require('es5-ext/error/custom')
  , ensureObject     = require('es5-ext/object/valid-object')
  , ensureCallable   = require('es5-ext/object/valid-callable')
  , ensureString     = require('es5-ext/object/validate-stringifiable-value')
  , d                = require('d')
  , ee               = require('event-emitter')

  , create = Object.create;

var QueryHandler = module.exports = function (handlersList) {
	var takenNames = create(null);
	this._handlers = ensureArray(handlersList).map(function (conf) {
		var handler = {};
		ensureObject(conf);
		handler.name = ensureString(conf.name);
		if (!handler.name) throw new Error("Query name must not be empty");
		if (takenNames[handler.name]) throw new Error("Query name must be unique");
		takenNames[handler.name] = true;
		if (conf.ensure != null) handler.ensure = ensureCallable(conf.ensure);
		if (conf.resolve != null) handler.resolve = ensureCallable(conf.resolve);
		return handler;
	});
};

ee(Object.defineProperties(QueryHandler.prototype, {
	_resolveQueryValue: d(function (handler, value) {
		if (value != null) {
			if (value !== value.trim()) {
				throw customError("Query value with edge whitespace",
					{ queryHandler: handler, fixedQueryValue: value.trim() || null });
			}
			if (!value) throw customError("Empty value", { queryHandler: handler });
			if (handler.ensure) {
				try {
					handler.ensure.call(this, value);
				} catch (e) {
					e.queryHandler = handler;
					throw e;
				}
			}
		}
		return handler.resolve ? handler.resolve.call(this, value) : value;
	}),
	resolve: d(function (query) {
		return this._handlers.reduce(function (resolvedQuery, handler) {
			var value = this._resolveQueryValue(handler, query[handler.name]);
			if (value != null) resolvedQuery[handler.name] = value;
			return resolvedQuery;
		}.bind(this), create(null));
	})
}));

@@ -1093,84 +1093,8 @@
 e);%0A
-%09%09if (conf.resolve != null) handler.resolve = ensureCallable(conf.resolve);%0A
 %09%09re
@@ -1484,24 +1484,27 @@
 andler %7D);%0A%09
+%09%7D%0A
 %09%09if (handle
@@ -1518,17 +1518,16 @@
 e) %7B%0A%09%09%09
-%09
 try %7B%0A%09%09
@@ -1528,17 +1528,24 @@
 y %7B%0A%09%09%09%09
-%09
+value = 
 handler.
@@ -1573,17 +1573,16 @@
 ue);%0A%09%09%09
-%09
 %7D catch 
@@ -1587,17 +1587,16 @@
 h (e) %7B%0A
-%09
 %09%09%09%09e.qu
@@ -1621,17 +1621,16 @@
 er;%0A%09%09%09%09
-%09
 throw e;
@@ -1637,28 +1637,22 @@
 %0A%09%09%09
-%09
 %7D%0A
-%09
 %09%09%7D%0A
-%09%09%7D%0A
 %09%09return
  han
@@ -1651,62 +1651,8 @@
 turn
- handler.resolve ? handler.resolve.call(this, value) :
  val

Remove need for `resolve`