'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

var _PageSizeChooser = require('./PageSizeChooser');

var _PageSizeChooser2 = _interopRequireDefault(_PageSizeChooser);

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

var _Pager2 = _interopRequireDefault(_Pager);

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

const MiniButton = _cspaceInput.baseComponents.MiniButton;


const messages = (0, _reactIntl.defineMessages)({
  previousLabel: {
    'id': 'pager.previous',
    'defaultMessage': '<'
  },
  nextLabel: {
    'id': 'pager.next',
    'defaultMessage': '>'
  },
  gap: {
    'id': 'pager.gap',
    'defaultMessage': '...'
  }
});

const propTypes = {
  currentPage: _propTypes2.default.number.isRequired,
  lastPage: _propTypes2.default.number.isRequired,
  pageSize: _propTypes2.default.number.isRequired,
  pageSizeOptionListName: _propTypes2.default.string,
  windowSize: _propTypes2.default.number,
  onPageChange: _propTypes2.default.func,
  onPageSizeChange: _propTypes2.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() {
    var _props = this.props;
    const currentPage = _props.currentPage,
          onPageChange = _props.onPageChange;


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

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


    if (onPageChange) {
      const pageNum = event.currentTarget.dataset.pagenum;


      onPageChange(parseInt(pageNum, 10));
    }
  }

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


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

  renderPages() {
    var _props3 = this.props;
    const currentPage = _props3.currentPage,
          lastPage = _props3.lastPage,
          windowSize = _props3.windowSize;


    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: _react2.default.createElement(_reactIntl.FormattedNumber, { value: 1 })
      });

      const gap = windowStart;

      if (gap > 2) {
        const gapMiddle = windowStart - Math.floor(windowStart / 2);

        pages.push({
          pageNum: gapMiddle,
          label: _react2.default.createElement(_reactIntl.FormattedMessage, messages.gap)
        });
      } else if (gap > 1) {
        pages.push({
          pageNum: 1,
          label: _react2.default.createElement(_reactIntl.FormattedNumber, { value: 2 })
        });
      }
    }

    for (let pageNum = windowStart; pageNum <= windowEnd; pageNum += 1) {
      pages.push({
        pageNum,
        label: _react2.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: _react2.default.createElement(_reactIntl.FormattedMessage, messages.gap)
        });
      } else if (gap > 1) {
        pages.push({
          pageNum: lastPage - 1,
          label: _react2.default.createElement(_reactIntl.FormattedNumber, { value: lastPage })
        });
      }

      pages.push({
        pageNum: lastPage,
        label: _react2.default.createElement(_reactIntl.FormattedNumber, { value: lastPage + 1 })
      });
    }

    const items = pages.map(page => _react2.default.createElement(
      'li',
      { key: page.pageNum },
      _react2.default.createElement(
        MiniButton,
        {
          'data-pagenum': page.pageNum,
          disabled: page.pageNum === currentPage,
          onClick: this.handlePageButtonClick
        },
        page.label
      )
    ));

    return _react2.default.createElement(
      'ul',
      null,
      items
    );
  }

  render() {
    var _props4 = this.props;
    const currentPage = _props4.currentPage,
          lastPage = _props4.lastPage,
          pageSize = _props4.pageSize,
          pageSizeOptionListName = _props4.pageSizeOptionListName,
          onPageSizeChange = _props4.onPageSizeChange;


    return _react2.default.createElement(
      'div',
      { className: _Pager2.default.common },
      _react2.default.createElement(_PageSizeChooser2.default, {
        embedded: true,
        pageSize: pageSize,
        pageSizeOptionListName: pageSizeOptionListName,
        onPageSizeChange: onPageSizeChange
      }),
      _react2.default.createElement(
        'nav',
        null,
        _react2.default.createElement(
          MiniButton,
          {
            disabled: currentPage === 0,
            onClick: this.handlePrevButtonClick
          },
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.previousLabel)
        ),
        this.renderPages(),
        _react2.default.createElement(
          MiniButton,
          {
            disabled: currentPage === lastPage,
            onClick: this.handleNextButtonClick
          },
          _react2.default.createElement(_reactIntl.FormattedMessage, messages.nextLabel)
        )
      )
    );
  }
}

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