'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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

exports.createRow = createRow;
exports.createRows = createRows;

var _getClassName2 = require('babel-plugin-react-css-modules/dist/browser/getClassName');

var _getClassName3 = _interopRequireDefault(_getClassName2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _TableRowColumn = require('./TableRowColumn');

var _TableRowColumn2 = _interopRequireDefault(_TableRowColumn);

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var _styleModuleImportMap = {
  './table.scss': {
    'table-wrapper': 'flive_table_table-wrapper',
    'table': 'flive_table_table',
    'table-row-column': 'flive_table_table-row-column',
    'table--header': 'flive_table_table--header',
    'table--footer': 'flive_table_table--footer',
    'table-row': 'flive_table_table-row',
    'white': 'flive_table_white',
    'grey': 'flive_table_grey',
    'table-row--selected': 'flive_table_table-row--selected',
    'table-row-column--not--selectable': 'flive_table_table-row-column--not--selectable',
    'table-row-column--checkbox': 'flive_table_table-row-column--checkbox',
    'table-row-column--selected': 'flive_table_table-row-column--selected',
    'table-row-column--selectable': 'flive_table_table-row-column--selectable',
    'table-row-column--sortable': 'flive_table_table-row-column--sortable',
    'table-row-column--order-asc': 'flive_table_table-row-column--order-asc',
    'table-row-column--order-desc': 'flive_table_table-row-column--order-desc',
    'table-row-column--sorted': 'flive_table_table-row-column--sorted',
    'table-body': 'flive_table_table-body',
    'wrapper-fixed': 'flive_table_wrapper-fixed',
    'wrapper-scroll': 'flive_table_wrapper-scroll',
    'table--scrolled': 'flive_table_table--scrolled',
    'filter-wrapper': 'flive_table_filter-wrapper',
    'filter': 'flive_table_filter'
  }
};

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var TableRow = function (_PureComponent) {
  _inherits(TableRow, _PureComponent);

  function TableRow() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, TableRow);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = TableRow.__proto__ || Object.getPrototypeOf(TableRow)).call.apply(_ref, [this].concat(args))), _this), _this.onCellClick = function (event, props) {
      var rowNumber = _this.props.rowNumber;

      if (_this.props.onCellClick) {
        _this.props.onCellClick(event, _extends({}, props, { rowNumber: rowNumber }));
      }
      _this.onRowClick(_extends({}, event));
    }, _this.onCheckboxClick = function (event) {
      _this.onRowClick(_extends({}, event, { ctrlKey: true }));
      event.stopPropagation();
    }, _this.onCheckboxChange = function (event) {
      event.stopPropagation();
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(TableRow, [{
    key: 'onRowClick',
    value: function onRowClick(event) {
      if (this.props.selectable && this.props.onRowClick) {
        this.props.onRowClick(event, this.props.rowNumber);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props = this.props,
          rowNumber = _props.rowNumber,
          displayRowCheckbox = _props.displayRowCheckbox,
          adjustForCheckbox = _props.adjustForCheckbox,
          selected = _props.selected,
          selectable = _props.selectable,
          multiSelectable = _props.multiSelectable,
          sortedField = _props.sortedField,
          order = _props.order,
          white = _props.white,
          grey = _props.grey,
          className = _props.className;

      var rowColumns = displayRowCheckbox || adjustForCheckbox ? [_react2.default.createElement(
        _TableRowColumn2.default,
        { checkbox: true },
        displayRowCheckbox && _react2.default.createElement('input', {
          type: 'checkbox',
          checked: selected,
          onClick: this.onCheckboxClick,
          onChange: this.onCheckboxChange
        })
      )] : [];
      var rowColumnsResult = rowColumns.concat(_react2.default.Children.toArray(this.props.children)).map(function (child, columnNumber) {
        return _react2.default.isValidElement(child) ? _react2.default.cloneElement(child, {
          key: rowNumber + '-' + columnNumber, // eslint-disable-line react/no-array-index-key
          onClick: _this2.onCellClick,
          selected: selected,
          columnNumber: columnNumber,
          selectable: selectable,
          multiSelectable: multiSelectable,
          sortedOrder: sortedField === child.props.sort ? order : null
        }) : null;
      });

      return _react2.default.createElement(
        'tr',
        {
          className: (className ? className + ' ' : '') + (0, _getClassName3.default)((0, _classnames2.default)('table-row', { 'table-row--selected': selected }, { white: white }, { grey: grey }), _styleModuleImportMap)
        },
        rowColumnsResult
      );
    }
  }]);

  return TableRow;
}(_react.PureComponent);

TableRow.propTypes = {
  /**
   * Children passed to table row.
   */
  children: _propTypes2.default.node,
  /**
   * @ignore in documentation
   * Controls the display of the row checkbox.
   * is automatically populated when used with TableBody.
   */
  displayRowCheckbox: _propTypes2.default.bool,
  /**
   * @ignore in documentation
   * Controls the display of the row to adjust checkbox.
   * is automatically populated when used with TableHeader or TableFooter.
   */
  adjustForCheckbox: _propTypes2.default.bool,
  /**
   * @ignore
   * Called when a row cell is clicked.
   * rowNumber is the row number and columnId is
   * the column number or the column key.
   */
  onCellClick: _propTypes2.default.func,
  /**
   * @ignore
   * Called when row is clicked.
   */
  onRowClick: _propTypes2.default.func,
  /**
   * Number to identify the row. This property is
   * automatically populated when used with the TableBody component.
   */
  rowNumber: _propTypes2.default.number,
  /**
   * If true, table rows can be selected. If multiple row
   * selection is desired, enable multiSelectable.
   * The default value is true.
   */
  selectable: _propTypes2.default.bool,
  /**
   * Indicates that a particular row is selected.
   * This property can be used to programmatically select rows.
   */
  selected: _propTypes2.default.bool,
  multiSelectable: _propTypes2.default.bool,
  sortedField: _propTypes2.default.array,
  order: _propTypes2.default.string,
  white: _propTypes2.default.bool,
  grey: _propTypes2.default.bool,
  className: _propTypes2.default.string
};
TableRow.defaultProps = {
  children: null,
  hoverable: false,
  onCellClick: null,
  onRowClick: null,
  rowNumber: null,
  selectable: false,
  displayRowCheckbox: false,
  adjustForCheckbox: false,
  selected: false,
  multiSelectable: false,
  sortedField: null,
  order: null,
  white: false,
  grey: false,
  className: undefined
};
function createRow(_ref2) {
  var child = _ref2.child,
      isRowSelected = _ref2.isRowSelected,
      props = _ref2.props,
      index = _ref2.index,
      _ref2$rowNumber = _ref2.rowNumber,
      rowNumber = _ref2$rowNumber === undefined ? 0 : _ref2$rowNumber;

  var _ref3 = props || {},
      onRowClick = _ref3.onRowClick,
      onCellClick = _ref3.onCellClick,
      rest = _objectWithoutProperties(_ref3, ['onRowClick', 'onCellClick']);

  var resultProps = _extends({
    key: 'f-' + rowNumber,
    selected: typeof isRowSelected === 'function' ? isRowSelected(index) : false,
    index: index
  }, rest, {
    rowNumber: rowNumber
  });
  if (child) {
    if (onRowClick) {
      resultProps.onRowClick = function (event, row) {
        onRowClick(event, row);
        if (child.props.onRowClick) {
          child.props.onRowClick(event, row);
        }
      };
    }
    if (onCellClick) {
      resultProps.onCellClick = function (event, row, columnIndex) {
        onCellClick(event, row, columnIndex);
        if (child.props.onCellClick) {
          child.props.onCellClick(event, row);
        }
      };
    }
    if (_react2.default.isValidElement(child)) {
      return _react2.default.cloneElement(child, resultProps);
    }
  }
  return null;
}

function createRows(_ref4) {
  var children = _ref4.children,
      isRowSelected = _ref4.isRowSelected,
      props = _ref4.props;

  var rowNumber = 0;
  return _react2.default.Children.map(children, function (child) {
    var resultChild = createRow({ child: child, isRowSelected: isRowSelected, props: props, rowNumber: rowNumber });
    if (resultChild) {
      rowNumber += 1;
    }
    return resultChild;
  });
}

exports.default = TableRow;