import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onPageChange", "page", "total", "pageSize", "showPageOnly"];
import React, { useMemo } from "react";
import PropTypes from "prop-types";
import IconButton from "../../IconButton";
import ChevronLeft from "../../Icon/ChevronLeft";
import ChevronRight from "../../Icon/ChevronRight";
import Flex from "../../CssFlex";
import TextField from "../TextField";

var Pagination = function Pagination(props) {
  var onPageChange = props.onPageChange,
      page = props.page,
      total = props.total,
      pageSize = props.pageSize,
      showPageOnly = props.showPageOnly,
      restProps = _objectWithoutProperties(props, _excluded);

  var lastPage = useMemo(function () {
    return Math.max(0, Math.ceil(total / pageSize) - 1);
  }, [total, pageSize]);
  var options = useMemo(function () {
    return Array.from({
      length: Math.ceil(total / pageSize)
    }).map(function (_, i) {
      var position = (i + 1) * pageSize;
      return {
        label: showPageOnly ? "".concat(i + 1) : "".concat(position - pageSize + 1, " - ").concat(Math.min(position, total)),
        value: i
      };
    });
  }, [total, pageSize]);
  return /*#__PURE__*/React.createElement(Flex, _extends({
    py: "md",
    gap: "sm",
    justifyContent: ["center"]
  }, restProps), /*#__PURE__*/React.createElement(IconButton, {
    icon: /*#__PURE__*/React.createElement(ChevronLeft, null),
    disabled: page === 0,
    onClick: function onClick() {
      return onPageChange(page - 1);
    }
  }), /*#__PURE__*/React.createElement(TextField, {
    select: true,
    value: page,
    disabled: options.length === 1,
    onChange: function onChange(e) {
      onPageChange(Number(e.target.value));
    }
  }, options.map(function (_ref) {
    var value = _ref.value,
        label = _ref.label;
    return /*#__PURE__*/React.createElement("option", {
      key: value,
      value: value
    }, label);
  })), /*#__PURE__*/React.createElement(IconButton, {
    icon: /*#__PURE__*/React.createElement(ChevronRight, null),
    disabled: page === lastPage,
    onClick: function onClick() {
      return onPageChange(page + 1);
    }
  }));
};

Pagination.propTypes = {
  page: PropTypes.number.isRequired,
  total: PropTypes.number.isRequired,
  onPageChange: PropTypes.func.isRequired,
  pageSize: PropTypes.number
};
Pagination.defaultProps = {
  pageSize: 10
};
export default Pagination;