'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 utility file contains any common functions that are used in row renderers
                                                                                                                                                                                                                                                                   * - get infos from parent props, content field, ...
                                                                                                                                                                                                                                                                   * - render a cell
                                                                                                                                                                                                                                                                   * - ...
                                                                                                                                                                                                                                                                   */

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _CellTitle2 = _interopRequireDefault(_CellTitle);

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

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

/**
 * Get the cell renderer from VirtualizedList.Content configuration
 * @param field The VirtualizedList.Content instance
 */
function getCellRenderer(field) {
  return field.props.cellRenderer;
}

/**
 * Get the id from row parent instance
 * @param parent The row parent instance
 */
function getId(parent) {
  return parent.props.id;
}

/**
 * Enhance VirtualizedList.Content columnData with the row parent id
 * @param parent The row parent instance
 * @param field The VirtualizedList.Content instance
 */
function getColumnData(parent, field) {
  return _extends({}, field.props.columnData, {
    id: getId(parent)
  });
}

/**
 * Get the data property key from VirtualizedList.Content instance
 * @param field The VirtualizedList.Content instance
 */
function getDataKey(field) {
  return field.props.dataKey;
}

/**
 * Get the content label from VirtualizedList.Content instance
 * @param field The VirtualizedList.Content instance
 */
function getLabel(field) {
  return field.props.label;
}

/**
 * Get the collection item
 * @param parent The row parent instance
 * @param index The item index in the collection
 */
function getRowData(parent, index) {
  return parent.props.collection[index];
}

/**
 * Get cell data
 * @param field The VirtualizedList.Content instance
 * @param parent The row instance
 * @param index The item index in collection
 */
function getCellData(field, parent, index) {
  return field.props.cellDataGetter({
    columnData: getColumnData(parent, field),
    dataKey: getDataKey(field),
    rowData: getRowData(parent, index)
  });
}

/**
 * Extract the title VirtualizedList.Content from the other ones
 * @param parent The row parent
 */
function extractSpecialFields(parent) {
  var children = _react2.default.Children.toArray(parent.props.children);
  var titleField = children.find(function (field) {
    return getCellRenderer(field) === _CellTitle2.default;
  });
  var selectionField = children.find(function (field) {
    return field.props.id === _constants.internalIds.rowSelector;
  });
  var otherFields = children.filter(function (field) {
    return field !== titleField && field !== selectionField;
  });

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

/**
 * Render a cell
 * @param index The item index in collection
 * @param parent The row parent instance
 * @param field The VirtualizedList.Content instance
 */
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
  });
}