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

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 headerClasses(headerError) {
	var _classNames;

	return (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _Header2.default['tc-enumeration-header'], true), _defineProperty(_classNames, 'tc-enumeration-header', true), _defineProperty(_classNames, 'has-error', !!headerError), _classNames));
}

function headerErrorClasses() {
	var _classNames2;

	return (0, _classnames2.default)((_classNames2 = {}, _defineProperty(_classNames2, _Header2.default['tc-enumeration-header-error'], true), _defineProperty(_classNames2, 'tc-enumeration-header-error', true), _classNames2));
}

function getAction(action, index, getInternalInputRef) {
	function onClick(event) {
		var internalInputRef = getInternalInputRef();
		if (action.onClick) {
			action.onClick(event, {
				value: internalInputRef.value
			});
		}
	}

	return _react2.default.createElement(_Action2.default, {
		key: index + '-enum-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 _this = this;

	var headerInput = _ref.headerInput,
	    headerError = _ref.headerError,
	    onInputChange = _ref.onInputChange,
	    inputPlaceholder = _ref.inputPlaceholder,
	    onAddKeyDown = _ref.onAddKeyDown,
	    value = _ref.value,
	    inputRef = _ref.inputRef;

	var internalInputRef = null;

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

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

	function getInternalInputRef() {
		return internalInputRef;
	}

	return _react2.default.createElement(
		'header',
		{ className: headerClasses(headerError) },
		_react2.default.createElement('input', {
			type: 'text',
			placeholder: inputPlaceholder,
			ref: function ref(input) {
				internalInputRef = input;
				if (inputRef) {
					inputRef(input);
				}
			},
			onChange: onInputChangeHandler,
			onKeyDown: onAddKeyDownHandler,
			value: value,
			autoFocus: true
		}),
		headerError && _react2.default.createElement(
			'div',
			{ className: headerErrorClasses() },
			headerError
		),
		headerInput.map(function (action, index) {
			return getAction(action, index, getInternalInputRef.bind(_this));
		})
	);
}

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

exports.default = HeaderInput;