'use strict';

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

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 _Item = require('./Item/Item.component');

var _Item2 = _interopRequireDefault(_Item);

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

function itemsClasses() {
	return (0, _classnames2.default)(_Items2.default['tc-listview-items'], 'tc-listview-items');
}

function itemContainer(additionalClassName) {
	return (0, _classnames2.default)(_Items2.default['tc-item-container'], _Items2.default[additionalClassName], 'tc-item-container', additionalClassName);
}

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.renderItem = _this.renderItem.bind(_this);
		_this.getRowHeight = _this.getRowHeight.bind(_this);
		_this.rowRenderer = _this.rowRenderer.bind(_this);
		_this.renderToggleAllOrItem = _this.renderToggleAllOrItem.bind(_this);
		_this.renderToggleAll = _this.renderToggleAll.bind(_this);

		_this.hasToggleAll = _this.props.items.length > 1;
		return _this;
	}

	_createClass(Items, [{
		key: 'getRowHeight',
		value: function getRowHeight(_ref) {
			var index = _ref.index;

			if (this.hasToggleAll && index === 0) {
				return 40;
			}
			return this.props.getItemHeight();
		}
	}, {
		key: 'getRowCount',
		value: function getRowCount() {
			if (this.hasToggleAll) {
				return this.props.items.length + 1;
			}
			return this.props.items.length;
		}
	}, {
		key: 'rowRenderer',
		value: function rowRenderer(props) {
			var key = props.key,
			    index = props.index,
			    style = props.style;

			var isToggle = this.hasToggleAll && index === 0;
			return _react2.default.createElement(
				'div',
				{ className: itemContainer(isToggle && 'toggle'), key: key, style: style },
				this.renderToggleAllOrItem(index)
			);
		}
	}, {
		key: 'renderToggleAllOrItem',
		value: function renderToggleAllOrItem(index) {
			var computedIndex = index;

			if (this.hasToggleAll) {
				if (index === 0) {
					return this.renderToggleAll();
				}
				computedIndex = index - 1;
			}

			return this.renderItem(this.props.items[computedIndex], computedIndex);
		}
	}, {
		key: 'renderToggleAll',
		value: function renderToggleAll() {
			var _props = this.props,
			    toggleAllLabel = _props.toggleAllLabel,
			    toggleAllChecked = _props.toggleAllChecked,
			    onToggleAll = _props.onToggleAll;

			var toggleAllId = 'tc-listview-toggle-all';
			return _react2.default.createElement(
				'div',
				{ className: 'checkbox' },
				_react2.default.createElement(
					'label',
					{ htmlFor: toggleAllId },
					_react2.default.createElement('input', {
						id: toggleAllId,
						type: 'checkbox',
						onChange: onToggleAll,
						checked: !!toggleAllChecked
					}),
					_react2.default.createElement(
						'strong',
						null,
						toggleAllLabel
					)
				)
			);
		}
	}, {
		key: 'renderItem',
		value: function renderItem(item, index) {
			var computedIndex = this.hasToggleAll ? index + 1 : index;
			// affecting index to the item

			return _react2.default.createElement(_Item2.default, {
				key: computedIndex + '-item',
				id: computedIndex + '-item',
				item: item,
				searchCriteria: this.props.searchCriteria
			});
		}
	}, {
		key: 'render',
		value: function render() {
			var _this2 = this;

			return _react2.default.createElement(
				'div',
				{ className: itemsClasses() },
				_react2.default.createElement(
					_reactVirtualized.AutoSizer,
					null,
					function (_ref2) {
						var height = _ref2.height,
						    width = _ref2.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: _this2.props.items,
							className: listClasses(),
							rowRenderer: _this2.rowRenderer,
							width: width,
							height: height,
							rowCount: _this2.getRowCount(),
							rowHeight: _this2.getRowHeight
						});
					}
				)
			);
		}
	}]);

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

Items.propTypes = {
	items: _react.PropTypes.arrayOf(_react.PropTypes.shape({
		label: _react.PropTypes.string,
		onChange: _react.PropTypes.func,
		checked: _react.PropTypes.bool,
		index: _react.PropTypes.number
	})),
	getItemHeight: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.func, _react2.default.PropTypes.number]),
	searchCriteria: _react.PropTypes.string,
	toggleAllChecked: _react.PropTypes.bool,
	toggleAllLabel: _react.PropTypes.string,
	onToggleAll: _react.PropTypes.func
};

exports.default = Items;