'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 _Navbar = require('react-bootstrap/lib/Navbar');

var _Navbar2 = _interopRequireDefault(_Navbar);

var _SelectAll = require('./SelectAll');

var _SelectAll2 = _interopRequireDefault(_SelectAll);

var _SelectDisplayMode = require('./SelectDisplayMode');

var _SelectDisplayMode2 = _interopRequireDefault(_SelectDisplayMode);

var _SelectSortBy = require('./SelectSortBy');

var _SelectSortBy2 = _interopRequireDefault(_SelectSortBy);

var _Pagination = require('./Pagination');

var _Pagination2 = _interopRequireDefault(_Pagination);

var _Filter = require('./Filter');

var _Filter2 = _interopRequireDefault(_Filter);

var _Label = require('./Label');

var _Label2 = _interopRequireDefault(_Label);

var _ActionBar = require('../../ActionBar');

var _ActionBar2 = _interopRequireDefault(_ActionBar);

var _Toolbar = require('./Toolbar.scss');

var _Toolbar2 = _interopRequireDefault(_Toolbar);

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

function adaptActionsIds(actions, parentId) {
	return actions && actions.map(function (action) {
		if (action.id) {
			return _extends({}, action, {
				id: parentId + '-actions-' + action.id
			});
		}
		return action;
	});
}

function adaptLeftAndRightActions(actions, parentId) {
	return actions && {
		left: adaptActionsIds(actions.left, parentId),
		right: adaptActionsIds(actions.right, parentId)
	};
}

/**
 * @param {string} id the id of Toolbar
 * @param {object} actionBar the ActionBar properties
 * @param {object} display the SelectDisplayMode properties
 * @param {object} sort the SelectSortBy properties
 * @param {object} pagination the Pagination properties
 * @param {object} filter the Filter properties
 * @example
 <Toolbar id="my-toolbar"></Toolbar>
 */
function Toolbar(_ref) {
	var id = _ref.id,
	    actionBar = _ref.actionBar,
	    selectAllCheckbox = _ref.selectAllCheckbox,
	    display = _ref.display,
	    sort = _ref.sort,
	    pagination = _ref.pagination,
	    filter = _ref.filter;

	var actionBarProps = actionBar;
	if (id && actionBar) {
		var actions = actionBar.actions,
		    multiSelectActions = actionBar.multiSelectActions;

		actionBarProps = _extends({}, actionBar, {
			actions: adaptLeftAndRightActions(actions, id),
			multiSelectActions: adaptLeftAndRightActions(multiSelectActions, id)
		});
	}
	var displayModeId = id && id + '-display-mode';
	var hasToolbarItem = selectAllCheckbox || display || sort || pagination || filter;

	return _react2.default.createElement(
		'div',
		{ className: 'tc-list-toolbar' },
		actionBar && _react2.default.createElement(_ActionBar2.default, actionBarProps),
		hasToolbarItem && _react2.default.createElement(
			_Navbar2.default,
			{
				componentClass: 'div',
				className: _Toolbar2.default['tc-list-toolbar'],
				role: 'toolbar', fluid: true
			},
			selectAllCheckbox && _react2.default.createElement(_SelectAll2.default, selectAllCheckbox),
			display && _react2.default.createElement(_Label2.default, { text: 'Display:', htmlFor: displayModeId }),
			display && _react2.default.createElement(_SelectDisplayMode2.default, _extends({ id: displayModeId }, display)),
			sort && _react2.default.createElement(_Label2.default, { text: 'Sort by:', htmlFor: id && id + '-sort-by' }),
			sort && _react2.default.createElement(_SelectSortBy2.default, _extends({ id: id && id + '-sort' }, sort)),
			pagination && _react2.default.createElement(_Label2.default, { text: 'Show:', htmlFor: id && id + '-pagination-size' }),
			pagination && _react2.default.createElement(_Pagination2.default, _extends({ id: id && id + '-pagination' }, pagination)),
			filter && _react2.default.createElement(_Filter2.default, _extends({ id: id && id + '-filter' }, filter))
		)
	);
}

Toolbar.propTypes = {
	id: _react2.default.PropTypes.string,
	actionBar: _react2.default.PropTypes.shape(_ActionBar2.default.propTypes),
	selectAllCheckbox: _react2.default.PropTypes.shape(_SelectAll2.default.propTypes),
	display: _react2.default.PropTypes.shape(_SelectDisplayMode2.default.propTypes),
	sort: _react2.default.PropTypes.shape(_SelectSortBy2.default.propTypes),
	pagination: _react2.default.PropTypes.shape(_Pagination2.default.propTypes),
	filter: _react2.default.PropTypes.shape(_Filter2.default.propTypes)
};

exports.default = Toolbar;