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

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _has = require('lodash/has');

var _has2 = _interopRequireDefault(_has);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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 Table = function (_React$Component) {
  _inherits(Table, _React$Component);

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

    return _possibleConstructorReturn(this, Object.getPrototypeOf(Table).apply(this, arguments));
  }

  _createClass(Table, [{
    key: 'getChildContext',
    value: function getChildContext() {
      return {
        columns: this.props.columns,
        data: this.props.data,
        rowKey: this.props.rowKey
      };
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = // eslint-disable-line no-unused-vars
      this.props;
      var columns = _props.columns;
      var data = _props.data;
      var children = _props.children;

      var props = _objectWithoutProperties(_props, ['columns', 'data', 'children']);

      return _react2.default.createElement(
        'table',
        props,
        children
      );
    }
  }]);

  return Table;
}(_react2.default.Component);

Table.propTypes = {
  columns: _react2.default.PropTypes.arrayOf(_react2.default.PropTypes.shape({
    header: _react2.default.PropTypes.shape({
      label: _react2.default.PropTypes.string,
      transform: _react2.default.PropTypes.func,
      format: _react2.default.PropTypes.func,
      props: _react2.default.PropTypes.object
    }),
    cell: _react2.default.PropTypes.shape({
      property: _react2.default.PropTypes.string,
      transform: _react2.default.PropTypes.func,
      format: _react2.default.PropTypes.func,
      resolve: _react2.default.PropTypes.func,
      props: _react2.default.PropTypes.object
    })
  })).isRequired,
  data: _react2.default.PropTypes.array.isRequired,
  rowKey: _react2.default.PropTypes.string.isRequired,
  children: _react2.default.PropTypes.any
};
Table.childContextTypes = {
  columns: _react2.default.PropTypes.array,
  data: _react2.default.PropTypes.array,
  rowKey: _react2.default.PropTypes.string.isRequired
};

var Header = function Header(_ref, _ref2) {
  var children = _ref.children;
  var className = _ref.className;

  var props = _objectWithoutProperties(_ref, ['children', 'className']);

  var columns = _ref2.columns;
  return _react2.default.createElement(
    'thead',
    props,
    _react2.default.createElement(
      'tr',
      null,
      columns.map(function (column, i) {
        var _ref3 = // eslint-disable-line no-shadow
        column.header || {};

        var label = _ref3.label;
        var _ref3$transform = _ref3.transform;
        var transform = _ref3$transform === undefined ? function (a) {
          return {};
        } : _ref3$transform;
        var _ref3$format = _ref3.format;
        var // eslint-disable-line no-unused-vars
        format = _ref3$format === undefined ? function (a) {
          return a;
        } : _ref3$format;
        var props = _ref3.props;

        var extraParameters = { cellData: label };
        var key = i + '-header';
        var transformed = transform(label, extraParameters);

        if (!transformed) {
          console.warn('Table.Header - Failed to receive a transformed result', transformed); // eslint-disable-line max-len, no-console
        }

        var mergedClassName = mergeClassNames(className, transformed && transformed.className);

        return _react2.default.createElement(
          'th',
          _extends({
            key: key
          }, _extends({}, props, transformed, { className: mergedClassName })),
          transformed.children || format(label, extraParameters)
        );
      })
    ),
    children
  );
};
Header.propTypes = {
  children: _react2.default.PropTypes.any,
  className: _react2.default.PropTypes.string
};
Header.contextTypes = {
  columns: _react2.default.PropTypes.array.isRequired
};
Header.displayName = 'Table.Header';

var Body = function Body(_ref4, _ref5) {
  var row = _ref4.row;
  var className = _ref4.className;

  var props = _objectWithoutProperties(_ref4, ['row', 'className']);

  var columns = _ref5.columns;
  var data = _ref5.data;
  var rowKey = _ref5.rowKey;
  return _react2.default.createElement(
    'tbody',
    props,
    data.map(function (r, i) {
      return _react2.default.createElement(
        'tr',
        _extends({ key: (r[rowKey] || i) + '-row' }, row(r, i)),
        columns.map(function (column, j) {
          var _column$cell = // eslint-disable-line no-shadow
          column.cell;
          var property = _column$cell.property;
          var _column$cell$transfor = _column$cell.transform;
          var transform = _column$cell$transfor === undefined ? function (a) {
            return {};
          } : _column$cell$transfor;
          var _column$cell$format = _column$cell.format;
          var // eslint-disable-line no-unused-vars
          format = _column$cell$format === undefined ? function (a) {
            return a;
          } : _column$cell$format;
          var _column$cell$resolve = _column$cell.resolve;
          var resolve = _column$cell$resolve === undefined ? function (a) {
            return a;
          } : _column$cell$resolve;
          var props = _column$cell.props;

          if (property && !(0, _has2.default)(r, property)) {
            console.warn('Table.Body - Failed to find "' + property + '" property from', r); // eslint-disable-line max-len, no-console
          }

          var extraParameters = { cellData: data[i], property: property };
          var value = (0, _get2.default)(r, property);
          var resolvedValue = resolve(value, extraParameters);
          var transformed = transform(value, extraParameters);

          if (!transformed) {
            console.warn('Table.Body - Failed to receive a transformed result', transformed); // eslint-disable-line max-len, no-console
          }

          var mergedClassName = mergeClassNames(className, transformed && transformed.className);

          return _react2.default.createElement(
            'td',
            _extends({
              key: j + '-cell'
            }, _extends({}, props, transformed, { className: mergedClassName })),
            transformed.children || format(resolvedValue, extraParameters)
          );
        })
      );
    })
  );
};
Body.propTypes = {
  row: _react2.default.PropTypes.func,
  className: _react2.default.PropTypes.string
};
Body.defaultProps = {
  row: function row() {}
};
Body.contextTypes = {
  columns: _react2.default.PropTypes.array.isRequired,
  data: _react2.default.PropTypes.array.isRequired,
  rowKey: _react2.default.PropTypes.string.isRequired
};
Body.displayName = 'Table.Body';

function mergeClassNames(a, b) {
  if (a && b) {
    return a + ' ' + b;
  }

  // Either a or b at this point
  return (a || '') + (b || '');
}

Table.Header = Header;
Table.Body = Body;

exports.default = Table;