'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.renderItem = exports.renderSectionTitle = exports.renderItemsContainerFactory = exports.renderInputComponent = undefined;

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

var _ControlLabel2 = _interopRequireDefault(_ControlLabel);

var _FormControl = require('react-bootstrap/lib/FormControl');

var _FormControl2 = _interopRequireDefault(_FormControl);

var _reactDebounceInput = require('react-debounce-input');

var _reactDebounceInput2 = _interopRequireDefault(_reactDebounceInput);

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

var _Icon2 = _interopRequireDefault(_Icon);

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

var _CircularProgress2 = _interopRequireDefault(_CircularProgress);

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

var _Typeahead2 = _interopRequireDefault(_Typeahead);

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 renderInputComponent = exports.renderInputComponent = function renderInputComponent(props) {
	var key = props.key,
	    debounceMinLength = props.debounceMinLength,
	    debounceTimeout = props.debounceTimeout,
	    icon = props.icon,
	    rest = _objectWithoutProperties(props, ['key', 'debounceMinLength', 'debounceTimeout', 'icon']);

	var renderedIcon = icon && _react2.default.createElement(
		'div',
		{ className: _Typeahead2.default['icon-cls'] },
		_react2.default.createElement(_Icon2.default, { name: icon.name, title: icon.title })
	);

	return _react2.default.createElement(
		'div',
		{ className: _Typeahead2.default['typeahead-input-icon'] },
		_react2.default.createElement(
			_ControlLabel2.default,
			{ srOnly: true, htmlFor: key },
			'Search'
		),
		debounceMinLength || debounceTimeout ? _react2.default.createElement(_reactDebounceInput2.default, _extends({
			id: key
		}, rest, {
			autoFocus: true,
			element: _FormControl2.default,
			minLength: debounceMinLength,
			debounceTimeout: debounceTimeout
		})) : _react2.default.createElement(_FormControl2.default, _extends({
			id: key,
			autoFocus: true
		}, rest)),
		renderedIcon
	);
};
renderInputComponent.propTypes = {
	key: _react.PropTypes.string,
	debounceMinLength: _react.PropTypes.number,
	debounceTimeout: _react.PropTypes.number,
	icon: _react.PropTypes.shape({
		name: _react.PropTypes.string,
		title: _react.PropTypes.string
	})
};

var ItemContainer = function ItemContainer(props) {
	var items = props.items,
	    noResultText = props.noResultText,
	    searching = props.searching,
	    searchingText = props.searchingText,
	    containerProps = _objectWithoutProperties(props, ['items', 'noResultText', 'searching', 'searchingText']);

	var className = containerProps.className,
	    restProps = _objectWithoutProperties(containerProps, ['className']);

	if (searching) {
		return _react2.default.createElement(
			'div',
			_extends({ className: className + ' ' + _Typeahead2.default['is-searching'] }, restProps),
			_react2.default.createElement(
				'span',
				{ className: 'is-searching' },
				searchingText
			),
			' ',
			_react2.default.createElement(_CircularProgress2.default, null)
		);
	}
	if (items && !items.length) {
		return _react2.default.createElement(
			'div',
			_extends({ className: className + ' ' + _Typeahead2.default['no-result'] }, restProps),
			_react2.default.createElement(
				'span',
				{ className: 'no-result' },
				noResultText
			)
		);
	}
	return _react2.default.createElement('div', containerProps);
};
ItemContainer.propTypes = {
	className: _react.PropTypes.string,
	items: _react.PropTypes.arrayOf(_react.PropTypes.shape({
		title: _react.PropTypes.string,
		description: _react.PropTypes.string,
		suggestions: _react.PropTypes.arrayOf(_react.PropTypes.shape({
			title: _react.PropTypes.string,
			description: _react.PropTypes.string
		}))
	})),
	noResultText: _react.PropTypes.string,
	searching: _react.PropTypes.bool,
	searchingText: _react.PropTypes.string
};

var renderItemsContainerFactory = exports.renderItemsContainerFactory = function renderItemsContainerFactory(items, noResultText, searching, searchingText) {
	var renderItemsContainerComponent = function renderItemsContainerComponent(props) {
		var id = props.id,
		    key = props.key,
		    ref = props.ref,
		    rest = _objectWithoutProperties(props, ['id', 'key', 'ref']);

		return _react2.default.createElement(
			'div',
			{
				id: id,
				ref: ref,
				key: key
			},
			_react2.default.createElement(ItemContainer, _extends({}, rest, {
				items: items,
				noResultText: noResultText,
				searching: searching,
				searchingText: searchingText
			}))
		);
	};

	renderItemsContainerComponent.propTypes = {
		id: _react.PropTypes.string,
		key: _react.PropTypes.string,
		ref: _react.PropTypes.func
	};

	return renderItemsContainerComponent;
};

var renderSectionTitle = exports.renderSectionTitle = function renderSectionTitle(section) {
	if (section) {
		return _react2.default.createElement(
			'div',
			{ className: _Typeahead2.default['section-header'] },
			section.icon && _react2.default.createElement(_Icon2.default, { name: section.icon.name, title: section.icon.title }),
			_react2.default.createElement(
				'span',
				{ className: _Typeahead2.default['section-header-title'] },
				section.title
			)
		);
	}
	return null;
};

function emphasise(text, value) {
	if (!text) {
		return '';
	}
	if (!value) {
		return [text];
	}

	var parts = text.split(new RegExp('(' + value + ')', 'gi')).filter(Boolean);
	return parts.map(function (part) {
		if (value && part.toUpperCase() === value.toUpperCase()) {
			return _react2.default.createElement(
				'em',
				{ className: _Typeahead2.default['highlight-match'] },
				part
			);
		}
		return part;
	});
}

var renderItem = exports.renderItem = function renderItem(item, _ref) {
	var value = _ref.value;

	var title = item.title ? item.title.trim() : '';
	return _react2.default.createElement(
		'div',
		{ className: _Typeahead2.default.item, title: title },
		_react2.default.createElement(
			'span',
			{ className: _Typeahead2.default['item-title'] },
			emphasise(title, value)
		),
		_react2.default.createElement(
			'p',
			{ className: _Typeahead2.default['item-description'] },
			emphasise(item.description, value)
		)
	);
};