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

var _uuid = require('uuid');

var _uuid2 = _interopRequireDefault(_uuid);

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

var _Icon2 = _interopRequireDefault(_Icon);

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

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

/**
 * @param {object} props react props
 * @example
 const props = {
	label: 'Add File',
	icon: 'fa fa-plus',
	onClick: action('onAdd'),
	items: [
		{
			label: 'From Local',
			onClick: action('From Local click'),
		},
		{
			label: 'From Remote',
			onClick: action('From Remote click'),
		},
	],
	emptyDropdownLabel: 'No option',
};
 <ActionSplitDropdown {...props} />
 */
function rClick(event, onClick, action, model) {
	return onClick(event, { action: action, model: model });
}

function ActionSplitDropdown(props) {
	var icon = props.icon,
	    items = props.items,
	    label = props.label,
	    model = props.model,
	    _onClick = props.onClick,
	    emptyDropdownLabel = props.emptyDropdownLabel,
	    className = props.className,
	    rest = _objectWithoutProperties(props, ['icon', 'items', 'label', 'model', 'onClick', 'emptyDropdownLabel', 'className']);

	var Title = _react2.default.createElement(
		'span',
		null,
		icon ? _react2.default.createElement(_Icon2.default, { name: icon }) : null,
		_react2.default.createElement(
			'span',
			null,
			label
		)
	);

	return _react2.default.createElement(
		_reactBootstrap.SplitButton,
		_extends({
			onClick: function onClick(event) {
				return rClick(event, _onClick, _extends({ label: label }, rest), model);
			},
			title: Title,
			id: _uuid2.default.v4(),
			className: (0, _classnames2.default)(className, _ActionSplitDropdown2.default['tc-split-dropdown'])
		}, rest),
		items.length ? items.map(function (item, index) {
			return _react2.default.createElement(
				_reactBootstrap.MenuItem,
				_extends({}, item, { key: index }),
				item.icon && _react2.default.createElement(_Icon2.default, { name: item.icon }),
				item.label
			);
		}) : _react2.default.createElement(
			_reactBootstrap.MenuItem,
			{ disabled: true },
			emptyDropdownLabel
		)
	);
}

ActionSplitDropdown.propTypes = {
	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,
	model: _react.PropTypes.object, // eslint-disable-line react/forbid-prop-types
	onClick: _react.PropTypes.func.isRequired,
	emptyDropdownLabel: _react.PropTypes.string,
	className: _react.PropTypes.string
};

exports.default = ActionSplitDropdown;