"use strict";

exports.__esModule = true;
exports.default = undefined;

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _akutils = require("akutils");

var _lodash = require("lodash.isnumber");

var _lodash2 = _interopRequireDefault(_lodash);

var _lodash3 = require("lodash.result");

var _lodash4 = _interopRequireDefault(_lodash3);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var PojoQueryBuilder = function () {
	function PojoQueryBuilder() {
		_classCallCheck(this, PojoQueryBuilder);

		this.filter = [];
	}

	_createClass(PojoQueryBuilder, [{
		key: "toString",
		value: function toString() {
			return this.filter.reduce(function (acc, f) {
				return "" + acc + (acc.length ? ', ' : '') + f[0];
			}, "");
		}

		/*
  	Iterates the top-level key/value pairs and filters out any pairs where the value does not pass all
  	predicate tests.
  */

	}, {
		key: "on",
		value: function on(model) {
			var _this = this;

			return this.filter.reduce(function (acc, m, key) {
				var _iteratorNormalCompletion = true;
				var _didIteratorError = false;
				var _iteratorError = undefined;

				try {
					for (var _iterator = _this.filter[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
						var _step$value = _slicedToArray(_step.value, 2);

						var predFn = _step$value[1];

						if (!predFn(m)) {
							return acc;
						}
					}
				} catch (err) {
					_didIteratorError = true;
					_iteratorError = err;
				} finally {
					try {
						if (!_iteratorNormalCompletion && _iterator.return) {
							_iterator.return();
						}
					} finally {
						if (_didIteratorError) {
							throw _iteratorError;
						}
					}
				}

				acc.push(m);

				return acc;
			}, []);
		}

		/*
  	Adds a pameter name/value pair to the URL query string.
  */

	}, {
		key: "equals",
		value: function equals(name, value) {
			this.filter.push(["'" + name + "' == " + value, function (m) {
				return propertyValue(m, name) == value;
			}]);

			return this;
		}

		/*
  	Adds a pameter name/value pair to the URL query string of the form: name=gt(value).
  */

	}, {
		key: "gt",
		value: function gt(name, value) {
			(0, _akutils.assert)(function (a) {
				return a.is((0, _lodash2.default)(value), "gt() requires a value to be a number");
			});

			this.filter.push(["'" + name + "' > " + value, function (m) {
				return (0, _lodash4.default)(m, name) > value;
			}]);

			return this;
		}

		/*
  	Adds a pameter name/value pair to the URL query string of the form: name=gte(value).
  */

	}, {
		key: "gte",
		value: function gte(name, value) {
			(0, _akutils.assert)(function (a) {
				return a.is((0, _lodash2.default)(value), "gte() requires a value to be a number");
			});

			this.filter.push(["'" + name + "' >= " + value, function (m) {
				return (0, _lodash4.default)(m, name) >= value;
			}]);

			return this;
		}

		/*
  	Adds a pameter name/value pair to the URL query string of the form: name=lt(value).
  */

	}, {
		key: "lt",
		value: function lt(name, value) {
			(0, _akutils.assert)(function (a) {
				return a.is((0, _lodash2.default)(value), "lt() requires a value to be a number");
			});

			this.filter.push(["'" + name + "' < " + value, function (m) {
				return (0, _lodash4.default)(m, name) < value;
			}]);

			return this;
		}

		/*
  	Adds a pameter name/value pair to the URL query string of the form: name=lte(value).
  */

	}, {
		key: "lte",
		value: function lte(name, value) {
			(0, _akutils.assert)(function (a) {
				return a.is((0, _lodash2.default)(value), "lte() requires a value to be a number");
			});

			this.filter.push(["'" + name + "' <= " + value, function (m) {
				return (0, _lodash4.default)(m, name) <= value;
			}]);

			return this;
		}

		/*
  	Adds a pameter name/value pair to the URL query string of the form: name=op(value).
  */

	}, {
		key: "filterOp",
		value: function filterOp(name, op, value) {
			this.filter[name] = op + "(" + value + ")";

			return this;
		}
	}]);

	return PojoQueryBuilder;
}();

exports.default = PojoQueryBuilder;