'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 _reactVirtualized = require('react-virtualized');

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

var _Action2 = _interopRequireDefault(_Action);

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

var _Item2 = _interopRequireDefault(_Item);

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

var _ItemEdit2 = _interopRequireDefault(_ItemEdit);

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

var _ItemEdit4 = _interopRequireDefault(_ItemEdit3);

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

var _Items2 = _interopRequireDefault(_Items);

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 listClasses() {
	return (0, _classnames2.default)(_defineProperty({}, _Items2.default['tc-list-items'], true));
}

function itemsClasses() {
	var _classNames2;

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

function itemContainer() {
	var _classNames3;

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

var DISPLAY_MODE_EDIT = 'DISPLAY_MODE_EDIT';

var virtualizedListClassName = 'ReactVirtualized__List';

var Items = function (_React$PureComponent) {
	_inherits(Items, _React$PureComponent);

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

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

		_this.lazyLoadingTimer = null;
		_this.getItem = _this.getItem.bind(_this);
		_this.getRowHeight = _this.getRowHeight.bind(_this);
		_this.rowRenderer = _this.rowRenderer.bind(_this);
		_this.scrollEnumeration = _this.scrollEnumeration.bind(_this);
		return _this;
	}

	_createClass(Items, [{
		key: 'getItem',
		value: function getItem(item, index) {
			// affecting index to the item
			var itemWithIndex = _extends({}, item, {
				index: index
			});

			switch (item.displayMode) {
				case DISPLAY_MODE_EDIT:
					{
						var itemPropsEdit = {
							key: this.props.itemsProp.key,
							actions: this.props.itemsProp.actionsEdit,
							onSubmitItem: this.props.itemsProp.onSubmitItem,
							onAbortItem: this.props.itemsProp.onAbortItem,
							onChangeItem: this.props.itemsProp.onChangeItem
						};
						itemWithIndex.itemProps = itemPropsEdit;

						return _react2.default.createElement(_ItemEdit2.default, {
							key: index + '-item',
							id: index + '-item',
							item: itemWithIndex,
							currentEdit: this.props.currentEdit
						});
					}
				default:
					{
						var itemPropDefault = {
							key: this.props.itemsProp.key,
							actions: this.props.itemsProp.actionsDefault,
							onSelectItem: this.props.itemsProp.onSelectItem
						};
						itemWithIndex.itemProps = itemPropDefault;

						return _react2.default.createElement(_Item2.default, {
							key: index + '-item',
							id: index + '-item',
							item: itemWithIndex,
							itemProps: itemPropDefault,
							searchCriteria: this.props.searchCriteria
						});
					}
			}
		}
	}, {
		key: 'getRowHeight',
		value: function getRowHeight(_ref) {
			var index = _ref.index;

			var isEditMode = this.props.items[index].displayMode === DISPLAY_MODE_EDIT;
			return this.props.itemsProp.getItemHeight(isEditMode);
		}
	}, {
		key: 'scrollEnumeration',
		value: function scrollEnumeration(event) {
			var _this2 = this;

			// needed because of React's event pooling
			event.persist();
			if (this.lazyLoadingTimer !== null) {
				clearTimeout(this.lazyLoadingTimer);
			}

			this.lazyLoadingTimer = setTimeout(function () {
				// react-virtualized fire scroll events not to be considered
				if (event.target.className.includes(virtualizedListClassName) && event.target.scrollTop + event.target.clientHeight >= event.target.scrollHeight) {
					_this2.props.itemsProp.onLoadData();
				}
			}, 500);
		}
	}, {
		key: 'rowRenderer',
		value: function rowRenderer(_ref2) {
			var key = _ref2.key,
			    index = _ref2.index,
			    style = _ref2.style;

			return _react2.default.createElement(
				'div',
				{ className: itemContainer(), key: key, style: style },
				this.getItem(this.props.items[index], index)
			);
		}
	}, {
		key: 'render',
		value: function render() {
			var _this3 = this;

			var actions = this.props.itemsProp && this.props.itemsProp.actionsDefault;
			return _react2.default.createElement(
				'ul',
				{ className: itemsClasses(), onScroll: this.scrollEnumeration },
				_react2.default.createElement(
					_reactVirtualized.AutoSizer,
					null,
					function (_ref3) {
						var height = _ref3.height,
						    width = _ref3.width;
						return _react2.default.createElement(_reactVirtualized.List
						/**
       * The props 'items' and 'actions' does not exist in <List>
       * but only way to refresh component when items or actions change
       * See https://github.com/bvaughn/react-virtualized/#pure-components
       */
						, { items: _this3.props.items,
							actions: actions,
							className: listClasses(),
							rowRenderer: _this3.rowRenderer,
							width: width,
							height: height,
							rowCount: _this3.props.items.length,
							rowHeight: _this3.getRowHeight
						});
					}
				)
			);
		}
	}]);

	return Items;
}(_react2.default.PureComponent);

Items.propTypes = _extends({
	items: _react.PropTypes.arrayOf(_react.PropTypes.shape({
		values: _react.PropTypes.arrayOf(_react.PropTypes.string)
	})),
	searchCriteria: _react.PropTypes.string,
	itemsProp: _react.PropTypes.shape({
		key: _react.PropTypes.string.isRequired,
		getItemHeight: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.func, _react2.default.PropTypes.number]),
		onSubmitItem: _react.PropTypes.func,
		onAbortItem: _react.PropTypes.func,
		onSelectItem: _react.PropTypes.func,
		actionsDefault: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Action2.default.propTypes)),
		actionsEdit: _react.PropTypes.arrayOf(_react.PropTypes.shape(_Action2.default.propTypes))
	}).isRequired
}, _ItemEdit4.default);

exports.default = Items;