'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 _reactBootstrap = require('react-bootstrap');

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _Action2 = _interopRequireDefault(_Action);

var _ActionDropdown = require('./ActionDropdown');

var _ActionDropdown2 = _interopRequireDefault(_ActionDropdown);

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

var _ActionSplitDropdown2 = _interopRequireDefault(_ActionSplitDropdown);

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_DROPDOWN = 'dropdown';
var TYPE_SPLIT_DROPDOWN = 'splitDropdown';

function getButtonGroupProps(props) {
	var buttonGroupProps = {};
	Object.keys(_reactBootstrap.ButtonGroup.propTypes).forEach(function (id) {
		if (props[id] !== undefined) {
			buttonGroupProps[id] = props[id];
		}
	});
	return buttonGroupProps;
}

function getActionComponent(displayMode) {
	switch (displayMode) {
		case TYPE_DROPDOWN:
			return _ActionDropdown2.default;
		case TYPE_SPLIT_DROPDOWN:
			return _ActionSplitDropdown2.default;
		default:
			return _Action2.default;
	}
}

/**
 * @param {object} props react props
 * @example
const actions: [
    {
		label: 'edit',
		icon: 'fa fa-edit',
		onClick: action('onEdit'),
	},
    {
		label: 'delete',
		icon: 'fa fa-trash-o',
		onClick: action('onDelete'),
	},
    {
		displayMode: 'dropdown',
		label: 'related items',
		icon: 'fa fa-file-excel-o',
		items: [
			{
				label: 'document 1',
				onClick: action('document 1 click'),
			},
			{
				label: 'document 2',
				onClick: action('document 2 click'),
			},
		],
	},
	{
		id: 'split-dropdown-id',
		displayMode: 'splitDropdown',
		label: 'add file',
		onClick: action('onClick'),
		items: [
			{
				label: 'file 1',
				onClick: action('file 1 click'),
			},
			{
				label: 'file 2',
				onClick: action('file 2 click'),
			},
		],
	},
 ];
 <Actions actions={actions} tooltipPlacement="right" hideLabel link />
 */
function Actions(props) {
	var buttonGroupProps = getButtonGroupProps(props);

	return _react2.default.createElement(
		_reactBootstrap.ButtonGroup,
		_extends({
			className: (0, _classnames2.default)('tc-actions', props.className)
		}, buttonGroupProps),
		props.actions.map(function (action, index) {
			var displayMode = action.displayMode,
			    rest = _objectWithoutProperties(action, ['displayMode']);

			var ActionComponent = getActionComponent(displayMode);
			var params = _extends({
				key: index,
				hideLabel: props.hideLabel,
				link: props.link,
				tooltipPlacement: props.tooltipPlacement
			}, rest);

			return _react2.default.createElement(ActionComponent, params);
		})
	);
}

Actions.propTypes = _extends({
	actions: _react.PropTypes.arrayOf(_react.PropTypes.oneOfType([_react.PropTypes.shape(_Action2.default.propTypes), _react.PropTypes.shape({
		displayMode: _react.PropTypes.oneOf([TYPE_DROPDOWN, TYPE_SPLIT_DROPDOWN])
	})])),
	className: _react.PropTypes.string,
	hideLabel: _react.PropTypes.bool,
	tooltipPlacement: _Action2.default.propTypes.tooltipPlacement,
	link: _react.PropTypes.bool
}, _reactBootstrap.ButtonGroup.propTypes);

Actions.defaultProps = {
	actions: []
};

exports.default = Actions;