"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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var core_1 = require("@angular/core");
var lodash_1 = require("lodash");
var cardContainer_1 = require("../../cardContainer");
exports.defaultVisiblePageCount = 5;
var PagerComponent = (function () {
    function PagerComponent(cardContainer) {
        this.cardContainer = cardContainer;
        this.pager = this.cardContainer.dataSource.pager;
    }
    Object.defineProperty(PagerComponent.prototype, "lastPage$", {
        get: function () {
            return this.dataSource.count$.combineLatest(this.pager.pageSize$)
                .map(function (_a) {
                var count = _a[0], pageSize = _a[1];
                return Math.ceil(count / pageSize);
            });
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PagerComponent.prototype, "canGoBack$", {
        get: function () {
            return this.pager.pageNumber$.map(function (pageNumber) { return pageNumber > 1; });
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PagerComponent.prototype, "canGoForward$", {
        get: function () {
            return this.pager.pageNumber$.combineLatest(this.lastPage$)
                .map(function (_a) {
                var pageNumber = _a[0], lastPage = _a[1];
                return pageNumber < lastPage;
            });
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PagerComponent.prototype, "pages$", {
        get: function () {
            var _this = this;
            return this.pager.pageNumber$.combineLatest(this.lastPage$)
                .map(function (_a) {
                var page = _a[0], lastPage = _a[1];
                var nonCurrentVisiblePages = _this.visiblePageCount - 1;
                var before = Math.floor(nonCurrentVisiblePages / 2);
                var after = Math.ceil(nonCurrentVisiblePages / 2);
                var startPage = page - before;
                var endPage = page + after;
                if (startPage < 1) {
                    startPage = 1;
                    endPage = Math.min(_this.visiblePageCount, lastPage);
                }
                else if (endPage > lastPage) {
                    endPage = lastPage;
                    startPage = Math.max(lastPage - nonCurrentVisiblePages, 1);
                }
                return lodash_1.range(startPage, endPage + 1);
            });
        },
        enumerable: true,
        configurable: true
    });
    PagerComponent.prototype.isCurrent = function (page) {
        return this.pager.pageNumber$.map(function (pageNumber) { return page === pageNumber; });
    };
    PagerComponent.prototype.ngOnInit = function () {
        if (this.pager) {
            this.visiblePageCount = this.pageCount != null ? this.pageCount : exports.defaultVisiblePageCount;
            this.dataSource = this.cardContainer.dataSource;
        }
    };
    PagerComponent.prototype.first = function () {
        this.pager.setPage(1);
    };
    PagerComponent.prototype.previous = function () {
        var _this = this;
        this.pager.pageNumber$.take(1).subscribe(function (pageNumber) {
            if (pageNumber > 1) {
                _this.pager.setPage(pageNumber - 1);
            }
        });
    };
    PagerComponent.prototype.goto = function (page) {
        var _this = this;
        this.lastPage$.take(1).subscribe(function (lastPage) {
            if (page >= 1 && page <= lastPage) {
                _this.pager.setPage(page);
            }
        });
    };
    PagerComponent.prototype.next = function () {
        var _this = this;
        this.pager.pageNumber$.combineLatest(this.lastPage$)
            .take(1)
            .subscribe(function (_a) {
            var pageNumber = _a[0], lastPage = _a[1];
            if (pageNumber < lastPage) {
                _this.pager.setPage(pageNumber + 1);
            }
        });
    };
    PagerComponent.prototype.last = function () {
        var _this = this;
        this.lastPage$.take(1).subscribe(function (lastPage) {
            _this.pager.setPage(lastPage);
        });
    };
    return PagerComponent;
}());
__decorate([
    core_1.Input(),
    __metadata("design:type", Number)
], PagerComponent.prototype, "pageCount", void 0);
PagerComponent = __decorate([
    core_1.Component({
        selector: 'rlPager',
        template: require('./pager.html'),
        changeDetection: core_1.ChangeDetectionStrategy.OnPush,
    }),
    __param(0, core_1.Inject(core_1.forwardRef(function () { return cardContainer_1.CardContainerComponent; }))),
    __metadata("design:paramtypes", [cardContainer_1.CardContainerComponent])
], PagerComponent);
exports.PagerComponent = PagerComponent;
//# sourceMappingURL=data:application/json;base64, 