'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; }; /**
                                                                                                                                                                                                                                                                   * This file contains all utility functions that applies to table
                                                                                                                                                                                                                                                                   */


exports.insertSelectionConfiguration = insertSelectionConfiguration;
exports.toColumns = toColumns;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _CellCheckbox = require('../CellCheckbox');

var _CellCheckbox2 = _interopRequireDefault(_CellCheckbox);

var _constants = require('./constants');

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

/**
 * Insert a checkbox column configuration to select a row.
 */
function insertSelectionConfiguration(_ref) {
	var children = _ref.children,
	    isSelected = _ref.isSelected,
	    selectionToggle = _ref.selectionToggle;

	var contentsConfiguration = _react2.default.Children.toArray(children);
	if (selectionToggle) {
		var toggleColumn = _react2.default.createElement(_reactVirtualized.Column, _extends({
			id: _constants.internalIds.rowSelector,
			label: '',
			dataKey: '',
			disableSort: true,
			width: 35,
			flexShrink: 0,
			flexGrow: 0,
			cellDataGetter: function cellDataGetter(_ref2) {
				var rowData = _ref2.rowData;
				return isSelected(rowData);
			},
			columnData: { label: 'Select this element', onChange: selectionToggle }
		}, _CellCheckbox2.default));
		contentsConfiguration = [toggleColumn].concat(contentsConfiguration);
	}
	return contentsConfiguration;
}

/**
 * Create new Columns from children, enhanced with
 * - header and row fixed classnames
 * - parent id (via columnData)
 */
function toColumns(id, theme, children) {
	return _react2.default.Children.toArray(children).map(function (field, index) {
		var colClassName = 'tc-list-cell-' + field.props.dataKey;
		var colProps = _extends({}, field.props, {
			headerClassName: (0, _classnames2.default)(field.props.headerClassName, theme.header, colClassName),
			className: (0, _classnames2.default)(field.props.className, theme.cell, colClassName),
			columnData: _extends({}, field.props.columnData, {
				id: id
			})
		});
		return _react2.default.createElement(_reactVirtualized.Column, _extends({ key: index }, colProps));
	});
}