'use strict';

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

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

/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| IBM Cognos Products: Content Explorer
 *| (C) Copyright IBM Corp. 2019, 2020
 *|
 *| US Government Users Restricted Rights - Use, duplication or disclosure
 *| restricted by GSA ADP Schedule Contract with IBM Corp.
 *+------------------------------------------------------------------------+
 */

define(['react', 'react-dom', 'ca-ui-toolkit', 'prop-types', '../../../../lib/@waca/dashboard-common/dist/ui/Toolbar', '../../../../lib/@waca/core-client/js/core-client/ui/KeyCodes'], function (React, ReactDOM, UI_TOOLKIT, PropTypes, Toolbar, KeyCodes) {
	var Container = UI_TOOLKIT.Container;

	var ToolbarSectionComponent = function (_React$Component) {
		_inherits(ToolbarSectionComponent, _React$Component);

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

			var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

			_this._setRef = _this._setRef.bind(_this);
			_this._toolbar = new Toolbar({ className: 'dockedToolbar toolbar', reactToolbar: true });
			_this._toolbarRendered = Promise.resolve();
			return _this;
		}

		ToolbarSectionComponent.prototype.componentDidMount = function componentDidMount() {
			this._callbacks = {
				state: this._updateState.bind(this),
				focus: this._focus.bind(this)
			};
			this.props.stateProvider.registerCallback(this.props.stateEvent, this._callbacks['state']);
			this.props.stateProvider.registerCallback(this.props.focusEvent, this._callbacks['focus']);
		};

		ToolbarSectionComponent.prototype.componentWillUnmount = function componentWillUnmount() {
			this.props.stateProvider.removeCallback(this.props.stateEvent, this._callbacks['state']);
			this.props.stateProvider.removeCallback(this.props.focusEvent, this._callbacks['focus']);
		};

		ToolbarSectionComponent.prototype._updateState = function _updateState(state) {
			var _this2 = this;

			this._toolbar.clearItems();
			this._toolbarRendered = this._toolbar.addItems(state.actions).then(function () {
				_this2._toolbar.ready();
			});
		};

		ToolbarSectionComponent.prototype._focus = function _focus() {
			var _this3 = this;

			this._initialFocus = document.activeElement;
			this._toolbarRendered.then(function () {
				_this3._toolbar.setFocus();
			});
		};

		ToolbarSectionComponent.prototype._onKeyDown = function _onKeyDown(e) {
			if (e.keyCode === KeyCodes.ESCAPE && this._initialFocus) {
				this._initialFocus.focus();
				this._initialFocus = null;
			}
		};

		ToolbarSectionComponent.prototype._setRef = function _setRef(element) {
			this[ToolbarSectionComponent.ROOT] = element;
			/* eslint-disable react/no-find-dom-node */
			var elementDOMNode = ReactDOM.findDOMNode(element);
			/* eslint-disable react/no-find-dom-node */
			this.props.toolbarSectionDOM.setNode(elementDOMNode);

			if (element) {
				elementDOMNode.appendChild(this._toolbar.el);
			} else if (this._toolbar.el.parentNode) {
				this._toolbar.el.parentNode.removeChild(this._toolbar.el);
			}
		};

		ToolbarSectionComponent.prototype.render = function render() {
			return React.createElement(Container, {
				className: ToolbarSectionComponent.ROOT + ' ' + this.props.className,
				ref: this._setRef,
				onKeyDown: this._onKeyDown.bind(this)
			});
		};

		return ToolbarSectionComponent;
	}(React.Component);

	ToolbarSectionComponent.ROOT = 'ToolbarSection';
	ToolbarSectionComponent.propTypes = {
		className: PropTypes.string,
		toolbarSectionDOM: PropTypes.shape({
			setNode: PropTypes.func
		}),
		stateProvider: PropTypes.shape({
			getState: PropTypes.func,
			registerCallback: PropTypes.func,
			removeCallback: PropTypes.func
		}),
		stateEvent: PropTypes.string,
		focusEvent: PropTypes.string
	};
	ToolbarSectionComponent.defaultProps = {
		focusEvent: 'null_focus'
	};


	return ToolbarSectionComponent;
});
//# sourceMappingURL=ToolbarSectionComponent.js.map