'use strict';

exports.__esModule = true;
exports.PagingTable = undefined;

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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _contexts = require('grommet/contexts');

var _StyledPagingTable = require('./StyledPagingTable');

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 responsiveColumns = function responsiveColumns(size, columns) {
  return Array.isArray(columns) ? columns.map(function (column) {
    if (size === undefined) {
      return column;
    }
    var show = column.show;
    if (column.responsiveShow) {
      var sizes = Array.isArray(column.responsiveShow) ? column.responsiveShow : [column.responsiveShow];
      if (sizes.indexOf(size) !== -1) {
        show = true;
      }
    }
    if (column.responsiveHide) {
      var _sizes = Array.isArray(column.responsiveHide) ? column.responsiveHide : [column.responsiveHide];
      if (_sizes.indexOf(size) !== -1) {
        show = false;
      }
    }
    if (show === undefined) {
      return column;
    }
    return _extends({}, column, { show: show });
  }) : columns;
};
var PagingTable = function PagingTable(_ref) {
  var data = _ref.data,
      columns = _ref.columns,
      rest = _objectWithoutProperties(_ref, ['data', 'columns']);

  var defaults = {
    showPagination: rest.onFetchData !== undefined || data && data.length > (rest.defaultPageSize || 20),
    minRows: data && data.length < (rest.defaultPageSize || 20) ? 0 : undefined
  };
  var newProps = _extends({}, defaults, rest);
  return _react2.default.createElement(
    _contexts.ResponsiveContext.Consumer,
    null,
    function (size) {
      return _react2.default.createElement(_StyledPagingTable.StyledPagingTable, _extends({
        data: data || [],
        columns: responsiveColumns(size, columns)
      }, newProps));
    }
  );
};

var PagingTableDoc = void 0;
if (process.env.NODE_ENV !== 'production') {
  PagingTableDoc = require('./doc').default(PagingTable); // eslint-disable-line global-require
}

var PagingTableWrapper = PagingTableDoc || PagingTable;

exports.PagingTable = PagingTableWrapper;