"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Table = exports.TableProvider = exports.useTableContext = void 0;

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

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

var _Box = require("../Box");

var _PseudoBox = require("../PseudoBox");

var _TablePagination = require("./components/TablePagination");

var _styles = _interopRequireDefault(require("./styles"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;
var TableContext = /*#__PURE__*/(0, _react.createContext)({});

var useTableContext = function useTableContext() {
  var context = (0, _react.useContext)(TableContext);

  if (context == null) {
    throw new Error('This component must be used within the `TableProvider` ');
  }

  return context;
};

exports.useTableContext = useTableContext;

var TableProvider = /*#__PURE__*/_react["default"].forwardRef(function (props, ref) {
  var _tableRef$current, _tableRef$current$get;

  var tableRef = (0, _react.useRef)(null);
  var tableRefWidth = tableRef && ((_tableRef$current = tableRef.current) === null || _tableRef$current === void 0 ? void 0 : (_tableRef$current$get = _tableRef$current.getBoundingClientRect()) === null || _tableRef$current$get === void 0 ? void 0 : _tableRef$current$get.width);

  var _useState = (0, _react.useState)(tableRefWidth),
      tableWidth = _useState[0],
      setTableWidth = _useState[1];

  (0, _react.useEffect)(function () {
    setTableWidth(tableRefWidth);
  }, [tableRefWidth]);
  return __jsx(TableContext.Provider, {
    value: {
      width: tableWidth
    }
  }, __jsx(_Box.Box, {
    ref: tableRef
  }, props.children({
    width: tableWidth
  })));
});

exports.TableProvider = TableProvider;

var Table = function Table(props, ref) {
  var rows = props.rows,
      renderRow = props.renderRow,
      renderHeader = props.renderHeader,
      afterRows = props.afterRows,
      loading = props.loading,
      onPageChange = props.onPageChange,
      cursor = props.cursor,
      height = props.height,
      _props$renderImmediat = props.renderImmediately,
      renderImmediately = _props$renderImmediat === void 0 ? true : _props$renderImmediat;
  var tableContext = useTableContext();
  var width = tableContext === null || tableContext === void 0 ? void 0 : tableContext.width;

  var _useTableStyle = (0, _styles["default"])({
    height: height
  }),
      tableStyleProps = _useTableStyle.table,
      containerStyleProps = _useTableStyle.container,
      footerStyleProps = _useTableStyle.footer;

  if (!width && typeof window !== 'undefined' && !renderImmediately) {
    return null;
  }

  if (rows.constructor !== Array) {
    throw new Error('Table rows must be array type');
  }

  var header = renderHeader();
  return __jsx(_PseudoBox.PseudoBox, containerStyleProps, __jsx(_Box.Box, (0, _extends2["default"])({
    as: "table"
  }, tableStyleProps), header, __jsx(_Box.Box, {
    as: "tbody"
  }, rows.map(function (row, index) {
    return renderRow(row);
  }))), __jsx(_Box.Box, props, afterRows, cursor && onPageChange && __jsx(_TablePagination.TablePagination, (0, _extends2["default"])({
    loading: loading,
    onPageChange: onPageChange,
    cursor: cursor
  }, footerStyleProps))));
};

exports.Table = Table;
//# sourceMappingURL=Table.js.map