'use strict';
/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: Dashboard (C) Copyright IBM Corp. 2016, 2020
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['jquery', 'underscore', '../DynamicFileLoader', '../lib/@waca/core-client/js/core-client/ui/core/View' //TODO change to non lib version
], function ($, _, DynamicFileLoader, View) {
	/**
  * Creates an application bar to serve as a main menu bar
  */
	var Toolbar = View.extend({
		moduleMap: [],
		addItemsReady: null,
		init: function init(options) {
			Toolbar.inherited('init', this, arguments);
			this.options = !this.options ? {} : this.options;
			_.extend(this.options, options);
			this.items = options.items;
			this.itemMap = {};
			this._readyArray = [];
			this._reactToolbarContainer = null;
			this._isReactToolbar = this._canRenderInReactToolbar(options.items);
		},
		/**
   * Adds an item to the toolbar
   *
   * Returns a deferred object that will be resolve once the rendered item is added to the toolbar
   */
		addItem: function addItem(itemSpec) {
			return this._addItem(itemSpec);
		},
		_addItem: function _addItem(itemSpec) {
			// Need to add the item to the bar before requiring a module
			var $item = $('
');
			this.$el.append($item);
			return this._renderItem(itemSpec, $item);
		},
		_renderItem: function _renderItem(itemSpec, $item) {
			if (!itemSpec.type && !itemSpec.module) {
				itemSpec.type = 'Menu';
			}
			var module = itemSpec.type ? 'ui/toolbar_components/' + itemSpec.type : itemSpec.module;
			return this._renderItemHelper(module, itemSpec, $item);
		},
		_renderItemHelper: function _renderItemHelper(module, itemSpec, $item) {
			var _this = this;
			var promise = DynamicFileLoader.load([module]).then(function (modules) {
				var Item = modules[0];
				itemSpec.el = $item;
				var item = new Item(itemSpec, _this.rootMenu || _this.parentView, _this);
				_this.itemMap[item.name] = item;
				return Promise.resolve().then(item.render.bind(item));
			});
			this._readyArray.push(promise);
			return promise;
		},
		/**
   * Removes an item from the toolbar
   */
		removeItem: function removeItem(itemName) {
			this.itemMap[itemName].remove();
			delete this.itemMap[itemName];
		},
		/**
   * If any item is of type SubView, we want to create the toolbar in the old way.
   * React toolbar does not support SubViews
   */
		_canRenderInReactToolbar: function _canRenderInReactToolbar() {
			var itemSpecs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
			var isSupportedInReactToolbar = function isSupportedInReactToolbar(spec) {
				return spec.type !== 'SubView';
			};
			return this.options.reactToolbar && itemSpecs.every(isSupportedInReactToolbar);
		},
		/**
   * Add an array of items to the toolbar
   * @param itemSpecs
   *
   * Returns a deferred object that will be resolved when all items are rendered and added to the toolbar
   */
		addItems: function addItems() {
			var _this2 = this;
			var itemSpecs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
			this._isReactToolbar = this._canRenderInReactToolbar(itemSpecs);
			if (this._isReactToolbar) {
				this.addItemsReady = DynamicFileLoader.load(['ui/react_toolbar/ReactToolbarContainer']).then(function (modules) {
					var ReactToolbarContainer = modules[0];
					_this2._reactToolbarContainer = new ReactToolbarContainer(itemSpecs, _this2.el, _this2);
					_this2.itemMap = itemSpecs; // TODO: consumers are expecting an item name:view map and not an array of specs
				});
			} else {
				this.addItemsReady = Promise.all(itemSpecs.map(function (itemSpec) {
					return _this2._addItem(itemSpec);
				}));
			}
			return this.addItemsReady;
		},
		clearItems: function clearItems() {
			_.each(this.itemMap, function (item) {
				if (item.remove) {
					item.remove();
				}
			});
			if (this._isReactToolbar) {
				if (this._reactToolbarContainer) {
					this._reactToolbarContainer.clearItems();
					this._reactToolbarContainer = null;
				}
			} else {
				this.$el.empty();
			}
			this.itemMap = {};
			this._readyArray = [];
		},
		/**
   * Draws the Toolbar
   */
		render: function render() {
			this.$el.empty();
			if (this.items) {
				return this.addItems(this.items);
			}
			return Promise.resolve();
		},
		ready: function ready() {
			var _this3 = this;
			if (this._isReactToolbar) {
				return this.addItemsReady.then(function () {
					var orientation = _this3.options.preferredVertical ? 'vertical' : 'horizontal';
					orientation = _this3.placement === 'top' ? 'horizontal' : orientation;
					return _this3._reactToolbarContainer.ready(orientation);
				});
			} else {
				return Promise.all(this._readyArray);
			}
		},
		setFocus: function setFocus() {
			var first = Object.keys(this.itemMap)[0];
			if (first) {
				var focusElement = this.itemMap[first];
				if (focusElement && focusElement.setFocus) {
					focusElement.setFocus();
				}
			}
		},
		updateButtons: function updateButtons() {
			if (this.itemMap) {
				_.each(this.itemMap, function (item) {
					if (item.update) {
						item.render();
					}
				});
			}
		},
		remove: function remove() {
			this.clearItems();
			Toolbar.inherited('remove', this, arguments);
		}
	});
	return Toolbar;
});
//# sourceMappingURL=Toolbar.js.map