"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var factory_1 = require("../factory");
;
var Pagination = /** @class */ (function (_super) {
    tslib_1.__extends(Pagination, _super);
    function Pagination(props) {
        var _this = _super.call(this, props) || this;
        _this.handlePageChange = _this.handlePageChange.bind(_this);
        return _this;
    }
    Pagination.prototype.renderSimple = function () {
        var _a = this.props, activePage = _a.activePage, hasNext = _a.hasNext, onPageChange = _a.onPageChange, cx = _a.classnames;
        return (React.createElement("ul", { className: cx("Pagination", "Pagination--sm") },
            React.createElement("li", { className: cx({
                    disabled: activePage < 2
                }), onClick: activePage < 2 ? function (e) { return e.preventDefault(); } : function () { return onPageChange(activePage - 1); } },
                React.createElement("a", null,
                    React.createElement("i", { className: "fa fa-chevron-left" }))),
            React.createElement("li", { className: cx({
                    disabled: !hasNext
                }), onClick: !hasNext ? function (e) { return e.preventDefault(); } : function () { return onPageChange(activePage + 1); } },
                React.createElement("a", null,
                    React.createElement("i", { className: "fa fa-chevron-right" })))));
    };
    Pagination.prototype.handlePageChange = function (e) {
        var _a = this.props, changePageNum = _a.changePageNum, items = _a.items;
        var value = e.currentTarget.value;
        if ((typeof value === 'number' || /^\d+$/.test(value)) && value > 0 || value === '') {
            if (value !== '') {
                value = parseInt(value, 10);
                value = (value > items ? items : value);
            }
            changePageNum(value);
        }
    };
    Pagination.prototype.renderNormal = function () {
        var _a = this.props, activePage = _a.activePage, items = _a.items, maxButtons = _a.maxButtons, onPageChange = _a.onPageChange, pageNum = _a.pageNum, cx = _a.classnames, showPageInput = _a.showPageInput;
        var pageButtons = [];
        var startPage;
        var endPage;
        if (activePage < (maxButtons - 1) / 2 + 2) {
            maxButtons = activePage + (maxButtons - 1) / 2;
        }
        if (items - activePage < (maxButtons - 1) / 2 + 2) {
            maxButtons = items - activePage + (maxButtons - 1) / 2 + 1;
        }
        if (maxButtons && maxButtons < items) {
            startPage = Math.max(Math.min(activePage - Math.floor(maxButtons / 2), items - maxButtons + 1), 1);
            endPage = startPage + maxButtons - 1;
        }
        else {
            startPage = 1;
            endPage = items;
        }
        var _loop_1 = function (page) {
            pageButtons.push(React.createElement("li", { onClick: function () { return onPageChange(page); }, key: page, className: cx({
                    active: page === activePage
                }) },
                React.createElement("a", { role: "button" }, page)));
        };
        for (var page = startPage; page <= endPage; ++page) {
            _loop_1(page);
        }
        if (startPage > 1) {
            if (startPage > 2) {
                pageButtons.unshift(React.createElement("li", { onClick: function () { return onPageChange(startPage - 1); }, key: "prev-ellipsis" },
                    React.createElement("a", { role: "button" }, "...")));
            }
            pageButtons.unshift(React.createElement("li", { onClick: function () { return onPageChange(1); }, key: 1, className: cx({
                    active: 1 === activePage
                }) },
                React.createElement("a", { role: "button" }, 1)));
        }
        if (endPage < items) {
            if (items - endPage > 1) {
                pageButtons.push(React.createElement("li", { className: cx("Pagination-ellipsis"), onClick: function () { return onPageChange(endPage + 1); }, key: "next-ellipsis" },
                    React.createElement("a", { role: "button" },
                        React.createElement("span", null, "..."))));
            }
            pageButtons.push(React.createElement("li", { onClick: function () { return onPageChange(items); }, key: items, className: cx({
                    active: items === activePage
                }) },
                React.createElement("a", { role: "button" }, items)));
        }
        pageButtons.unshift(React.createElement("li", { className: cx("Pagination-prev", {
                disabled: activePage === 1
            }), onClick: activePage === 1 ? function (e) { return e.preventDefault(); } : function () { return onPageChange(activePage - 1); }, key: "prev" },
            React.createElement("span", null)));
        pageButtons.push(React.createElement("li", { className: cx("Pagination-next", {
                disabled: activePage === items
            }), onClick: activePage === items ? function (e) { return e.preventDefault(); } : function () { return onPageChange(activePage + 1); }, key: "next" },
            React.createElement("span", null)));
        return (React.createElement("div", null,
            React.createElement("ul", { className: cx("Pagination", "Pagination--sm"), onSelect: function (value) { return onPageChange(value); } }, pageButtons),
            items > 9 && showPageInput ? (React.createElement("div", { className: "inline m-l-xs w-xs", key: "toPage" },
                React.createElement("span", { className: cx("Pagination-inputGroup") },
                    React.createElement("input", { type: "text", className: cx("Pagination-input"), onChange: this.handlePageChange, onFocus: function (e) { return e.currentTarget.select(); }, onKeyUp: function (e) { return e.keyCode == 13 && onPageChange(parseInt(e.currentTarget.value, 10)); }, value: pageNum }),
                    React.createElement("span", null,
                        React.createElement("button", { onClick: function () { return onPageChange(pageNum); }, type: "submit", className: cx('Button', 'Button--default') }, "Go"))))) : null));
    };
    Pagination.prototype.render = function () {
        var mode = this.props.mode;
        return mode === 'simple' ? this.renderSimple() : this.renderNormal();
    };
    Pagination.defaultProps = {
        activePage: 1,
        items: 1,
        maxButtons: 5,
        mode: 'normal',
        hasNext: false,
        showPageInput: true
    };
    return Pagination;
}(React.PureComponent));
exports.default = Pagination;
var PaginationRenderer = /** @class */ (function (_super) {
    tslib_1.__extends(PaginationRenderer, _super);
    function PaginationRenderer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    PaginationRenderer = tslib_1.__decorate([
        factory_1.Renderer({
            test: /(^|\/)pagination$/,
            name: 'pagination'
        })
    ], PaginationRenderer);
    return PaginationRenderer;
}(Pagination));
exports.PaginationRenderer = PaginationRenderer;
//# sourceMappingURL=./renderers/Pagination.js.map
