'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 _Header = require('./Header/Header.propTypes');

var _Header2 = _interopRequireDefault(_Header);

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

var _ItemEdit2 = _interopRequireDefault(_ItemEdit);

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

var _Action2 = _interopRequireDefault(_Action);

var _Header3 = require('./Header/Header.component');

var _Header4 = _interopRequireDefault(_Header3);

var _HeaderInput = require('./Header/HeaderInput.component');

var _HeaderInput2 = _interopRequireDefault(_HeaderInput);

var _HeaderSelected = require('./Header/HeaderSelected.component');

var _HeaderSelected2 = _interopRequireDefault(_HeaderSelected);

var _Items = require('./Items/Items.component');

var _Items2 = _interopRequireDefault(_Items);

var _Enumeration = require('./Enumeration.scss');

var _Enumeration2 = _interopRequireDefault(_Enumeration);

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

var DISPLAY_MODE_DEFAULT = 'DISPLAY_MODE_DEFAULT';
var DISPLAY_MODE_ADD = 'DISPLAY_MODE_ADD';
var DISPLAY_MODE_SEARCH = 'DISPLAY_MODE_SEARCH';
var DISPLAY_MODE_EDIT = 'DISPLAY_MODE_EDIT';
var DISPLAY_MODE_SELECTED = 'DISPLAY_MODE_SELECTED';

function enumerationClasses() {
	var _classNames;

	return (0, _classnames2.default)((_classNames = {}, _defineProperty(_classNames, _Enumeration2.default['tc-enumeration'], true), _defineProperty(_classNames, 'tc-enumeration', true), _classNames));
}

function Enumeration(props) {
	return _react2.default.createElement(
		'div',
		{ className: enumerationClasses() },
		_react2.default.createElement(HeaderEnumeration, props),
		_react2.default.createElement(ItemsEnumeration, props)
	);
}

Enumeration.propTypes = _extends({
	displayMode: _react.PropTypes.oneOf([DISPLAY_MODE_DEFAULT, DISPLAY_MODE_ADD, DISPLAY_MODE_SELECTED, DISPLAY_MODE_EDIT, DISPLAY_MODE_SEARCH]),
	inputRef: _react.PropTypes.func,
	required: _react.PropTypes.bool,
	headerError: _react.PropTypes.string,
	headerDefault: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Header2.default)).isRequired,
	headerInput: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Header2.default)),
	headerSelected: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Header2.default)),
	items: _react.PropTypes.arrayOf(_react.PropTypes.shape({
		values: _react.PropTypes.arrayOf(_react.PropTypes.string)
	})).isRequired,
	searchCriteria: _react.PropTypes.string,
	itemsProp: _react.PropTypes.shape({
		key: _react.PropTypes.string,
		getItemHeight: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.func, _react2.default.PropTypes.number]),
		onSubmitItem: _react.PropTypes.func,
		onChangeItem: _react.PropTypes.func,
		onSelectItem: _react.PropTypes.func,
		onAbortItem: _react.PropTypes.func,
		onLoadData: _react.PropTypes.func,
		actionsDefault: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Action2.default.propTypes)),
		actionsEdit: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Action2.default.propTypes))
	}).isRequired,
	onInputChange: _react.PropTypes.func.isRequired,
	onAddKeyDown: _react.PropTypes.func,
	inputPlaceholder: _react.PropTypes.string,
	inputValue: _react.PropTypes.string
}, _ItemEdit2.default);

function ItemsEnumeration(_ref) {
	var items = _ref.items,
	    itemsProp = _ref.itemsProp,
	    searchCriteria = _ref.searchCriteria,
	    currentEdit = _ref.currentEdit;

	if (items.length > 0) {
		return _react2.default.createElement(_Items2.default, {
			items: items,
			itemsProp: itemsProp,
			currentEdit: currentEdit,
			searchCriteria: searchCriteria
		});
	}
	return null;
}

ItemsEnumeration.propTypes = _extends({
	items: Enumeration.propTypes.items,
	itemsProp: Enumeration.propTypes.itemsProp,
	searchCriteria: Enumeration.propTypes.searchCriteria
}, _ItemEdit2.default);

function HeaderEnumeration(_ref2) {
	var displayMode = _ref2.displayMode,
	    headerError = _ref2.headerError,
	    onInputChange = _ref2.onInputChange,
	    onAddKeyDown = _ref2.onAddKeyDown,
	    headerInput = _ref2.headerInput,
	    headerDefault = _ref2.headerDefault,
	    headerSelected = _ref2.headerSelected,
	    items = _ref2.items,
	    required = _ref2.required,
	    inputValue = _ref2.inputValue,
	    inputRef = _ref2.inputRef;

	switch (displayMode) {
		case DISPLAY_MODE_SEARCH:
			{
				var propsInput = {
					headerInput: headerInput,
					onInputChange: onInputChange,
					onAddKeyDown: onAddKeyDown,
					headerError: headerError,
					inputRef: inputRef,
					inputPlaceholder: 'Search'
				};
				return _react2.default.createElement(_HeaderInput2.default, propsInput);
			}
		case DISPLAY_MODE_ADD:
			{
				var _propsInput = {
					headerInput: headerInput,
					onInputChange: onInputChange,
					onAddKeyDown: onAddKeyDown,
					headerError: headerError,
					inputRef: inputRef,
					value: inputValue,
					inputPlaceholder: 'New entry'
				};
				return _react2.default.createElement(_HeaderInput2.default, _propsInput);
			}
		case DISPLAY_MODE_DEFAULT:
			{
				var propsDefault = {
					headerDefault: headerDefault,
					required: required
				};

				return _react2.default.createElement(_Header4.default, propsDefault);
			}

		case DISPLAY_MODE_SELECTED:
			{
				var propsSelected = {
					headerSelected: headerSelected,
					nbItemsSelected: items.filter(function (item) {
						return item.isSelected && item.isSelected === true;
					}).length
				};
				return _react2.default.createElement(_HeaderSelected2.default, propsSelected);
			}

		default:
			return null;
	}
}

HeaderEnumeration.propTypes = {
	headerError: Enumeration.propTypes.headerError,
	displayMode: Enumeration.propTypes.displayMode,
	headerInput: Enumeration.propTypes.headerInput,
	headerDefault: Enumeration.propTypes.headerDefault,
	headerSelected: Enumeration.propTypes.headerSelected,
	onInputChange: Enumeration.propTypes.onInputChange,
	onAddKeyDown: Enumeration.propTypes.onAddKeyDown,
	items: Enumeration.propTypes.items,
	required: Enumeration.propTypes.required,
	inputValue: Enumeration.propTypes.inputValue,
	inputRef: Enumeration.propTypes.inputRef
};

exports.default = Enumeration;