"use strict";

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

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

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

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

var _ChevronLeftIcon = _interopRequireDefault(require("mdi-react/ChevronLeftIcon"));

var _ChevronRightIcon = _interopRequireDefault(require("mdi-react/ChevronRightIcon"));

var _PageFirstIcon = _interopRequireDefault(require("mdi-react/PageFirstIcon"));

var _PageLastIcon = _interopRequireDefault(require("mdi-react/PageLastIcon"));

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

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

var _Button = require("../../../Button");

var _Flex = require("../../../Flex");

var _Spinner = require("../../../Spinner");

var __jsx = _react["default"].createElement;

var TablePagination = function TablePagination(_ref) {
  var loading = _ref.loading,
      onPageChange = _ref.onPageChange,
      cursor = _ref.cursor,
      children = _ref.children,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["loading", "onPageChange", "cursor", "children"]);
  var total = cursor.total,
      perPage = cursor.perPage,
      currentPage = cursor.currentPage;
  var totalPages = cursor && Math.ceil(total / perPage);
  var lastPage = total % perPage > 0 ? Math.floor(total / perPage) + 1 : Math.floor(total / perPage);
  if (!cursor || totalPages <= 1 || !onPageChange) return null;

  if (loading) {
    return __jsx(_Flex.Flex, (0, _extends2["default"])({
      align: "center",
      w: "100%"
    }, props, {
      px: "spacing"
    }), __jsx(_Spinner.Spinner, null));
  }

  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    align: "center",
    justify: "flex-end",
    w: "100%"
  }, props), children, __jsx(_Flex.Flex, {
    whiteSpace: "nowrap",
    align: "center"
  }, __jsx(_Button.Button, {
    onClick: function onClick() {
      onPageChange(1);
    },
    iconOnly: true,
    leftIcon: _PageFirstIcon["default"],
    variant: "unstyled",
    ariaLabel: "First page",
    isDisabled: currentPage === 1
  }), __jsx(_Button.Button, {
    onClick: function onClick() {
      onPageChange(currentPage - 1);
    },
    iconOnly: true,
    leftIcon: _ChevronLeftIcon["default"],
    variant: "unstyled",
    ariaLabel: "Previous page",
    isDisabled: currentPage === 1
  }), __jsx(_Box.Box, {
    mx: 4,
    fontSize: "body"
  }, (currentPage - 1) * perPage + 1, "-", currentPage * perPage, " of ", total), __jsx(_Button.Button, {
    onClick: function onClick() {
      onPageChange(cursor.currentPage + 1);
    },
    iconOnly: true,
    leftIcon: _ChevronRightIcon["default"],
    variant: "unstyled",
    ariaLabel: "Next page",
    isDisabled: currentPage === lastPage
  }), __jsx(_Button.Button, {
    onClick: function onClick() {
      onPageChange(lastPage);
    },
    iconOnly: true,
    leftIcon: _PageLastIcon["default"],
    variant: "unstyled",
    ariaLabel: "Last page",
    isDisabled: currentPage === lastPage
  })));
};

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