'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 _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _reactDebounceInput2 = _interopRequireDefault(_reactDebounceInput);

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

var _FormControl2 = _interopRequireDefault(_FormControl);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _keycode = require('keycode');

var _keycode2 = _interopRequireDefault(_keycode);

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

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

var _Icon2 = _interopRequireDefault(_Icon);

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

var _Filter2 = _interopRequireDefault(_Filter);

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _onKeyDown(event, escAction, enterAction) {
	switch (event.keyCode) {
		case _keycode2.default.codes.enter:
			if (enterAction) {
				enterAction(event);
			}
			break;
		case _keycode2.default.codes.esc:
			if (escAction) {
				escAction(event);
			}
			break;
		default:
			break;
	}
}

function FilterInput(props) {
	var id = props.id,
	    debounceMinLength = props.debounceMinLength,
	    debounceTimeout = props.debounceTimeout,
	    onBlur = props.onBlur,
	    onFocus = props.onFocus,
	    onFilter = props.onFilter,
	    onToggle = props.onToggle,
	    placeholder = props.placeholder,
	    value = props.value;


	var inputProps = {
		id: id,
		name: 'search',
		type: 'search',
		value: value,
		placeholder: placeholder,
		className: _Filter2.default.search,
		'aria-label': 'Filter',
		onBlur: onBlur && function (event) {
			return onBlur(event, event.target.value);
		},
		onFocus: onFocus && function (event) {
			return onFocus(event, event.target.value);
		},
		onChange: function onChange(event) {
			return onFilter(event, event.target.value);
		},
		onKeyDown: function onKeyDown(event) {
			return _onKeyDown(event, onToggle, onBlur);
		},
		autoFocus: true
	};

	if (debounceMinLength || debounceTimeout) {
		return _react2.default.createElement(_reactDebounceInput2.default, _extends({}, inputProps, {
			element: _FormControl2.default,
			minLength: debounceMinLength,
			debounceTimeout: debounceTimeout
		}));
	}
	return _react2.default.createElement(_FormControl2.default, inputProps);
}

FilterInput.propTypes = {
	id: _react.PropTypes.string,
	debounceMinLength: _react.PropTypes.number,
	debounceTimeout: _react.PropTypes.number,
	onBlur: _react.PropTypes.func,
	onFocus: _react.PropTypes.func,
	onFilter: _react.PropTypes.func.isRequired,
	onToggle: _react.PropTypes.func,
	placeholder: _react.PropTypes.string,
	value: _react.PropTypes.string
};

/**
 * @param {object} props react props
 * @example
 <Filter id="my-filter" docked="false" onFilter="filter()"></Filter>
 */
function Filter(props) {
	var id = props.id,
	    debounceMinLength = props.debounceMinLength,
	    debounceTimeout = props.debounceTimeout,
	    docked = props.docked,
	    highlight = props.highlight,
	    onBlur = props.onBlur,
	    onFocus = props.onFocus,
	    onFilter = props.onFilter,
	    onToggle = props.onToggle,
	    placeholder = props.placeholder,
	    value = props.value;

	if (docked) {
		return _react2.default.createElement(_Actions.Action, {
			id: id,
			className: 'navbar-right',
			onClick: onToggle,
			label: 'Toggle filter',
			hideLabel: true,
			icon: 'talend-search',
			bsStyle: 'link'
		});
	}

	function onSubmit(event) {
		event.preventDefault();
		return onFilter(event, (0, _get2.default)(event, 'target.search.value'));
	}

	var classes = (0, _classnames2.default)('navbar-form', 'navbar-right', _Filter2.default.filter, _defineProperty({}, _Filter2.default.highlight, highlight));

	return _react2.default.createElement(
		'form',
		{
			className: classes,
			role: 'search',
			onSubmit: onSubmit
		},
		_react2.default.createElement(_Icon2.default, { name: 'talend-search', className: _Filter2.default['search-icon'] }),
		_react2.default.createElement(
			'div',
			{ className: 'form-group' },
			_react2.default.createElement(FilterInput, {
				id: id && id + '-input',
				debounceMinLength: debounceMinLength,
				debounceTimeout: debounceTimeout,
				onBlur: onBlur,
				onFocus: onFocus,
				onFilter: onFilter,
				onToggle: onToggle,
				placeholder: placeholder,
				value: value
			}),
			_react2.default.createElement(_Actions.Action, {
				id: id && id + '-cross-icon',
				bsStyle: 'link',
				icon: 'talend-cross',
				label: 'Remove filter',
				hideLabel: true,
				onClick: onToggle
			})
		)
	);
}

Filter.propTypes = {
	id: _react.PropTypes.string,
	debounceMinLength: _react.PropTypes.number,
	debounceTimeout: _react.PropTypes.number,
	docked: _react.PropTypes.bool,
	onBlur: _react.PropTypes.func,
	onFocus: _react.PropTypes.func,
	onFilter: _react.PropTypes.func.isRequired,
	onToggle: _react.PropTypes.func.isRequired,
	highlight: _react.PropTypes.bool,
	placeholder: _react.PropTypes.string,
	value: _react.PropTypes.string
};

Filter.defaultProps = {
	docked: true,
	placeholder: 'Filter'
};

exports.default = Filter;