var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

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 _class, _temp;

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

import './style.scss';

import React from 'react';
import PropTypes from 'prop-types';
import loAt from 'lodash/at';

var TableHead = function TableHead(cell) {
  for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    rest[_key - 1] = arguments[_key];
  }

  var _ref = Array.isArray(cell) ? [].concat(cell, rest) : [cell, 0].concat(rest),
      children = _ref[0],
      _ref$ = _ref[1],
      colspan = _ref$ === undefined ? 0 : _ref$,
      _ref$2 = _ref[2],
      align = _ref$2 === undefined ? 'left' : _ref$2;

  return colspan ? React.createElement(
    'th',
    { colSpan: colspan },
    children
  ) : React.createElement(
    'th',
    { align: align },
    children
  );
};

var TableCell = function TableCell(cell) {
  var align = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'left';
  return React.createElement(
    'td',
    { align: align },
    cell
  );
};

var Table = (_temp = _class = function (_React$Component) {
  _inherits(Table, _React$Component);

  function Table() {
    _classCallCheck(this, Table);

    return _possibleConstructorReturn(this, _React$Component.apply(this, arguments));
  }

  Table.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        head = _props.head,
        body = _props.body;

    return React.createElement(
      'table',
      {
        className: ['Table', this.props.striped && 'Table_striped'].join(' '),
        style: _extends({ tableLayout: this.props.layout }, this.props.style)
      },
      head && head.length && React.createElement(
        'thead',
        null,
        head.map(function (row, x) {
          return React.createElement(
            'tr',
            { key: 'th-' + x },
            row.map(function (d, i) {
              return TableHead(d, _this2.props.align[i]);
            })
          );
        })
      ),
      React.createElement(
        'tbody',
        null,
        body.map(function (row, x) {
          return React.createElement(
            'tr',
            { key: 'tr-' + x },
            (typeof row === 'undefined' ? 'undefined' : _typeof(row)) === 'object' && !Array.isArray(row) ? (_this2.props.pick.length ? loAt(row, _this2.props.pick) : Object.values(row)).map(function (d, i) {
              return TableCell(d, _this2.props.align[i]);
            }) : row.map(function (d, i) {
              return TableCell(d, _this2.props.align[i]);
            })
          );
        })
      )
    );
  };

  return Table;
}(React.Component), _class.defaultProps = {
  align: [],
  attr: {},
  layout: 'fixed',
  pick: [],
  striped: true
}, _temp);
export { Table as default };
Table.propTypes = process.env.NODE_ENV !== "production" ? {
  align: PropTypes.arrayOf(PropTypes.string),
  attr: PropTypes.object,
  body: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
  head: PropTypes.array,
  layout: PropTypes.oneOf(['fixed', 'auto', 'inherit']),
  pick: PropTypes.array,
  striped: PropTypes.bool
} : {};