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

var _CircularProgress2 = _interopRequireDefault(_CircularProgress);

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

var _Icon2 = _interopRequireDefault(_Icon);

var _getPropsFrom = require('../../utils/getPropsFrom');

var _getPropsFrom2 = _interopRequireDefault(_getPropsFrom);

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 LEFT = 'left';
var RIGHT = 'right';

function getIcon(_ref) {
	var icon = _ref.icon,
	    iconTransform = _ref.iconTransform,
	    inProgress = _ref.inProgress;

	if (inProgress) {
		return _react2.default.createElement(_CircularProgress2.default, { size: 'small', key: 'icon' });
	}

	if (icon) {
		return _react2.default.createElement(_Icon2.default, { name: icon, transform: iconTransform, key: 'icon' });
	}

	return null;
}
getIcon.propTypes = {
	icon: _react.PropTypes.string,
	iconTransform: _react.PropTypes.string,
	inProgress: _react.PropTypes.bool
};

function getLabel(_ref2) {
	var hideLabel = _ref2.hideLabel,
	    label = _ref2.label;

	if (hideLabel) {
		return null;
	}
	return _react2.default.createElement(
		'span',
		{ key: 'label' },
		label
	);
}

getLabel.propTypes = {
	label: _react.PropTypes.string,
	hideLabel: _react.PropTypes.bool
};

function adjustContentPlacement(icon, label, iconPosition) {
	if (iconPosition === RIGHT) {
		return [label, icon];
	}
	return [icon, label];
}

function getContent(props) {
	return adjustContentPlacement(getIcon(props), getLabel(props), props.iconPosition);
}

function noOp() {}

/**
 * @param {object} props react props
 * @example
 const props = {
	label: 'edit',
	icon: 'fa fa-edit',
	onClick: action('onEdit'),
	tooltipPlacement: 'right',
	hideLabel: true,
	link: true,
};
 <Action {...props} />
 */
function Action(props) {
	var bsStyle = props.bsStyle,
	    inProgress = props.inProgress,
	    disabled = props.disabled,
	    hideLabel = props.hideLabel,
	    label = props.label,
	    link = props.link,
	    model = props.model,
	    _props$onMouseDown = props.onMouseDown,
	    onMouseDown = _props$onMouseDown === undefined ? noOp : _props$onMouseDown,
	    _props$onClick = props.onClick,
	    onClick = _props$onClick === undefined ? noOp : _props$onClick,
	    tooltipPlacement = props.tooltipPlacement,
	    tooltip = props.tooltip,
	    tooltipLabel = props.tooltipLabel,
	    rest = _objectWithoutProperties(props, ['bsStyle', 'inProgress', 'disabled', 'hideLabel', 'label', 'link', 'model', 'onMouseDown', 'onClick', 'tooltipPlacement', 'tooltip', 'tooltipLabel']);

	var buttonProps = (0, _getPropsFrom2.default)(_reactBootstrap.Button, rest);
	var style = link ? 'link' : bsStyle;
	var rClick = onClick && function (event) {
		return onClick(event, {
			action: _extends({ label: label }, rest),
			model: model
		});
	};

	var rMouseDown = function rMouseDown(event) {
		return onMouseDown(event, {
			action: _extends({ label: label }, rest),
			model: model
		});
	};

	var buttonContent = getContent(props);

	var btn = _react2.default.createElement(
		_reactBootstrap.Button,
		_extends({
			onMouseDown: rMouseDown,
			onClick: rClick,
			bsStyle: style,
			disabled: inProgress || disabled,
			role: link ? 'link' : null
		}, buttonProps),
		buttonContent
	);

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

Action.propTypes = _extends({}, getIcon.propTypes, {
	id: _react.PropTypes.string,
	bsStyle: _react.PropTypes.string,
	disabled: _react.PropTypes.bool,
	hideLabel: _react.PropTypes.bool,
	iconPosition: _react.PropTypes.oneOf([LEFT, RIGHT]),
	label: _react.PropTypes.string.isRequired,
	link: _react.PropTypes.bool,
	model: _react.PropTypes.object, // eslint-disable-line react/forbid-prop-types
	name: _react.PropTypes.string,
	onClick: _react.PropTypes.func,
	tooltipPlacement: _reactBootstrap.OverlayTrigger.propTypes.placement,
	tooltip: _react.PropTypes.bool,
	tooltipLabel: _react.PropTypes.string
});

Action.defaultProps = {
	bsStyle: 'default',
	tooltipPlacement: 'top',
	inProgress: false,
	disabled: false
};

exports.default = Action;