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

exports.renderNavItem = renderNavItem;
exports.renderDropdownItem = renderDropdownItem;
exports.renderDropdown = renderDropdown;
exports.renderNav = renderNav;
exports.renderFormGroup = renderFormGroup;
exports.renderForm = renderForm;
exports.renderTypeahead = renderTypeahead;
exports.renderContent = renderContent;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactBootstrap = require('react-bootstrap');

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

var _Icon2 = _interopRequireDefault(_Icon);

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

var _Action2 = _interopRequireDefault(_Action);

var _Typeahead = require('../Typeahead');

var _Typeahead2 = _interopRequireDefault(_Typeahead);

var _AppHeaderBar = require('./AppHeaderBar.scss');

var _AppHeaderBar2 = _interopRequireDefault(_AppHeaderBar);

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 NAV_ITEM = 'navItem';
var DROPDOWN = 'dropdown';

function NavListItem(props) {
	var item = props.item,
	    rest = _objectWithoutProperties(props, ['item']);

	return _react2.default.createElement(
		'li',
		null,
		_react2.default.createElement(_Action2.default, _extends({}, item, {
			hideLabel: true,
			label: item.name,
			bsStyle: 'link'
		}, rest))
	);
}

NavListItem.propTypes = {
	item: _react2.default.PropTypes.shape(_Action2.default.propTypes)
};

function renderNavItem(item, index) {
	return _react2.default.createElement(NavListItem, { item: item, key: index });
}

renderNavItem.propTypes = {
	item: NavListItem.propTypes,
	index: _react2.default.PropTypes.number
};

function renderDropdownItem(props, index) {
	var icon = props.icon,
	    name = props.name,
	    rest = _objectWithoutProperties(props, ['icon', 'name']);

	return _react2.default.createElement(
		_reactBootstrap.MenuItem,
		_extends({ key: index }, rest),
		_react2.default.createElement(_Icon2.default, { name: icon }),
		name
	);
}

renderDropdownItem.propTypes = {
	icon: _react2.default.PropTypes.string,
	name: _react2.default.PropTypes.string,
	onClick: _react2.default.PropTypes.func
};

function renderDropdown(props, index) {
	var dropdownProps = props.dropdown;
	if (dropdownProps.onSelect) {
		dropdownProps = _extends({}, props.dropdown, {
			onSelect: function onSelect(eventKey, event) {
				return dropdownProps.onSelect(event, eventKey);
			}
		});
	}
	return _react2.default.createElement(
		_reactBootstrap.NavDropdown,
		_extends({}, dropdownProps, { key: index }),
		props.items.map(renderDropdownItem)
	);
}
renderDropdown.propTypes = {
	dropdown: _react2.default.PropTypes.shape(_reactBootstrap.NavDropdown.propTypes),
	items: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape(renderDropdownItem.propTypes))
};

function renderNav(props) {
	return _react2.default.createElement(
		_reactBootstrap.Nav,
		props.nav,
		props.navItems.map(function (itemDef, index) {
			var type = itemDef.type,
			    item = itemDef.item;

			switch (type) {
				case NAV_ITEM:
					return renderNavItem(item, index);
				case DROPDOWN:
					return renderDropdown(item, index);
				default:
					return null;
			}
		})
	);
}
renderNav.propTypes = {
	nav: _react2.default.PropTypes.shape(_reactBootstrap.Nav.propTypes),
	navItems: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape({
		type: _react2.default.PropTypes.oneOf([NAV_ITEM, DROPDOWN]),
		item: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.shape(renderNavItem.propTypes), _react2.default.PropTypes.shape(renderDropdown.propTypes)])
	}))
};

function renderFormGroup(props, index) {
	return _react2.default.createElement(
		_reactBootstrap.FormGroup,
		_extends({ key: index }, props.formgroup),
		_react2.default.createElement(_reactBootstrap.FormControl, props.formcontrol)
	);
}
renderFormGroup.propTypes = {
	formgroup: _react2.default.PropTypes.shape(_reactBootstrap.FormGroup.propTypes),
	formcontrol: _react2.default.PropTypes.shape(_reactBootstrap.FormControl.propTypes)
};

function renderForm(props, index) {
	return _react2.default.createElement(
		'form',
		_extends({ className: 'navbar-form navbar-right', role: 'search' }, props.form, { key: index }),
		props.formgroups && props.formgroups.map(renderFormGroup),
		_react2.default.createElement(
			_reactBootstrap.Button,
			props.button,
			props.icon && _react2.default.createElement(_Icon2.default, { name: props.icon }),
			props.buttonLabel
		)
	);
}
renderForm.propTypes = {
	form: _react2.default.PropTypes.shape(_reactBootstrap.Navbar.Form.propTypes),
	formgroups: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape(renderFormGroup.propTypes)),
	button: _react2.default.PropTypes.shape(_reactBootstrap.Button.propTypes),
	buttonLabel: _react2.default.PropTypes.string
};

function renderTypeahead(search, index) {
	return _react2.default.createElement(
		'form',
		{ className: 'navbar-form navbar-right', role: 'search', key: index },
		_react2.default.createElement(_Typeahead2.default, search)
	);
}
renderTypeahead.propTypes = _react2.default.PropTypes.shape(_Typeahead2.default.propTypes);

function renderContent(props, index) {
	if (props.navs) {
		return props.navs.map(renderNav);
	}
	if (props.forms) {
		return props.forms.map(renderForm);
	}
	if (props.search) {
		return renderTypeahead(props.search, index);
	}
	return null;
}
renderContent.propTypes = {
	navs: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape(renderNav.propTypes)),
	forms: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape(renderForm.propTypes)),
	search: _react2.default.PropTypes.shape(_Typeahead2.default.propTypes)
};

/**
 * The top bar is the place where the user finds useful information and tools
 * to describe the application
 * the logo, the name of the application, an optionnal global search,
 * few icons for feedback & onboarding and a menu to access logout
 * and profile information.
 * @param {object} props   react props
 */
function AppHeaderBar(props) {
	var brandLink = void 0;
	if (props.brandLink) {
		brandLink = _react2.default.createElement(
			_reactBootstrap.Button,
			_extends({
				bsStyle: 'link',
				role: 'link'
			}, props.brandLink),
			props.app
		);
	} else {
		brandLink = _react2.default.createElement(
			'span',
			null,
			props.app
		);
	}

	return _react2.default.createElement(
		_reactBootstrap.Navbar,
		{ fluid: true, fixedTop: true, inverse: true, className: 'tc-app-header-bar ' + _AppHeaderBar2.default['tc-app-header-bar'] },
		_react2.default.createElement(
			_reactBootstrap.Navbar.Header,
			null,
			_react2.default.createElement(
				_reactBootstrap.Navbar.Brand,
				null,
				brandLink
			),
			_react2.default.createElement(_reactBootstrap.Navbar.Toggle, null)
		),
		_react2.default.createElement(
			_reactBootstrap.Navbar.Collapse,
			null,
			props.content && props.content.map(renderContent)
		)
	);
}

AppHeaderBar.propTypes = {
	app: _react2.default.PropTypes.string.isRequired,
	brandLink: _react2.default.PropTypes.shape({
		onClick: _react2.default.PropTypes.func,
		className: _react2.default.PropTypes.string
	}),
	content: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape(renderContent.propTypes))
};

exports.default = AppHeaderBar;