'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.STATUS_CANCELED = exports.STATUS_FAILED = exports.STATUS_SUCCESSFUL = exports.STATUS_IN_PROGRESS = undefined;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _Actions = require('./../Actions/Actions.component');

var _Actions2 = _interopRequireDefault(_Actions);

var _CircularProgress = require('./../CircularProgress/CircularProgress.component');

var _CircularProgress2 = _interopRequireDefault(_CircularProgress);

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

var _Icon2 = _interopRequireDefault(_Icon);

var _Status = require('./Status.scss');

var _Status2 = _interopRequireDefault(_Status);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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

/**
 * @param {object} props react props
 * @example
 const status = {
			status: 'Successful',
			label: 'Successful',
			icon: 'fa fa-check',
			actions: [
					{
						label: 'cancel',
						icon: 'fa fa-cancel',
						onClick: action('onCancel'),
					},
					{
						label: 'delete',
						icon: 'fa fa-delete',
						onClick: action('onDelete'),
					}],
	};
 <Status {...status}/>
 */

var STATUS_IN_PROGRESS = exports.STATUS_IN_PROGRESS = 'inProgress';
var STATUS_SUCCESSFUL = exports.STATUS_SUCCESSFUL = 'successful';
var STATUS_FAILED = exports.STATUS_FAILED = 'failed';
var STATUS_CANCELED = exports.STATUS_CANCELED = 'canceled';

function getbsStyleFromStatus(status) {
	switch (status) {
		case STATUS_IN_PROGRESS:
			return 'info';
		case STATUS_SUCCESSFUL:
			return 'success';
		case STATUS_FAILED:
			return 'danger';
		case STATUS_CANCELED:
			return 'muted';
		default:
			return '';
	}
}

function renderIcon(status, icon, progress) {
	if (status === STATUS_IN_PROGRESS) {
		if (icon) {
			return _react2.default.createElement(_Icon2.default, { name: icon });
		}
		return _react2.default.createElement(_CircularProgress2.default, { size: 'small', percent: progress });
	}
	return icon && _react2.default.createElement(_Icon2.default, { name: icon });
}

function Status(props) {
	var status = props.status,
	    label = props.label,
	    icon = props.icon,
	    actions = props.actions,
	    progress = props.progress;


	var rootClassnames = (0, _classnames2.default)(_Status2.default['tc-status'], 'tc-status', 'text-' + getbsStyleFromStatus(status), _defineProperty({}, _Status2.default.action, actions && actions.length));

	return _react2.default.createElement(
		'div',
		{ className: rootClassnames },
		renderIcon(status, icon, progress),
		_react2.default.createElement(
			'span',
			{ className: (0, _classnames2.default)(_Status2.default['tc-status-label'], 'tc-status-label') },
			' ',
			label,
			' '
		),
		_react2.default.createElement(
			'span',
			{ className: _Status2.default['tc-status-actions'] },
			_react2.default.createElement(_Actions2.default, { actions: actions })
		)
	);
}

Status.propTypes = {
	status: _react.PropTypes.oneOf([STATUS_IN_PROGRESS, STATUS_SUCCESSFUL, STATUS_FAILED, STATUS_CANCELED]),
	label: _react.PropTypes.string.isRequired,
	icon: _react.PropTypes.string,
	actions: _Actions2.default.propTypes.actions,
	progress: _react.PropTypes.number
};

Status.defaultProps = {
	actions: []
};

exports.default = Status;