'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 _VirtualizedList = require('../../VirtualizedList');

var _VirtualizedList2 = _interopRequireDefault(_VirtualizedList);

var _CellTitle = require('../../VirtualizedList/CellTitle');

var _CellTitle2 = _interopRequireDefault(_CellTitle);

var _CellActions = require('../../VirtualizedList/CellActions');

var _CellActions2 = _interopRequireDefault(_CellActions);

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

function adaptOnSort(onChange) {
	if (!onChange) {
		return null;
	}
	return function onSortChange(_ref) {
		var sortBy = _ref.sortBy,
		    sortDirection = _ref.sortDirection;

		return onChange(null, { field: sortBy, isDescending: sortDirection === _VirtualizedList.SORT_BY.DESC });
	};
}

function ListToVirtualizedList(props) {
	var id = props.id,
	    items = props.items,
	    columns = props.columns,
	    titleProps = props.titleProps;


	if (!titleProps.actionsKey) {
		titleProps.actionsKey = 'actions';
	}
	// Backward compatibility: find array in object attr:
	var supposedActions = {};
	if (props.items.length > 0) {
		var item = props.items[0];
		Object.keys(item).filter(function (key) {
			return Array.isArray(item[key]);
		}).forEach(function (key) {
			supposedActions[key] = true;
		});
	}
	return _react2.default.createElement(
		_VirtualizedList2.default,
		{
			id: id,
			collection: items,
			type: (props.displayMode || 'TABLE').toUpperCase(),
			sort: adaptOnSort(props.sort && props.sort.onChange),
			sortBy: props.sort && props.sort.field,
			sortDirection: props.sort && props.sort.isDescending ? _VirtualizedList.SORT_BY.DESC : _VirtualizedList.SORT_BY.ASC
		},
		columns.map(function (column, index) {
			var cProps = {
				label: column.label,
				dataKey: column.key
			};
			if (column.key === titleProps.key) {
				_extends(cProps, _CellTitle2.default, {
					columnData: titleProps
				});
			}
			if (supposedActions[column.key]) {
				_extends(cProps, _CellActions2.default);
			}
			return _react2.default.createElement(_VirtualizedList2.default.Content, _extends({ key: index }, cProps));
		})
	);
}

ListToVirtualizedList.propTypes = {
	id: _react.PropTypes.string,
	displayMode: _react.PropTypes.oneOf(['large', 'table']),
	titleProps: _react.PropTypes.shape({
		actionsKey: _react.PropTypes.string,
		key: _react.PropTypes.string
	}),
	items: _react.PropTypes.arrayOf(_react.PropTypes.object),
	columns: _react.PropTypes.arrayOf(_react.PropTypes.object),
	sort: _react.PropTypes.shape({
		onChange: _react.PropTypes.func,
		field: _react.PropTypes.string,
		isDescending: _react.PropTypes.bool
	})
};

exports.default = ListToVirtualizedList;