'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; };

exports.getCellRenderer = getCellRenderer;
exports.getId = getId;
exports.getColumnData = getColumnData;
exports.getDataKey = getDataKey;
exports.getLabel = getLabel;
exports.getRowData = getRowData;
exports.getCellData = getCellData;
exports.extractTitle = extractTitle;
exports.renderCell = renderCell;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _CellTitle2 = _interopRequireDefault(_CellTitle);

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

function getCellRenderer(field) {
	return field.props.cellRenderer;
}

function getId(parent) {
	return parent.props.id;
}

function getColumnData(parent, field) {
	return _extends({}, field.props.columnData, {
		id: getId(parent)
	});
}

function getDataKey(field) {
	return field.props.dataKey;
}

function getLabel(field) {
	return field.props.label;
}

function getRowData(parent, index) {
	return parent.props.collection[index];
}

function getCellData(field, parent, index) {
	var item = getRowData(parent, index);
	return item[getDataKey(field)];
}

function extractTitle(parent) {
	var children = _react2.default.Children.toArray(parent.props.children);
	var titleField = children.find(function (field) {
		return getCellRenderer(field) === _CellTitle2.default;
	});
	var otherFields = children.filter(function (field) {
		return field !== titleField;
	});

	return { titleField: titleField, otherFields: otherFields };
}

function renderCell(index, parent, field) {
	var cellRenderer = getCellRenderer(field);
	return cellRenderer({
		cellData: getCellData(field, parent, index),
		columnData: getColumnData(parent, field),
		dataKey: getDataKey(field),
		rowData: getRowData(parent, index),
		rowIndex: index
	});
}