'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 _uuid = require('uuid');

var _uuid2 = _interopRequireDefault(_uuid);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _reactAutowhatever = require('react-autowhatever');

var _reactAutowhatever2 = _interopRequireDefault(_reactAutowhatever);

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

var _Typeahead2 = _interopRequireDefault(_Typeahead);

var _TypeaheadComponent = require('./Typeahead.component.renderers');

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

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

/**
 * Show suggestions for search bar
 * @example
 *
 * <Typeahead {...props} />
 */
function Typeahead(_ref) {
	var onToggle = _ref.onToggle,
	    icon = _ref.icon,
	    position = _ref.position,
	    rest = _objectWithoutProperties(_ref, ['onToggle', 'icon', 'position']);

	if (onToggle) {
		return _react2.default.createElement(_Actions.Action, {
			onClick: onToggle,
			label: icon.title,
			hideLabel: true,
			icon: icon.name,
			bsStyle: icon.bsStyle,
			className: _Typeahead2.default['only-icon-cls'],
			tooltipPlacement: icon.tooltipPlacement
		});
	}

	var containerClass = (0, _classnames2.default)(_Typeahead2.default['tc-typeahead-container'], position === 'right' && _Typeahead2.default.right);

	var autowhateverProps = _extends({}, rest, {
		inputProps: {
			value: rest.value,
			placeholder: rest.placeholder,
			onBlur: rest.onBlur,
			onChange: rest.onChange && function (event) {
				return rest.onChange(event, { value: event.target.value });
			},
			onKeyDown: rest.onKeyDown,
			debounceMinLength: rest.debounceMinLength,
			debounceTimeout: rest.debounceTimeout,
			icon: icon
		},
		itemProps: {
			onMouseDown: rest.onSelect
		},
		renderInputComponent: _TypeaheadComponent.renderInputComponent,
		renderItemsContainer: (0, _TypeaheadComponent.renderItemsContainerFactory)(rest.items, rest.noResultText, rest.searching, rest.searchingText),
		renderSectionTitle: _TypeaheadComponent.renderSectionTitle,
		renderItem: _TypeaheadComponent.renderItem,
		multiSection: true,
		getSectionItems: function getSectionItems(section) {
			return section.suggestions;
		},
		theme: _extends({}, rest.theme, {
			container: containerClass,
			containerOpen: _Typeahead2.default['container-open'],
			highlight: _Typeahead2.default['highlight-match'],
			input: _Typeahead2.default['typeahead-input'],
			itemFocused: _Typeahead2.default['item-focused'],
			itemsContainer: _Typeahead2.default['items-container'],
			itemsList: _Typeahead2.default.items,
			sectionContainer: _Typeahead2.default['section-container']
		}),
		focusedSectionIndex: rest.focusedSectionIndex,
		focusedItemIndex: rest.focusedItemIndex,
		items: rest.items || [],
		renderItemData: { value: rest.value }
	});

	return _react2.default.createElement(_reactAutowhatever2.default, autowhateverProps);
}

Typeahead.defaultProps = {
	id: _uuid2.default.v4(),
	position: 'left',
	items: null,
	noResultText: 'No result.',
	searching: false,
	searchingText: 'Searching for matches…'
};

Typeahead.propTypes = {
	id: _react.PropTypes.string,
	onToggle: _react.PropTypes.func,
	icon: _react.PropTypes.shape({
		name: _react.PropTypes.string,
		title: _react.PropTypes.string,
		bsStyle: _react.PropTypes.string
	}),
	position: _react.PropTypes.oneOf(['left', 'right']),
	value: _react.PropTypes.string,
	placeholder: _react.PropTypes.string,
	onBlur: _react.PropTypes.func,
	onChange: _react.PropTypes.func,
	onSelect: _react.PropTypes.func,
	onKeyDown: _react.PropTypes.func,
	focusedSectionIndex: _react.PropTypes.number,
	focusedItemIndex: _react.PropTypes.number,
	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,
	debounceMinLength: _react.PropTypes.number,
	debounceTimeout: _react.PropTypes.number
};

exports.default = Typeahead;