"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 validRowsPerPageValues = [10, 25, 50];

var getRowsPerPageOptions = function getRowsPerPageOptions(rowsPerPageTemplate) {
  return validRowsPerPageValues.map(function (value) {
    return {
      value: value,
      label: rowsPerPageTemplate.replace('{0}', value)
    };
  });
};

var getRowsPerPageOption = function getRowsPerPageOption(pageValue, rowsPerPageTemplate) {
  return (0, _ramda.pipe)((0, _ramda.groupBy)((0, _ramda.prop)('value')), (0, _ramda.prop)(pageValue), _ramda.head)(getRowsPerPageOptions(rowsPerPageTemplate));
};

var Pagination = function Pagination(_ref) {
  var numberOfRows = _ref.numberOfRows,
      rowsPerPage = _ref.rowsPerPage,
      page = _ref.page,
      onChange = _ref.onChange,
      className = _ref.className,
      _ref$rowsPerPageTempl = _ref.rowsPerPageTemplate,
      rowsPerPageTemplate = _ref$rowsPerPageTempl === void 0 ? '{0} per page' : _ref$rowsPerPageTempl,
      _ref$totalRecordsTemp = _ref.totalRecordsTemplate,
      totalRecordsTemplate = _ref$totalRecordsTemp === void 0 ? '{0} total records' : _ref$totalRecordsTemp;
  var maxPage = Math.ceil(numberOfRows / rowsPerPage) || 1;

  var handleRowsPerPage = function handleRowsPerPage(_ref2) {
    var nextRowsPerPage = _ref2.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
      });
    }
  };

  var getTranslatedNumberOfRecords = function getTranslatedNumberOfRecords() {
    return totalRecordsTemplate.replace('{0}', numberOfRows);
  };

  return _react.default.createElement("div", {
    className: "vs-pagination ".concat(className)
  }, _react.default.createElement("div", {
    className: "vs-rows-per-page-container"
  }, _react.default.createElement(_Select.default, {
    name: "rows-per-page",
    value: getRowsPerPageOption(rowsPerPage, rowsPerPageTemplate),
    options: getRowsPerPageOptions(rowsPerPageTemplate),
    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"
  }, getTranslatedNumberOfRecords()));
};

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;