'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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _keycode = require('keycode');

var _keycode2 = _interopRequireDefault(_keycode);

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

var _Action2 = _interopRequireDefault(_Action);

var _Item = require('./Item.scss');

var _Item2 = _interopRequireDefault(_Item);

var _Item3 = require('./Item.propTypes');

var _Item4 = _interopRequireDefault(_Item3);

var _ItemEdit = require('./ItemEdit.propTypes');

var _ItemEdit2 = _interopRequireDefault(_ItemEdit);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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 itemClasses(error) {
	var _classNames;

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

function itemErrorClasses() {
	var _classNames2;

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

function itemLabelClasses() {
	var _classNames3;

	return (0, _classnames2.default)((_classNames3 = {}, _defineProperty(_classNames3, _Item2.default['tc-enumeration-item-label'], true), _defineProperty(_classNames3, 'tc-enumeration-item-label', true), _classNames3));
}

function itemEditActionsClasses() {
	var _classNames4;

	return (0, _classnames2.default)((_classNames4 = {}, _defineProperty(_classNames4, _Item2.default['tc-enumeration-item-actions'], true), _defineProperty(_classNames4, 'tc-enumeration-item-actions', true), _classNames4));
}

var ItemEdit = function (_React$Component) {
	_inherits(ItemEdit, _React$Component);

	function ItemEdit(props) {
		_classCallCheck(this, ItemEdit);

		var _this = _possibleConstructorReturn(this, (ItemEdit.__proto__ || Object.getPrototypeOf(ItemEdit)).call(this, props));

		_this.onKeyDown = _this.onKeyDown.bind(_this);
		_this.submit = _this.submit.bind(_this);
		_this.itemChange = _this.itemChange.bind(_this);
		_this.cancel = _this.cancel.bind(_this);
		_this.onActionClick = _this.onActionClick.bind(_this);
		return _this;
	}

	_createClass(ItemEdit, [{
		key: 'componentDidMount',
		value: function componentDidMount() {
			this.itemInput.value = this.props.item[this.props.item.itemProps.key].join(',');
		}
	}, {
		key: 'onKeyDown',
		value: function onKeyDown(event) {
			switch (event.keyCode) {
				case (0, _keycode2.default)('escape'):
					this.cancel(event);
					break;
				case (0, _keycode2.default)('enter'):
					this.submit(event);
					break;
				default:
					break;
			}
		}
	}, {
		key: 'onActionClick',
		value: function onActionClick(event, action) {
			var indexItem = this.props.item.index;
			if (action.onClick) {
				action.onClick(event, {
					value: this.itemInput.value,
					index: indexItem
				});
			}
		}
	}, {
		key: 'getAction',
		value: function getAction(action, index) {
			var _this2 = this;

			return _react2.default.createElement(_Action2.default, {
				key: index,
				label: action.label,
				icon: action.icon,
				disabled: action.disabled,
				onClick: function onClick(event) {
					return _this2.onActionClick(event, action);
				},
				tooltipPlacement: 'bottom',
				inProgress: action.inProgress,
				hideLabel: true,
				link: true
			});
		}
	}, {
		key: 'submit',
		value: function submit(event) {
			return this.props.item.itemProps.onSubmitItem(event, {
				value: event.target.value,
				model: this.props.item,
				index: this.props.item.index
			});
		}
	}, {
		key: 'itemChange',
		value: function itemChange(event) {
			return this.props.item.itemProps.onChangeItem(event, {
				value: event.target.value,
				model: this.props.item,
				index: this.props.item.index
			});
		}
	}, {
		key: 'cancel',
		value: function cancel(event) {
			return this.props.item.itemProps.onAbortItem(event, {
				value: event.target.value,
				model: this.props.item,
				index: this.props.item.index
			});
		}
	}, {
		key: 'render',
		value: function render() {
			var _this3 = this;

			function updateDisabledStatus(action, currentEdit) {
				if (currentEdit[action.id] !== undefined && 'disabled' in currentEdit[action.id]) {
					return _extends({}, action, {
						disabled: currentEdit[action.id].disabled
					});
				}

				return action;
			}

			var editActions = this.props.item.itemProps.actions.map(function (action) {
				return updateDisabledStatus(action, _this3.props.currentEdit);
			});

			return _react2.default.createElement(
				'li',
				{ className: itemClasses(this.props.item.error), id: this.props.id },
				_react2.default.createElement('input', {
					className: itemLabelClasses(),
					ref: function ref(input) {
						_this3.itemInput = input;
					},
					type: 'text',
					onKeyDown: this.onKeyDown,
					onChange: this.itemChange,
					autoFocus: true
				}),
				_react2.default.createElement(
					'div',
					{ className: itemEditActionsClasses() },
					editActions.map(function (action, index) {
						return _this3.getAction(action, index);
					})
				),
				this.props.item.error && _react2.default.createElement(
					'div',
					{ className: itemErrorClasses() },
					this.props.item.error
				)
			);
		}
	}]);

	return ItemEdit;
}(_react2.default.Component);

ItemEdit.propTypes = _extends({}, _Item4.default, _ItemEdit2.default, {
	currentEdit: _react.PropTypes.shape({
		validate: _react.PropTypes.shape({
			disabled: _react.PropTypes.bool
		}),
		abort: _react.PropTypes.shape({
			disabled: _react.PropTypes.bool
		})
	})
});

exports.default = ItemEdit;