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

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

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

	var Breadcrumbs = function () {
		function Breadcrumbs(breadcrumbEl, graph, clickOnlyMode) {
			var breadcrumbs = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];

			_classCallCheck(this, Breadcrumbs);

			this.breadcrumbEl = breadcrumbEl;
			this.graph = graph;
			this.clickOnlyMode = clickOnlyMode;
			this.breadcrumbs = breadcrumbs;
			this.map = new WeakMap();
			this.ctx = this._createCanvas(breadcrumbEl);
			this._preShortenItems(this.breadcrumbs);
			return this;
		}

		_createClass(Breadcrumbs, [{
			key: '_addToWeakMap',
			value: function _addToWeakMap(item) {
				var cachedItem = this.map.get(item) || null;
				if (!cachedItem) {
					this.map.set(item, item);
				}
			}
		}, {
			key: '_preShortenItems',
			value: function _preShortenItems(items) {
				var _iteratorNormalCompletion = true;
				var _didIteratorError = false;
				var _iteratorError = undefined;

				try {
					for (var _iterator = items[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
						var item = _step.value;

						this._getShortenedText(item);
					}
				} catch (err) {
					_didIteratorError = true;
					_iteratorError = err;
				} finally {
					try {
						if (!_iteratorNormalCompletion && _iterator.return) {
							_iterator.return();
						}
					} finally {
						if (_didIteratorError) {
							throw _iteratorError;
						}
					}
				}
			}
		}, {
			key: '_getShortenedText',
			value: function _getShortenedText(item) {
				var cachedItem = this.map.get(item) || {};
				if (!cachedItem.shortText && item.label.length > 13) {
					cachedItem.shortText = item.label.substr(0, 6) + '...' + item.label.substr(item.label.length - 6);
				} else if (!cachedItem.shortText) {
					cachedItem.shortText = item.label;
				}
				this.map.set(item, cachedItem);
				return cachedItem.shortText;
			}
		}, {
			key: '_sizeOfIndividualFullItem',
			value: function _sizeOfIndividualFullItem(item) {
				var cachedItem = this.map.get(item) || {};
				cachedItem.fullSize = cachedItem.fullSize || parseInt(this.ctx.measureText(item.label).width, 10);
				this.map.set(item, cachedItem);
				return cachedItem.fullSize;
			}
		}, {
			key: '_sizeOfIndividualShortItem',
			value: function _sizeOfIndividualShortItem(item) {
				var cachedItem = this.map.get(item) || {};
				cachedItem.shortSize = cachedItem.shortSize || parseInt(this.ctx.measureText(cachedItem.shortText).width, 10);
				this.map.set(item, cachedItem);
				return cachedItem.shortSize;
			}
		}, {
			key: '_calculateSizeOfBreadcrumbs',
			value: function _calculateSizeOfBreadcrumbs(strArray) {
				var useFullSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

				if (strArray) {
					var accum = 0,
					    i = 0,
					    len = strArray.length,
					    sizeOfItem = void 0;

					for (i = 0; i < len; i++, sizeOfItem = null) {

						if (useFullSize) {
							sizeOfItem = this._sizeOfIndividualFullItem(strArray[i]);
						} else {
							sizeOfItem = this._sizeOfIndividualShortItem(strArray[i]);
						}
						var source = strArray[i].source ? strArray[i].source : strArray[i];

						accum += sizeOfItem;

						if (strArray[i].label !== "..." && this.graph.hasSiblings(source) && !this.clickOnlyMode) {
							accum += 21;
						}

						accum += 20;

						if (i !== len - 1) {
							accum += this.clickOnlyMode ? 30 : 15;
						}
					}
					return accum + 10;
				}
			}
		}, {
			key: '_createCanvas',
			value: function _createCanvas(breadcrumbEl) {
				var style = window.getComputedStyle(breadcrumbEl, null),
				    fontSize = style.getPropertyValue('font-size'),
				    fontFamily = style.getPropertyValue('font-family');

				var canvas = document.createElement('canvas');

				canvas.height = 20;
				canvas.width = 9999;

				var ctx = canvas.getContext('2d');
				ctx.font = fontSize + " " + fontFamily;
				return ctx;
			}
		}, {
			key: 'sizeOfFullBreadcrumbs',
			get: function get() {
				this.__sizeOfFullBreadcrumbs = this.__sizeOfFullBreadcrumbs || this._calculateSizeOfBreadcrumbs(this.breadcrumbs);
				return this.__sizeOfFullBreadcrumbs;
			}
		}, {
			key: 'sizeOfAllShortenedItemsExcludingLastItem',
			get: function get() {
				return this._calculateSizeOfBreadcrumbs(this.breadcrumbs.slice(0, this.breadcrumbs.length - 1), false);
			}
		}, {
			key: 'sizeOfFullLastItem',
			get: function get() {
				return this._calculateSizeOfBreadcrumbs(this.breadcrumbs.slice(-1));
			}
		}, {
			key: 'sizeOfShortLastItem',
			get: function get() {
				return this._calculateSizeOfBreadcrumbs(this.breadcrumbs.slice(-1), false);
			}
		}, {
			key: 'lastItemFull',
			get: function get() {
				return this.breadcrumbs.slice(-1)[0];
			}
		}, {
			key: 'lastItemShort',
			get: function get() {
				return this.shortenedItems.slice(-1)[0];
			}
		}, {
			key: 'shortenedItems',
			get: function get() {
				var _this = this;

				this.__shortenedItems = this.__shortenedItems || this.breadcrumbs.map(function (item) {
					var wrapper = {};
					wrapper.source = item;
					wrapper.isTruncated = true;
					wrapper.label = _this._getShortenedText(item);
					wrapper.children = item.children;
					wrapper.selectedItem = item.selectedItem;
					wrapper.hasChildren = item.hasChildren;
					return wrapper;
				});
				return this.__shortenedItems;
			}
		}, {
			key: 'sizeOfEllipsis',
			get: function get() {
				return 36;
			}
		}, {
			key: 'sizeOfAllShortenedItems',
			get: function get() {
				return this._calculateSizeOfBreadcrumbs(this.breadcrumbs, false);
			}
		}, {
			key: 'allShortenedItemsExcludingLast',
			get: function get() {
				return this.shortenedItems.slice(0, this.shortenedItems.length - 1);
			}
		}]);

		return Breadcrumbs;
	}();

	exports.default = Breadcrumbs;
});