"use strict";

exports.__esModule = true;

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

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var discrimS = Symbol();
var discrimCacheS = Symbol();

var Cache = (function (_Array) {
	_inherits(Cache, _Array);

	function Cache(discrim, limit) {
		_classCallCheck(this, Cache);

		_Array.call(this);
		this[discrimS] = discrim || "id";
		this[discrimCacheS] = {};
		this.limit = limit;
	}

	Cache.prototype.get = function get(key, value) {
		if (typeof key === 'function') {
			var valid = key;
			key = null;
		} else if (key && !value) {
			return this[discrimCacheS][key] || null;
		} else if (key === this[discrimS] && typeof value === "string") {
			return this[discrimCacheS][value] || null;
		} else if (value && value.constructor.name === 'RegExp') {
			var valid = function valid(item) {
				return value.test(item);
			};
		} else if (typeof value !== 'function') {
			var valid = function valid(item) {
				return item == value;
			};
		}

		for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
			var _ref;

			if (_isArray) {
				if (_i >= _iterator.length) break;
				_ref = _iterator[_i++];
			} else {
				_i = _iterator.next();
				if (_i.done) break;
				_ref = _i.value;
			}

			var item = _ref;

			if (valid(key == null ? item : item[key])) {
				return item;
			}
		}

		return null;
	};

	Cache.prototype.has = function has(key, value) {
		return !!this.get(key, value);
	};

	Cache.prototype.getAll = function getAll(key, value) {
		var found = new Cache(this[discrimS]);

		if (typeof key === 'function') {
			var valid = key;
			key = null;
		} else if (value && value.constructor.name === 'RegExp') {
			var valid = function valid(item) {
				return value.test(item);
			};
		} else if (typeof value !== 'function') {
			var valid = function valid(item) {
				return item == value;
			};
		}

		for (var _iterator2 = this, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
			var _ref2;

			if (_isArray2) {
				if (_i2 >= _iterator2.length) break;
				_ref2 = _iterator2[_i2++];
			} else {
				_i2 = _iterator2.next();
				if (_i2.done) break;
				_ref2 = _i2.value;
			}

			var item = _ref2;

			if (valid(key == null ? item : item[key])) {
				found.add(item);
			}
		}

		return found;
	};

	Cache.prototype.add = function add(data) {
		var cacheKey = data[this[discrimS]];
		if (this[discrimCacheS][cacheKey]) {
			return this[discrimCacheS][cacheKey];
		}
		if (this.limit && this.length >= this.limit) {
			this.splice(0, 1);
		}
		this.push(data);
		this[discrimCacheS][cacheKey] = data;
		return data;
	};

	Cache.prototype.update = function update(old, data) {
		var obj = this[discrimCacheS][old[this[discrimS]]];
		if (obj) {
			for (var key in data) {
				if (data.hasOwnProperty(key)) {
					obj[key] = data[key];
				}
			}
			return obj;
		}
		return false;
	};

	Cache.prototype.random = function random() {
		return this[Math.floor(Math.random() * this.length)];
	};

	Cache.prototype.remove = function remove(data) {
		if (!this[discrimCacheS][data[this[discrimS]]]) return false;

		delete this[discrimCacheS][data[this[discrimS]]];
		for (var i in this) {
			if (this[i] && this[i][this[discrimS]] === data[this[discrimS]]) {
				this.splice(i, 1);
				return true;
			}
		}
		return false;
	};

	return Cache;
})(Array);

exports["default"] = Cache;
module.exports = exports["default"];
