(function (global, factory) {
	if (typeof define === "function" && define.amd) {
		define(["exports", "./IronSelection"], factory);
	} else if (typeof exports !== "undefined") {
		factory(exports, require("./IronSelection"));
	} else {
		var mod = {
			exports: {}
		};
		factory(mod.exports, global.IronSelection);
		global.IronMultiSelectable = mod.exports;
	}
})(this, function (exports, _IronSelection2) {
	"use strict";

	Object.defineProperty(exports, "__esModule", {
		value: true
	});

	var _IronSelection3 = _interopRequireDefault(_IronSelection2);

	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 _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;
		};
	}();

	function _possibleConstructorReturn(self, call) {
		if (!self) {
			throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
		}

		return call && (typeof call === "object" || typeof call === "function") ? call : self;
	}

	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 IronMultiSelectable = function (_IronSelection) {
		_inherits(IronMultiSelectable, _IronSelection);

		function IronMultiSelectable(props) {
			_classCallCheck(this, IronMultiSelectable);

			var _this = _possibleConstructorReturn(this, (IronMultiSelectable.__proto__ || Object.getPrototypeOf(IronMultiSelectable)).call(this, props));

			_this.multi = props.multi || false;
			_this.selectedValues = [];
			_this.selectedItems = [];
			return _this;
		}

		_createClass(IronMultiSelectable, [{
			key: "push",
			value: function push(prop, value) {
				this[prop].push(value);
			}
		}, {
			key: "splice",
			value: function splice(prop, index, len) {
				this[prop].splice(index, len);
			}
		}, {
			key: "select",
			value: function select(value) {
				if (this.multi) {
					this._toggleSelected(value);
				} else {
					this.selected = value;
				}
			}
		}, {
			key: "_updateSelected",
			value: function _updateSelected() {
				if (this.multi) {
					this._selectMulti(this.selectedValues);
				} else {
					this._selectSelected(this.selected);
				}
			}
		}, {
			key: "_toggleSelected",
			value: function _toggleSelected(value) {
				var i = this.selectedValues.indexOf(value);
				var unselected = i < 0;
				if (unselected) {
					this.push("selectedValues", value);
				} else {
					this.splice("selectedValues", i, 1);
				}
			}
		}, {
			key: "_valuesToItems",
			value: function _valuesToItems(values) {
				return values == null ? null : values.map(function (value) {
					return this._valueToItem(value);
				}, this);
			}
		}, {
			key: "_selectMulti",
			value: function _selectMulti(values) {
				values = values || [];
				var selectedItems = (this._valuesToItems(values) || []).filter(function (item) {
					return item !== null && item !== undefined;
				});

				this._selection.clear(selectedItems);

				for (var i = 0; i < selectedItems.length; i++) {
					this._selection.setItemSelected(selectedItems[i], true);
				}

				if (this.fallbackSelection && !this._selection.get().length) {
					var fallback = this._valueToItem(this.fallbackSelection);
					if (fallback) {
						this.select(this.fallbackSelection);
					}
				}
			}
		}]);

		return IronMultiSelectable;
	}(_IronSelection3.default);

	exports.default = IronMultiSelectable;
});