import "core-js/modules/es6.object.assign";
import "core-js/modules/web.dom.iterable";
import "core-js/modules/es6.array.iterator";
import "core-js/modules/es6.object.keys";

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { EuiPaginationButton } from './pagination_button';
import { EuiButtonIcon } from '../button';
var MAX_VISIBLE_PAGES = 5;
var NUMBER_SURROUNDING_PAGES = Math.floor(MAX_VISIBLE_PAGES * 0.5);
export var EuiPagination = function EuiPagination(_ref) {
  var className = _ref.className,
      pageCount = _ref.pageCount,
      activePage = _ref.activePage,
      onPageClick = _ref.onPageClick,
      compressed = _ref.compressed,
      rest = _objectWithoutProperties(_ref, ["className", "pageCount", "activePage", "onPageClick", "compressed"]);

  var classes = classNames('euiPagination', className);
  var pages = [];
  var firstPageInRange = Math.max(0, Math.min(activePage - NUMBER_SURROUNDING_PAGES, pageCount - MAX_VISIBLE_PAGES));
  var lastPageInRange = Math.min(pageCount, firstPageInRange + MAX_VISIBLE_PAGES);

  for (var i = firstPageInRange, index = 0; i < lastPageInRange; i++, index++) {
    pages.push(React.createElement(EuiPaginationButton, {
      isActive: i === activePage,
      key: index,
      onClick: onPageClick.bind(null, i),
      hideOnMobile: true,
      "aria-label": "Page ".concat(i + 1, " of ").concat(lastPageInRange),
      "data-test-subj": "pagination-button-".concat(i)
    }, i + 1));
  }

  var previousButton = React.createElement(EuiButtonIcon, {
    onClick: onPageClick.bind(null, activePage - 1),
    iconType: "arrowLeft",
    disabled: activePage === 0,
    color: "text",
    "aria-label": "Previous page",
    "data-test-subj": "pagination-button-previous"
  });
  var firstPageButtons = [];

  if (firstPageInRange > 0) {
    firstPageButtons.push(React.createElement(EuiPaginationButton, {
      key: "0",
      onClick: onPageClick.bind(null, 0),
      hideOnMobile: true,
      "aria-label": "Page 1 of ".concat(lastPageInRange)
    }, "1"));

    if (firstPageInRange > 1) {
      firstPageButtons.push(React.createElement(EuiPaginationButton, {
        key: "beginningEllipsis",
        isPlaceholder: true,
        hideOnMobile: true,
        "aria-hidden": true
      }, React.createElement("span", null, "\u2026")));
    }
  }

  var lastPageButtons = [];

  if (lastPageInRange < pageCount) {
    if (lastPageInRange < pageCount - 1) {
      lastPageButtons.push(React.createElement(EuiPaginationButton, {
        key: "endingEllipsis",
        isPlaceholder: true,
        hideOnMobile: true,
        "aria-hidden": true
      }, React.createElement("span", null, "\u2026")));
    }

    lastPageButtons.push(React.createElement(EuiPaginationButton, {
      key: pageCount - 1,
      onClick: onPageClick.bind(null, pageCount - 1),
      hideOnMobile: true,
      "aria-label": "Jump to the last page, number ".concat(pageCount)
    }, pageCount));
  }

  var nextButton = React.createElement(EuiButtonIcon, {
    onClick: onPageClick.bind(null, activePage + 1),
    iconType: "arrowRight",
    "aria-label": "Next page",
    disabled: activePage === pageCount - 1,
    color: "text",
    "data-test-subj": "pagination-button-next"
  });

  if (pages.length > 1) {
    var selectablePages = pages;

    if (compressed) {
      return React.createElement("div", _extends({
        className: classes
      }, rest), previousButton, nextButton);
    } else {
      return React.createElement("div", _extends({
        className: classes,
        role: "group"
      }, rest), previousButton, firstPageButtons, selectablePages, lastPageButtons, nextButton);
    }
  } else {
    // Don't render pagination if it isn't needed. Then span is here for a docs bug.
    return React.createElement("span", null);
  }
};
EuiPagination.propTypes = {
  className: PropTypes.string,

  /**
   * The total number of pages
   */
  pageCount: PropTypes.number,
  activePage: PropTypes.number,
  onPageClick: PropTypes.func,

  /**
   * If true, will only show next/prev arrows instead of page numbers.
   */
  compressed: PropTypes.bool
};
EuiPagination.__docgenInfo = {
  "description": "",
  "methods": [],
  "displayName": "EuiPagination",
  "props": {
    "className": {
      "type": {
        "name": "string"
      },
      "required": false,
      "description": ""
    },
    "pageCount": {
      "type": {
        "name": "number"
      },
      "required": false,
      "description": "The total number of pages"
    },
    "activePage": {
      "type": {
        "name": "number"
      },
      "required": false,
      "description": ""
    },
    "onPageClick": {
      "type": {
        "name": "func"
      },
      "required": false,
      "description": ""
    },
    "compressed": {
      "type": {
        "name": "bool"
      },
      "required": false,
      "description": "If true, will only show next/prev arrows instead of page numbers."
    }
  }
};