"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIPagination = void 0;
var custom_paging_1 = require("../elements/custom-paging");
var config_1 = require("../../config");
var aurelia_framework_1 = require("aurelia-framework");
var exports_1 = require("../../exports");
var UIPagination = exports.UIPagination = (function () {
    function UIPagination(taskQueue, config) {
        this.taskQueue = taskQueue;
        this.config = config;
        this.resetPageIfInvalidOnly = false;
        this.displayPages = [];
        this.hideSinglePage = true;
        this.boundaryLinks = true;
        this.directionLinks = true;
        this.boundaryLinks = config.globalConfig.showPaginationBoundaryLinks;
    }
    UIPagination.prototype.bind = function () {
        if (this.currentPage === undefined || this.currentPage === null || this.currentPage < 1) {
            this.currentPage = 1;
        }
        this.calculatePages();
        exports_1.UIInternal.broadcast(custom_paging_1.CUSTOM_PAGING_ATTACHED);
    };
    UIPagination.prototype.totalPagesChanged = function () {
        this.resetCurrentPage();
        this.calculatePages();
    };
    UIPagination.prototype.paginationSizeChanged = function () {
        this.resetCurrentPage();
        this.calculatePages();
    };
    UIPagination.prototype.resetCurrentPage = function () {
        if (false == this.resetPageIfInvalidOnly || this.currentPage > this.totalPages) {
            console.warn('set current page to 1 (ui-pagination)');
            this.currentPage = 1;
        }
    };
    UIPagination.prototype.currentPageChanged = function () {
        this.calculatePages();
    };
    UIPagination.prototype.calculatePages = function () {
        if (this.displayPages == null) {
            this.displayPages = new Array();
        }
        this.displayPages.splice(0);
        if (isNaN(this.paginationSize) || this.totalPages <= this.paginationSize) {
            this.displayAllPages();
        }
        else {
            this.limitVisiblePages();
        }
    };
    UIPagination.prototype.displayAllPages = function () {
        for (var i = 1; i <= this.totalPages; i++) {
            this.displayPages.push({
                title: i.toString(),
                value: i
            });
        }
    };
    UIPagination.prototype.limitVisiblePages = function () {
        var totalTiers = Math.ceil(this.totalPages / this.paginationSize);
        var activeTier = Math.ceil(this.currentPage / this.paginationSize);
        var start = ((activeTier - 1) * this.paginationSize) + 1;
        var end = start + this.paginationSize;
        if (activeTier > 1) {
            this.displayPages.push({
                title: '...',
                value: start - 1
            });
        }
        for (var i = start; i < end; i++) {
            if (i > this.totalPages) {
                break;
            }
            this.displayPages.push({
                title: i.toString(),
                value: i
            });
        }
        if (activeTier < totalTiers) {
            this.displayPages.push({
                title: '...',
                value: end
            });
        }
    };
    UIPagination.prototype.selectPage = function (page) {
        if (page < 1 || page > this.totalPages || page === this.currentPage) {
            return;
        }
        this.currentPage = page;
    };
    UIPagination.prototype.nextPage = function () {
        var page = this.currentPage + 1;
        this.selectPage(page);
    };
    UIPagination.prototype.previousPage = function () {
        var page = this.currentPage - 1;
        this.selectPage(page);
    };
    UIPagination.prototype.firstPage = function () {
        this.selectPage(1);
    };
    UIPagination.prototype.lastPage = function () {
        this.selectPage(this.totalPages);
    };
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Number)
    ], UIPagination.prototype, "currentPage", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Number)
    ], UIPagination.prototype, "totalPages", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Number)
    ], UIPagination.prototype, "paginationSize", void 0);
    __decorate([
        aurelia_framework_1.bindable,
        __metadata("design:type", Boolean)
    ], UIPagination.prototype, "resetPageIfInvalidOnly", void 0);
    UIPagination = __decorate([
        aurelia_framework_1.autoinject,
        (0, aurelia_framework_1.customElement)('ui-pagination'),
        __metadata("design:paramtypes", [aurelia_framework_1.TaskQueue, config_1.Config])
    ], UIPagination);
    return UIPagination;
}());

//# sourceMappingURL=ui-pagination.js.map
