'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _Action2 = _interopRequireDefault(_Action);

var _Header = require('./Header.scss');

var _Header2 = _interopRequireDefault(_Header);

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

var inputRef = void 0;

function headerClasses() {
	return (0, _classnames2.default)(_Header2.default['tc-listview-header'], 'tc-listview-header');
}

function getAction(action, index) {
	function onClick(event) {
		if (action.onClick) {
			action.onClick(event, {
				value: inputRef.value
			});
		}
	}

	return _react2.default.createElement(_Action2.default, {
		key: index + '-listview-header-action',
		label: action.label,
		icon: action.icon,
		onClick: onClick,
		disabled: action.disabled,
		tooltipPlacement: 'bottom',
		inProgress: action.inProgress,
		hideLabel: true,
		link: true
	});
}

function HeaderInput(_ref) {
	var headerInput = _ref.headerInput,
	    onInputChange = _ref.onInputChange,
	    inputPlaceholder = _ref.inputPlaceholder,
	    onAddKeyDown = _ref.onAddKeyDown;

	function onInputChangeHandler(event) {
		onInputChange(event, {
			value: event.target.value
		});
	}

	function onAddKeyDownHandler(event) {
		onAddKeyDown(event, {
			value: event.target.value
		});
	}

	return _react2.default.createElement(
		'header',
		{ className: headerClasses() },
		_react2.default.createElement('input', {
			type: 'text',
			placeholder: inputPlaceholder,
			ref: function ref(input) {
				inputRef = input;
			},
			onChange: onInputChangeHandler,
			onKeyDown: onAddKeyDownHandler,
			'aria-label': inputPlaceholder,
			autoFocus: true
		}),
		headerInput.map(getAction)
	);
}

HeaderInput.propTypes = {
	headerInput: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Action2.default.propTypes)).isRequired,
	onInputChange: _react.PropTypes.func,
	inputPlaceholder: _react.PropTypes.string,
	onAddKeyDown: _react.PropTypes.func
};

exports.default = HeaderInput;