'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.headerClasses = headerClasses;
exports.renderActions = renderActions;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _Action2 = _interopRequireDefault(_Action);

var _Header = require('./Header.scss');

var _Header2 = _interopRequireDefault(_Header);

var _Header3 = require('./Header.propTypes');

var _Header4 = _interopRequireDefault(_Header3);

var _Header5 = require('./Header.defaultProps');

var _Header6 = _interopRequireDefault(_Header5);

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

function headerClasses() {
	return (0, _classnames2.default)(_Header2.default['tc-listview-header'], 'tc-listview-header');
}

function getAction(action, index) {
	function onClick(event) {
		if (action.onClick) {
			action.onClick(event, { value: event.target.value });
		}
	}

	return _react2.default.createElement(_Action2.default, {
		key: index + '-listview-header-action',
		label: action.label,
		icon: action.icon,
		onClick: onClick,
		disabled: action.disabled,
		btooltipPlacement: 'bottom',
		inProgress: action.inProgress,
		hideLabel: true,
		link: true
	});
}

function renderActions() {
	var headerDefault = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

	if (headerDefault.length) {
		return _react2.default.createElement(
			'div',
			{ className: 'actions' },
			headerDefault.map(getAction)
		);
	}
	return null;
}

function Header(_ref) {
	var headerDefault = _ref.headerDefault,
	    headerLabel = _ref.headerLabel,
	    nbItemsSelected = _ref.nbItemsSelected,
	    nbItems = _ref.nbItems,
	    required = _ref.required;

	function renderTitle() {
		if (headerLabel) {
			return _react2.default.createElement(
				'strong',
				null,
				headerLabel,
				required && '*'
			);
		}
		return null;
	}

	function renderCount() {
		if (nbItems >= 1 && nbItemsSelected >= 1) {
			return _react2.default.createElement(
				'small',
				null,
				'(' + nbItemsSelected + '/' + nbItems + ' selected)'
			);
		}
		return null;
	}

	return _react2.default.createElement(
		'header',
		{ className: headerClasses() },
		renderTitle(),
		renderCount(),
		renderActions(headerDefault)
	);
}

Header.propTypes = _Header4.default;

Header.defaultProps = _Header6.default;

exports.default = Header;