"use strict";

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

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _Select = _interopRequireDefault(require("../Select"));

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

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

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

var _ramda = require("ramda");

var _ErrorBoundary = require("../ErrorBoundary");

require("./style.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var defaultRowsPerPage = {
  value: 10,
  label: '10 per page'
};
var rowsPerPageOptions = [defaultRowsPerPage, {
  value: 25,
  label: '25 per page'
}, {
  value: 50,
  label: '50 per page'
}];
var rowsPerPageOptionsMap = (0, _ramda.groupBy)(function (_ref) {
  var value = _ref.value;
  return value;
})(rowsPerPageOptions);

var getRowsPerPageOption = function getRowsPerPageOption(pageValue) {
  return (0, _ramda.pipe)((0, _ramda.prop)(pageValue), _ramda.head)(rowsPerPageOptionsMap);
};

var Pagination = function Pagination(_ref2) {
  var numberOfRows = _ref2.numberOfRows,
      rowsPerPage = _ref2.rowsPerPage,
      page = _ref2.page,
      onChange = _ref2.onChange;
  var maxPage = Math.ceil(numberOfRows / rowsPerPage);

  var handleRowsPerPage = function handleRowsPerPage(_ref3) {
    var nextRowsPerPage = _ref3.value;
    var paginationValue = {
      page: 1,
      rowsPerPage: nextRowsPerPage
    };
    onChange(paginationValue);
  };

  var handlePreviousPage = function handlePreviousPage() {
    var nextPage = page - 1;

    if (nextPage > 0) {
      onChange({
        page: nextPage,
        rowsPerPage: rowsPerPage
      });
    }
  };

  var handleNextPage = function handleNextPage() {
    var nextPage = page + 1;

    if (nextPage <= maxPage) {
      onChange({
        page: nextPage,
        rowsPerPage: rowsPerPage
      });
    }
  };

  return _react.default.createElement("div", {
    className: "vs-pagination"
  }, _react.default.createElement("div", {
    className: "vs-rows-per-page-container"
  }, _react.default.createElement(_Select.default, {
    name: "rows-per-page",
    value: getRowsPerPageOption(rowsPerPage),
    options: rowsPerPageOptions,
    onChange: handleRowsPerPage
  })), _react.default.createElement("div", {
    className: "vs-page-control"
  }, _react.default.createElement(_Button.Button, {
    type: "icon",
    className: "vs-previous-page",
    onClick: handlePreviousPage
  }, _react.default.createElement(_ChevronLeftIcon.default, null)), _react.default.createElement("div", {
    className: "vs-pagination-paging"
  }, page, "/", maxPage), _react.default.createElement(_Button.Button, {
    type: "icon",
    className: "vs-next-page",
    onClick: handleNextPage
  }, _react.default.createElement(_ChevronRightIcon.default, null))), _react.default.createElement("div", {
    className: "vs-pagination-total-records"
  }, numberOfRows, " total records"));
};

Pagination.propTypes = {
  page: _propTypes.default.number,
  rowsPerPage: _propTypes.default.number,
  numberOfRows: _propTypes.default.number.isRequired,
  onChange: _propTypes.default.func.isRequired
};

var _default = (0, _ErrorBoundary.withErrorBoundary)(Pagination);

exports.default = _default;