"use strict";

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

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

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

var _reactIntl = require("react-intl");

var _cspaceInput = require("cspace-input");

var _PageSizeChooser = _interopRequireDefault(require("./PageSizeChooser"));

var _Pager = _interopRequireDefault(require("../../../styles/cspace-ui/Pager.css"));

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }

const {
  MiniButton
} = _cspaceInput.baseComponents;
const messages = (0, _reactIntl.defineMessages)({
  previousLabel: {
    "id": "pager.previous",
    "defaultMessage": "<"
  },
  nextLabel: {
    "id": "pager.next",
    "defaultMessage": ">"
  },
  gap: {
    "id": "pager.gap",
    "defaultMessage": "..."
  }
});
const propTypes = {
  currentPage: _propTypes.default.number.isRequired,
  lastPage: _propTypes.default.number.isRequired,
  pageSize: _propTypes.default.number.isRequired,
  pageSizeOptionListName: _propTypes.default.string,
  windowSize: _propTypes.default.number,
  onPageChange: _propTypes.default.func,
  onPageSizeChange: _propTypes.default.func
};
const defaultProps = {
  windowSize: 5
};

class Pager extends _react.Component {
  constructor() {
    super();
    this.handleNextButtonClick = this.handleNextButtonClick.bind(this);
    this.handlePageButtonClick = this.handlePageButtonClick.bind(this);
    this.handlePrevButtonClick = this.handlePrevButtonClick.bind(this);
  }

  handleNextButtonClick() {
    const {
      currentPage,
      onPageChange
    } = this.props;

    if (onPageChange) {
      onPageChange(currentPage + 1);
    }
  }

  handlePageButtonClick(event) {
    const {
      onPageChange
    } = this.props;

    if (onPageChange) {
      const {
        pagenum: pageNum
      } = event.currentTarget.dataset;
      onPageChange(parseInt(pageNum, 10));
    }
  }

  handlePrevButtonClick() {
    const {
      currentPage,
      onPageChange
    } = this.props;

    if (onPageChange) {
      onPageChange(currentPage - 1);
    }
  }

  renderPages() {
    const {
      currentPage,
      lastPage,
      windowSize
    } = this.props;
    const pageCount = lastPage + 1;
    let windowStart = 0;
    let windowEnd = lastPage;

    if (windowSize < pageCount) {
      let beforeWindowSize = Math.floor((windowSize - 1) / 2);
      let afterWindowSize = windowSize - 1 - beforeWindowSize;

      if (beforeWindowSize > currentPage) {
        const offset = beforeWindowSize - currentPage;
        beforeWindowSize -= offset;
        afterWindowSize += offset;
      } else if (currentPage + afterWindowSize > lastPage) {
        const offset = currentPage + afterWindowSize - lastPage;
        beforeWindowSize += offset;
        afterWindowSize -= offset;
      }

      windowStart = currentPage - beforeWindowSize;
      windowEnd = currentPage + afterWindowSize;
    }

    const pages = [];

    if (windowStart > 0) {
      pages.push({
        pageNum: 0,
        label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          value: 1
        })
      });
      const gap = windowStart;

      if (gap > 2) {
        const gapMiddle = windowStart - Math.floor(windowStart / 2);
        pages.push({
          pageNum: gapMiddle,
          label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.gap)
        });
      } else if (gap > 1) {
        pages.push({
          pageNum: 1,
          label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
            value: 2
          })
        });
      }
    }

    for (let pageNum = windowStart; pageNum <= windowEnd; pageNum += 1) {
      pages.push({
        pageNum,
        label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          value: pageNum + 1
        })
      });
    }

    if (windowEnd < lastPage) {
      const gap = lastPage - windowEnd;

      if (gap > 2) {
        const gapMiddle = windowEnd + Math.ceil((lastPage - windowEnd) / 2);
        pages.push({
          pageNum: gapMiddle,
          label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.gap)
        });
      } else if (gap > 1) {
        pages.push({
          pageNum: lastPage - 1,
          label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
            value: lastPage
          })
        });
      }

      pages.push({
        pageNum: lastPage,
        label: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          value: lastPage + 1
        })
      });
    }

    const items = pages.map(page => /*#__PURE__*/_react.default.createElement("li", {
      key: page.pageNum
    }, /*#__PURE__*/_react.default.createElement(MiniButton, {
      "data-pagenum": page.pageNum,
      disabled: page.pageNum === currentPage,
      onClick: this.handlePageButtonClick
    }, page.label)));
    return /*#__PURE__*/_react.default.createElement("ul", null, items);
  }

  render() {
    const {
      currentPage,
      lastPage,
      pageSize,
      pageSizeOptionListName,
      onPageSizeChange
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _Pager.default.common
    }, /*#__PURE__*/_react.default.createElement(_PageSizeChooser.default, {
      embedded: true,
      pageSize: pageSize,
      pageSizeOptionListName: pageSizeOptionListName,
      onPageSizeChange: onPageSizeChange
    }), /*#__PURE__*/_react.default.createElement("nav", null, /*#__PURE__*/_react.default.createElement(MiniButton, {
      disabled: currentPage === 0,
      onClick: this.handlePrevButtonClick
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.previousLabel)), this.renderPages(), /*#__PURE__*/_react.default.createElement(MiniButton, {
      disabled: currentPage === lastPage,
      onClick: this.handleNextButtonClick
    }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.nextLabel))));
  }

}

exports.default = Pager;
Pager.propTypes = propTypes;
Pager.defaultProps = defaultProps;