import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { SoftPaginationButton } from './SoftPaginationButton';
import { SoftButtonIcon } from '../ButtonIcon';
import { SoftI18n } from '../I18n';
const MAX_VISIBLE_PAGES = 5;
const NUMBER_SURROUNDING_PAGES = Math.floor(MAX_VISIBLE_PAGES * 0.5);
export const SoftPagination = ({ className, pageCount, activePage, onPageClick, compressed, ...rest }) => {
    const classes = classNames('softPagination', className);
    const pages = [];
    const firstPageInRange = Math.max(0, Math.min(activePage - NUMBER_SURROUNDING_PAGES, pageCount - MAX_VISIBLE_PAGES));
    const lastPageInRange = Math.min(pageCount, firstPageInRange + MAX_VISIBLE_PAGES);
    for (let i = firstPageInRange, index = 0; i < lastPageInRange; i++, index++) {
        pages.push(React.createElement(SoftI18n, { key: index, token: "softPagination.pageOfTotal", default: "Page {page} of {total}", values: { page: i + 1, total: lastPageInRange } }, pageOfTotal => (React.createElement(SoftPaginationButton, { isActive: i === activePage, onClick: onPageClick.bind(null, i), hideOnMobile: true, "aria-label": pageOfTotal, "data-test-subj": `pagination-button-${i}` }, i + 1))));
    }
    const previousButton = (React.createElement(SoftI18n, { token: "softPagination.previousPage", default: "Previous page" }, previousPage => (React.createElement(SoftButtonIcon, { onClick: onPageClick.bind(null, activePage - 1), iconType: "arrowLeft", disabled: activePage === 0, color: "text", "aria-label": previousPage, "data-test-subj": "pagination-button-previous" }))));
    const firstPageButtons = [];
    if (firstPageInRange > 0) {
        firstPageButtons.push(React.createElement(SoftI18n, { key: "0", token: "softPagination.pageOfTotal", default: "Page {page} of {total}", values: { page: 1, total: lastPageInRange } }, pageOfTotal => (React.createElement(SoftPaginationButton, { onClick: onPageClick.bind(null, 0), hideOnMobile: true, "aria-label": pageOfTotal }, "1"))));
        if (firstPageInRange > 1) {
            firstPageButtons.push(React.createElement(SoftPaginationButton, { key: "beginningEllipsis", isPlaceholder: true, hideOnMobile: true, "aria-hidden": true },
                React.createElement("span", null, "\u2026")));
        }
    }
    const lastPageButtons = [];
    if (lastPageInRange < pageCount) {
        if (lastPageInRange < pageCount - 1) {
            lastPageButtons.push(React.createElement(SoftPaginationButton, { key: "endingEllipsis", isPlaceholder: true, hideOnMobile: true, "aria-hidden": true },
                React.createElement("span", null, "\u2026")));
        }
        lastPageButtons.push(React.createElement(SoftI18n, { key: pageCount - 1, token: "softPagination.jumpToLastPage", default: "Jump to the last page, number {pageCount}", values: { pageCount } }, jumpToLastPage => (React.createElement(SoftPaginationButton, { onClick: onPageClick.bind(null, pageCount - 1), hideOnMobile: true, "aria-label": jumpToLastPage }, pageCount))));
    }
    const nextButton = (React.createElement(SoftI18n, { token: "softPagination.nextPage", default: "Next page" }, nextPage => (React.createElement(SoftButtonIcon, { onClick: onPageClick.bind(null, activePage + 1), iconType: "arrowRight", "aria-label": nextPage, disabled: activePage === pageCount - 1, color: "text", "data-test-subj": "pagination-button-next" }))));
    if (pages.length > 1) {
        const selectablePages = pages;
        if (compressed) {
            return (React.createElement("div", Object.assign({ className: classes }, rest),
                previousButton,
                nextButton));
        }
        else {
            return (React.createElement("div", Object.assign({ 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);
    }
};
// @ts-ignore
SoftPagination.propTypes = {
    className: PropTypes.string,
    /**
     * The total number of pages.
     */
    pageCount: PropTypes.number,
    /**
     * The current page using a zero based index.
     * So if you set the activePage to 1, it will activate the second page.
     */
    activePage: PropTypes.number,
    onPageClick: PropTypes.func,
    /**
     * If true, will only show next/prev arrows instead of page numbers.
     */
    compressed: PropTypes.bool,
};
//# sourceMappingURL=SoftPagination.js.map