"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _at = _interopRequireDefault(require("lodash/at"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

// Style

/* istanbul ignore next */
if (typeof window !== 'undefined') {}

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

  var _ref = Array.isArray(cell) ? [].concat((0, _toConsumableArray2["default"])(cell), rest) : [cell, 0].concat(rest),
      _ref2 = (0, _slicedToArray2["default"])(_ref, 3),
      children = _ref2[0],
      _ref2$ = _ref2[1],
      colspan = _ref2$ === void 0 ? 0 : _ref2$,
      _ref2$2 = _ref2[2],
      align = _ref2$2 === void 0 ? 'left' : _ref2$2;

  return colspan ? /*#__PURE__*/_react["default"].createElement("th", {
    colSpan: colspan
  }, children) : /*#__PURE__*/_react["default"].createElement("th", {
    align: align
  }, children);
};

var TableCell = function TableCell(cell) {
  var align = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'left';
  return /*#__PURE__*/_react["default"].createElement("td", {
    align: align
  }, cell);
};

var Table = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Table, _React$Component);

  var _super = _createSuper(Table);

  function Table() {
    (0, _classCallCheck2["default"])(this, Table);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(Table, [{
    key: "render",
    value: function render() {
      var _this = this;

      var _this$props = this.props,
          head = _this$props.head,
          body = _this$props.body;
      return /*#__PURE__*/_react["default"].createElement("table", {
        className: ['Table', this.props.striped && 'Table_striped'].join(' '),
        style: _objectSpread({
          tableLayout: this.props.layout
        }, this.props.style)
      }, head && head.length && /*#__PURE__*/_react["default"].createElement("thead", null, head.map(function (row, x) {
        return /*#__PURE__*/_react["default"].createElement("tr", {
          key: "th-".concat(x)
        }, row.map(function (d, i) {
          return TableHead(d, _this.props.align[i]);
        }));
      })), /*#__PURE__*/_react["default"].createElement("tbody", null, body.map(function (row, x) {
        return /*#__PURE__*/_react["default"].createElement("tr", {
          key: "tr-".concat(x)
        }, (0, _typeof2["default"])(row) === 'object' && !Array.isArray(row) ? (_this.props.pick.length ? (0, _at["default"])(row, _this.props.pick) : Object.values(row)).map(function (d, i) {
          return TableCell(d, _this.props.align[i]);
        }) : row.map(function (d, i) {
          return TableCell(d, _this.props.align[i]);
        }));
      })));
    }
  }]);
  return Table;
}(_react["default"].Component);

exports["default"] = Table;
(0, _defineProperty2["default"])(Table, "propTypes", {
  align: _propTypes["default"].arrayOf(_propTypes["default"].string),
  attr: _propTypes["default"].object,
  body: _propTypes["default"].oneOfType([_propTypes["default"].object, _propTypes["default"].array]).isRequired,
  head: _propTypes["default"].array,
  layout: _propTypes["default"].oneOf(['fixed', 'auto', 'inherit']),
  pick: _propTypes["default"].array,
  striped: _propTypes["default"].bool
});
(0, _defineProperty2["default"])(Table, "defaultProps", {
  align: [],
  attr: {},
  layout: 'fixed',
  pick: [],
  striped: true
});