import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
var _excluded = ["className", "currentPage", "maxNumPagesVisible", "numPages", "children"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import Box from '../box';
import { TextBody } from '../typography';
import { IconChevronLeftMediumOutline, IconChevronRightMediumOutline } from '@teamleader/ui-icons';
import cx from 'classnames';
import theme from './theme.css';
/** @type {React.ComponentType<any>} */

var Pagination = /*#__PURE__*/function (_PureComponent) {
  _inherits(Pagination, _PureComponent);

  var _super = _createSuper(Pagination);

  function Pagination() {
    _classCallCheck(this, Pagination);

    return _super.apply(this, arguments);
  }

  _createClass(Pagination, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          className = _this$props.className,
          currentPage = _this$props.currentPage,
          maxNumPagesVisible = _this$props.maxNumPagesVisible,
          numPages = _this$props.numPages,
          children = _this$props.children,
          others = _objectWithoutProperties(_this$props, _excluded);

      if (numPages < 2) {
        return null;
      }

      var classNames = cx(theme['pagination'], className);
      var iterator = [currentPage];
      var counter = 0;

      while (iterator.length < maxNumPagesVisible && iterator.length < numPages) {
        if (counter % 2 === 0 && iterator[0] !== 1) {
          iterator.unshift(iterator[0] - 1);
        } else if (counter % 2 === 1 && iterator[iterator.length - 1] !== numPages) {
          iterator.push(iterator[iterator.length - 1] + 1);
        }

        counter++;
      } // make sure the first and the last item are the first and the last page


      iterator[0] = 1;
      iterator[iterator.length - 1] = numPages; // should we replace the 2nd and 2nd last by ellipsis?

      if (iterator.length > 1 && iterator[1] !== 2) {
        iterator[1] = 'ellipsis-left';
      }

      if (iterator.length > 1 && iterator[iterator.length - 2] !== numPages - 1) {
        iterator[iterator.length - 2] = 'ellipsis-right';
      }

      return /*#__PURE__*/React.createElement(Box, _extends({
        "data-teamleader-ui": "pagination",
        className: classNames,
        element: "nav"
      }, others), /*#__PURE__*/React.createElement("ul", {
        className: theme['list']
      }, currentPage > 1 && /*#__PURE__*/React.createElement("li", {
        className: theme['list-item']
      }, children({
        number: currentPage - 1,
        isActive: false,
        icon: /*#__PURE__*/React.createElement(IconChevronLeftMediumOutline, null),
        iconPlacement: 'left'
      })), iterator.map(function (page) {
        var isActive = page === currentPage;
        return /*#__PURE__*/React.createElement("li", {
          key: page,
          className: theme['list-item']
        }, String(page).startsWith('ellipsis') ? /*#__PURE__*/React.createElement(TextBody, {
          className: theme['ellipsis'],
          element: "span"
        }, "...") : children({
          number: page,
          text: page,
          isActive: isActive,
          className: isActive ? theme['is-current'] : ''
        }));
      }), currentPage < numPages && /*#__PURE__*/React.createElement("li", {
        className: theme['list-item']
      }, children({
        number: currentPage + 1,
        isActive: false,
        icon: /*#__PURE__*/React.createElement(IconChevronRightMediumOutline, null),
        iconPlacement: 'right'
      }))));
    }
  }]);

  return Pagination;
}(PureComponent);

Pagination.defaultProps = {
  currentPage: 1,
  maxNumPagesVisible: 7
};
export default Pagination;