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

var _TooltipTrigger2 = _interopRequireDefault(_TooltipTrigger);

var _Icon = require('../../Icon');

var _Icon2 = _interopRequireDefault(_Icon);

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 getMenuItem(item, index) {
	if (item.divider) {
		return _react2.default.createElement(_reactBootstrap.MenuItem, { key: index, divider: true });
	}
	return _react2.default.createElement(
		_reactBootstrap.MenuItem,
		_extends({ key: index, eventKey: item }, item),
		item.icon && _react2.default.createElement(_Icon2.default, { name: item.icon }),
		item.label
	);
}

/**
 * @param {object} props react props
 * @example
 const props = {
	label: 'related items',
	icon: 'fa fa-file-excel-o',
	items: [
		{
			icon: 'talend-icon',
			label: 'document 1',
			onClick: action('document 1 click'),
		},
		{
			divider: true,
		},
		{
			label: 'document 2',
			onClick: action('document 2 click'),
		},
	],
	tooltipPlacement: 'right',
	hideLabel: true,
	link: true,
	onSelect: action('item selected'),
};
 <ActionDropdown {...props} />
 */
function ActionDropdown(props) {
	var bsStyle = props.bsStyle,
	    hideLabel = props.hideLabel,
	    icon = props.icon,
	    items = props.items,
	    label = props.label,
	    link = props.link,
	    onSelect = props.onSelect,
	    tooltipPlacement = props.tooltipPlacement,
	    tooltipLabel = props.tooltipLabel,
	    rest = _objectWithoutProperties(props, ['bsStyle', 'hideLabel', 'icon', 'items', 'label', 'link', 'onSelect', 'tooltipPlacement', 'tooltipLabel']);

	var title = _react2.default.createElement(
		'span',
		null,
		icon ? _react2.default.createElement(_Icon2.default, { name: icon }) : null,
		hideLabel ? null : _react2.default.createElement(
			'span',
			null,
			label
		)
	);
	var style = link ? 'link' : bsStyle;

	function onItemSelect(object, event) {
		if (onSelect) {
			onSelect(event, object);
		}
	}

	var dropdown = _react2.default.createElement(
		_reactBootstrap.DropdownButton,
		_extends({
			title: title,
			bsStyle: style,
			role: 'button',
			onSelect: onItemSelect
		}, rest),
		items.length ? items.map(getMenuItem) : _react2.default.createElement(
			_reactBootstrap.MenuItem,
			{ disabled: true },
			'No options'
		)
	);

	if (hideLabel || tooltipLabel) {
		return _react2.default.createElement(
			_TooltipTrigger2.default,
			{
				label: tooltipLabel || label,
				tooltipPlacement: tooltipPlacement
			},
			dropdown
		);
	}
	return dropdown;
}

ActionDropdown.propTypes = {
	bsStyle: _react.PropTypes.string,
	hideLabel: _react.PropTypes.bool,
	icon: _react.PropTypes.string,
	items: _react.PropTypes.arrayOf(_react.PropTypes.shape(_extends({
		icon: _react.PropTypes.string,
		label: _react.PropTypes.string
	}, _reactBootstrap.MenuItem.propTypes))).isRequired,
	label: _react.PropTypes.string.isRequired,
	link: _react.PropTypes.bool,
	onSelect: _react.PropTypes.func,
	tooltipPlacement: _reactBootstrap.OverlayTrigger.propTypes.placement,
	tooltipLabel: _react.PropTypes.string
};

ActionDropdown.defaultProps = {
	bsStyle: 'default',
	tooltipPlacement: 'top'
};

exports.default = ActionDropdown;