'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.NoRow = NoRow;
exports.default = ListGrid;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactVirtualized = require('react-virtualized');

var _RowSelection = require('../RowSelection');

var _RowSelection2 = _interopRequireDefault(_RowSelection);

var _gridrow = require('../utils/gridrow');

var _ListGrid = require('./ListGrid.scss');

var _ListGrid2 = _interopRequireDefault(_ListGrid);

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

function NoRow() {
	return _react2.default.createElement(
		'div',
		{ className: 'tc-list-no-rows' },
		'No rows'
	);
}

function getRowDataFromParent(_ref) {
	var parent = _ref.parent,
	    index = _ref.index;

	return (0, _gridrow.getRowData)(parent, index);
}

/**
 * List renderer that accepts a custom row renderer.
 * The row renderer will create a row element for each collection item.
 */
function ListGrid(props) {
	var children = props.children,
	    collection = props.collection,
	    id = props.id,
	    height = props.height,
	    isSelected = props.isSelected,
	    rowHeight = props.rowHeight,
	    rowRenderer = props.rowRenderer,
	    selectionToggle = props.selectionToggle,
	    width = props.width;


	var enhancedRowRenderer = rowRenderer;
	if (selectionToggle) {
		enhancedRowRenderer = (0, _RowSelection2.default)(rowRenderer, {
			isSelected: isSelected,
			getRowData: getRowDataFromParent
		});
	}

	return _react2.default.createElement(
		_reactVirtualized.List,
		{
			className: _ListGrid2.default['tc-list-list'],
			collection: collection,
			id: id,
			height: height,
			overscanRowCount: 10,
			noRowsRenderer: NoRow,
			rowCount: collection.length,
			rowHeight: rowHeight,
			rowRenderer: enhancedRowRenderer,
			width: width
		},
		children
	);
}

ListGrid.displayName = 'VirtualizedList(ListGrid)';
ListGrid.propTypes = {
	children: _react.PropTypes.arrayOf(_react.PropTypes.element),
	collection: _react.PropTypes.arrayOf(_react.PropTypes.object),
	height: _react.PropTypes.number,
	id: _react.PropTypes.string,
	isSelected: _react.PropTypes.func,
	rowHeight: _react.PropTypes.number,
	rowRenderer: _react.PropTypes.func,
	selectionToggle: _react.PropTypes.func,
	width: _react.PropTypes.number
};

ListGrid.defaultProps = {
	rowHeight: 135
};