'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 _Actions = require('../Actions');

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var DISPLAY_MODES = {
	DROPDOWN: 'dropdown',
	SPLIT_DROPDOWN: 'splitDropdown',
	BTN_GROUP: 'btnGroup'
};

var actionsShape = {
	left: _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(_Actions.Action.propTypes), _react.PropTypes.shape(_Actions.ActionSplitDropdown.propTypes)])),
	right: _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(_Actions.Action.propTypes), _react.PropTypes.shape(_Actions.ActionSplitDropdown.propTypes)])),
	children: _react.PropTypes.node
};

function getRenderers(props) {
	return _extends({
		Action: _Actions.Action,
		Actions: _Actions.Actions,
		ActionDropdown: _Actions.ActionDropdown,
		ActionSplitDropdown: _Actions.ActionSplitDropdown
	}, props ? props.renderers : {});
}

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

	if (selected > 0) {
		return multiSelectActions || {};
	}
	return actions || {};
}

function getContentClassName(_ref2) {
	var _classNames;

	var tag = _ref2.tag,
	    className = _ref2.className,
	    left = _ref2.left,
	    right = _ref2.right;

	return (0, _classnames2.default)(className, (_classNames = {}, _defineProperty(_classNames, '' + _ActionBar2.default['navbar-left'], left), _defineProperty(_classNames, '' + _ActionBar2.default['navbar-right'], right), _defineProperty(_classNames, 'navbar-left', left), _defineProperty(_classNames, 'navbar-right', right), _defineProperty(_classNames, 'navbar-text', tag === 'p'), _defineProperty(_classNames, 'navbar-btn', tag === 'button'), _defineProperty(_classNames, 'navbar-form', tag === 'form'), _defineProperty(_classNames, 'navbar-link', tag === 'a'), _classNames));
}

function Content(_ref3) {
	var _ref3$tag = _ref3.tag,
	    tag = _ref3$tag === undefined ? 'div' : _ref3$tag,
	    left = _ref3.left,
	    right = _ref3.right,
	    className = _ref3.className,
	    children = _ref3.children,
	    rest = _objectWithoutProperties(_ref3, ['tag', 'left', 'right', 'className', 'children']);

	var props = _extends({
		className: getContentClassName({ tag: tag, left: left, right: right, className: className })
	}, rest);
	return _react2.default.createElement(tag, props, children);
}
Content.propTypes = {
	children: _react.PropTypes.node,
	className: _react.PropTypes.string,
	left: _react.PropTypes.bool,
	right: _react.PropTypes.bool,
	tag: _react.PropTypes.oneOf(['p', 'button', 'form', 'a', 'div'])
};

function SwitchActions(_ref4) {
	var actions = _ref4.actions,
	    left = _ref4.left,
	    right = _ref4.right,
	    selected = _ref4.selected,
	    renderers = _ref4.renderers;

	var Renderers = getRenderers({ renderers: renderers });
	return _react2.default.createElement(
		Content,
		{ left: left, right: right },
		selected > 0 && !right ? _react2.default.createElement(Count, { selected: selected }) : null,
		actions.map(function (action, index) {
			var displayMode = action.displayMode,
			    rest = _objectWithoutProperties(action, ['displayMode']);

			switch (displayMode) {
				case DISPLAY_MODES.DROPDOWN:
					return _react2.default.createElement(Renderers.ActionDropdown, _extends({ key: index }, rest));
				case DISPLAY_MODES.SPLIT_DROPDOWN:
					return _react2.default.createElement(Renderers.ActionSplitDropdown, _extends({ key: index }, rest));
				case DISPLAY_MODES.BTN_GROUP:
					return _react2.default.createElement(Renderers.Actions, _extends({ key: index }, rest));
				default:
					return _react2.default.createElement(Renderers.Action, _extends({ key: index }, rest));
			}
		})
	);
}
SwitchActions.propTypes = {
	actions: _react.PropTypes.arrayOf(_react.PropTypes.shape(actionsShape)).isRequired,
	left: _react.PropTypes.bool,
	right: _react.PropTypes.bool,
	selected: _react.PropTypes.number,
	renderers: _react.PropTypes.shape({
		Action: _react.PropTypes.func,
		Actions: _react.PropTypes.func,
		ActionSplitDropdown: _react.PropTypes.func
	})
};
SwitchActions.defaultProps = {
	actions: []
};

function Count(_ref5) {
	var selected = _ref5.selected;

	if (!selected) {
		return null;
	}
	return _react2.default.createElement(
		'span',
		{ className: (0, _classnames2.default)(_ActionBar2.default['tc-actionbar-selected-count'], 'tc-actionbar-selected-count') },
		selected,
		' selected'
	);
}
Count.propTypes = {
	selected: _react.PropTypes.number
};

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

	var cssClass = (0, _classnames2.default)(_ActionBar2.default['tc-actionbar-container'], 'tc-actionbar-container', 'nav', props.className);
	return _react2.default.createElement(
		'nav',
		{ className: cssClass },
		(left || !!props.selected) && _react2.default.createElement(SwitchActions, {
			renderers: props.renderers,
			key: 0,
			actions: left,
			selected: props.selected,
			left: true
		}),
		props.children,
		right && _react2.default.createElement(SwitchActions, {
			renderers: props.renderers,
			key: 1,
			actions: right,
			selected: props.selected,
			right: true
		})
	);
}

ActionBar.propTypes = {
	selected: _react.PropTypes.number,
	children: _react.PropTypes.node,
	className: _react.PropTypes.string,
	renderers: _react.PropTypes.shape(SwitchActions.propTypes.renderers)
};

ActionBar.displayName = 'ActionBar';
ActionBar.DISPLAY_MODES = DISPLAY_MODES;
ActionBar.Count = Count;
ActionBar.SwitchActions = SwitchActions;
ActionBar.getActionsToRender = getActionsToRender;
ActionBar.Content = Content;
ActionBar.getContentClassName = getContentClassName;
ActionBar.getRenderers = getRenderers;
exports.default = ActionBar;