'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Action = require('../Actions/Action');

var _Action2 = _interopRequireDefault(_Action);

var _ActionSplitDropdown = require('../Actions/ActionSplitDropdown');

var _ActionSplitDropdown2 = _interopRequireDefault(_ActionSplitDropdown);

var _ActionBar = require('./ActionBar.scss');

var _ActionBar2 = _interopRequireDefault(_ActionBar);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var TYPE_SPLIT_DROPDOWN = 'splitDropdown';

function ActionBar(_ref) {
	var selected = _ref.selected,
	    actions = _ref.actions,
	    multiSelectActions = _ref.multiSelectActions;

	var getActionsToRender = function getActionsToRender() {
		return selected > 0 ? multiSelectActions : actions;
	};

	var renderActions = function renderActions(actionsToRender) {
		return actionsToRender.map(function (action, index) {
			var displayMode = action.displayMode,
			    rest = _objectWithoutProperties(action, ['displayMode']);

			if (displayMode === TYPE_SPLIT_DROPDOWN) {
				return _react2.default.createElement(_ActionSplitDropdown2.default, _extends({ key: index }, rest));
			}

			return _react2.default.createElement(_Action2.default, _extends({ key: index }, rest));
		});
	};

	var renderSelectedCount = function renderSelectedCount() {
		return _react2.default.createElement(
			'span',
			{ className: (0, _classnames2.default)(_ActionBar2.default['tc-actionbar-selected-count'], 'tc-actionbar-selected-count') },
			selected,
			' selected'
		);
	};

	var renderActionBar = function renderActionBar() {
		var _getActionsToRender = getActionsToRender(),
		    left = _getActionsToRender.left,
		    right = _getActionsToRender.right;

		var actionBar = [];
		if (left) {
			actionBar.push(_react2.default.createElement(
				'div',
				{ key: 0, className: (0, _classnames2.default)(_ActionBar2.default['navbar-left'], 'navbar-left') },
				selected > 0 ? renderSelectedCount() : null,
				renderActions(left)
			));
		}
		if (right) {
			actionBar.push(_react2.default.createElement(
				'div',
				{ key: 1, className: (0, _classnames2.default)(_ActionBar2.default['navbar-right'], 'navbar-right') },
				selected > 0 && !left ? renderSelectedCount() : null,
				renderActions(right)
			));
		}
		return actionBar;
	};

	return _react2.default.createElement(
		'nav',
		{ className: (0, _classnames2.default)(_ActionBar2.default['tc-actionbar-container'], 'tc-actionbar-container', 'nav') },
		renderActionBar()
	);
}

var actionsShape = {
	left: _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(_Action2.default.propTypes), _react.PropTypes.shape(_ActionSplitDropdown2.default.propTypes)])),
	right: _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(_Action2.default.propTypes), _react.PropTypes.shape(_ActionSplitDropdown2.default.propTypes)]))
};

ActionBar.propTypes = {
	selected: _react.PropTypes.number,
	actions: _react.PropTypes.shape(actionsShape).isRequired,
	multiSelectActions: _react.PropTypes.shape(actionsShape)
};

exports.default = ActionBar;