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

var _Toolbar2 = _interopRequireDefault(_Toolbar);

var _Display = require('./Display/Display.propTypes');

var _Display2 = _interopRequireDefault(_Display);

var _DisplayLarge = require('./DisplayLarge');

var _DisplayLarge2 = _interopRequireDefault(_DisplayLarge);

var _DisplayTable = require('./DisplayTable');

var _DisplayTable2 = _interopRequireDefault(_DisplayTable);

var _DisplayTile = require('./DisplayTile');

var _DisplayTile2 = _interopRequireDefault(_DisplayTile);

var _Content = require('./Content');

var _Content2 = _interopRequireDefault(_Content);

var _ListToVirtualizedList = require('./ListToVirtualizedList');

var _ListToVirtualizedList2 = _interopRequireDefault(_ListToVirtualizedList);

var _List = require('./List.scss');

var _List2 = _interopRequireDefault(_List);

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

function ListToolbar(_ref) {
	var id = _ref.id,
	    toolbar = _ref.toolbar,
	    displayMode = _ref.displayMode,
	    list = _ref.list;

	if (!toolbar) {
		return null;
	}

	var toolbarProps = _extends({}, toolbar, {
		id: id
	});

	if (toolbar.display) {
		toolbarProps.display.mode = displayMode;
	}

	if (list.itemProps && list.itemProps.isSelected && list.itemProps.onToggleAll) {
		toolbarProps.selectAllCheckbox = {
			id: id,
			items: list.items,
			isSelected: list.itemProps.isSelected,
			onToggleAll: list.itemProps.onToggleAll
		};
	}
	return _react2.default.createElement(_Toolbar2.default, toolbarProps);
}
ListToolbar.propTypes = {
	id: _react.PropTypes.string,
	displayMode: _react.PropTypes.string,
	list: _react.PropTypes.oneOfType([_react.PropTypes.shape(_Display2.default), _react.PropTypes.shape(_Content2.default.propTypes)]),
	toolbar: _react.PropTypes.shape(_Toolbar2.default.propTypes)
};

function DisplayModeComponent(_ref2) {
	var id = _ref2.id,
	    useContent = _ref2.useContent,
	    displayMode = _ref2.displayMode,
	    list = _ref2.list,
	    virtualized = _ref2.virtualized;

	if (useContent) {
		return _react2.default.createElement(_Content2.default, _extends({
			id: id && id + '-content',
			displayMode: displayMode
		}, list));
	}
	if (virtualized) {
		return _react2.default.createElement(
			'div',
			{ className: 'tc-list-display-virtualized' },
			_react2.default.createElement(_ListToVirtualizedList2.default, _extends({
				id: id,
				displayMode: displayMode
			}, list))
		);
	}
	switch (displayMode) {
		case 'tile':
			return _react2.default.createElement(_DisplayTile2.default, _extends({ id: id }, list));
		case 'large':
			return _react2.default.createElement(_DisplayLarge2.default, _extends({ id: id }, list));
		default:
			return _react2.default.createElement(_DisplayTable2.default, _extends({ id: id }, list));
	}
}
DisplayModeComponent.propTypes = {
	id: _react.PropTypes.string,
	displayMode: _react.PropTypes.string,
	list: _react.PropTypes.oneOfType([_react.PropTypes.shape(_Display2.default), _react.PropTypes.shape(_Content2.default.propTypes)]),
	useContent: _react.PropTypes.bool,
	virtualized: _react.PropTypes.bool
};

function ListDisplay(_ref3) {
	var id = _ref3.id,
	    useContent = _ref3.useContent,
	    displayMode = _ref3.displayMode,
	    list = _ref3.list,
	    virtualized = _ref3.virtualized;

	return _react2.default.createElement(DisplayModeComponent, {
		id: id,
		useContent: useContent,
		displayMode: displayMode,
		list: list,
		virtualized: virtualized
	});
}
ListDisplay.propTypes = DisplayModeComponent.propTypes;

/**
 * @param {object} props react props
 * @example
 const props = {
	displayMode: 'table' / 'large' / 'tile' / component
	list: {
		items: [{}, {}, ...],
		columns: [
			{key, label},
			{key, label},
		]
	},
	toolbar: {
		display: {
			onChange: function,
		},
		sort: {
			field: key,
			options: [
				{key, label},
			],
			isDescending: true / false,
			onChange: function,
		},
		pagination: {
			onChange: function,
			itemsLength: number,
		},
		filter: {
			onFilter: function,
		},
	}
}
 <List {...props}></List>
 */
function List(_ref4) {
	var id = _ref4.id,
	    displayMode = _ref4.displayMode,
	    toolbar = _ref4.toolbar,
	    list = _ref4.list,
	    useContent = _ref4.useContent,
	    virtualized = _ref4.virtualized;

	var classnames = (0, _classnames2.default)('tc-list', _List2.default.list);
	return _react2.default.createElement(
		'div',
		{ className: classnames },
		_react2.default.createElement(ListToolbar, {
			id: id,
			toolbar: toolbar,
			displayMode: displayMode,
			list: list
		}),
		_react2.default.createElement(ListDisplay, {
			id: id,
			useContent: useContent,
			displayMode: displayMode,
			list: list,
			virtualized: virtualized
		})
	);
}

List.propTypes = _extends({}, ListToolbar.propTypes, ListDisplay.propTypes);

List.defaultProps = {
	displayMode: 'table',
	useContent: false
};

exports.default = List;