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);
};
import { Router } from 'aurelia-router';
import { bindable, autoinject, customElement, observable, TaskQueue } from 'aurelia-framework';
import { UIInternal } from '../../exports';
import { Guid } from './utilities/guid';
export var CustomPaging = (function () {
    function CustomPaging(taskQueue, router) {
        this.taskQueue = taskQueue;
        this.router = router;
        this.customPagingAttached = CUSTOM_PAGING_ATTACHED;
        this.instanceId = new Date().toISOString();
    }
    CustomPaging.prototype.activePageChanged = function (newValue, oldValue) {
        if (newValue !== oldValue) {
            this.goToPage(newValue - 1);
        }
    };
    CustomPaging.prototype.goToPage = function (page) {
        if (this.gridOptions.api != null) {
            this.gridOptions.api.paginationGoToPage(page);
        }
    };
    CustomPaging.prototype.totalPagesChanged = function () {
        var _a, _b;
        if (this.totalPages > 1) {
            var previousPageNbr = this.retrievePreviousPageNbr();
            if (previousPageNbr != null && previousPageNbr <= this.totalPages) {
                (_b = (_a = this.gridOptions) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.paginationGoToPage(previousPageNbr - 1);
            }
        }
    };
    CustomPaging.prototype.bind = function () {
        var _this = this;
        var _a, _b, _c;
        this.activePage = (_c = (_b = (_a = this.gridOptions) === null || _a === void 0 ? void 0 : _a.api) === null || _b === void 0 ? void 0 : _b.paginationGetCurrentPage()) !== null && _c !== void 0 ? _c : 1;
        this.gridOptions.suppressPaginationPanel = true;
        this.gridOptions.onPaginationChanged = function (event) {
            if (event.api != null) {
                _this.totalPages = event.api.paginationGetTotalPages();
                var currentPage = event.api.paginationGetCurrentPage() + 1;
                if (_this.activePage != currentPage) {
                    _this.activePage = currentPage;
                }
                else {
                    UIInternal.broadcast(UIInternal.EVT_PAGINATION_CHANGE);
                    _this.setCurrentSessionPagination(currentPage);
                }
            }
        };
    };
    Object.defineProperty(CustomPaging.prototype, "paginationSessionKey", {
        get: function () {
            var _a, _b, _c, _d;
            (_a = this.paginationSessionIdentifier) !== null && _a !== void 0 ? _a : (this.paginationSessionIdentifier = (_d = (_c = (_b = this.router.currentInstruction) === null || _b === void 0 ? void 0 : _b.config) === null || _c === void 0 ? void 0 : _c.moduleId) !== null && _d !== void 0 ? _d : Guid.newGuid().toString());
            return [LIST_VIEW_BACK_TO_PAGE, this.paginationSessionIdentifier].join('/');
        },
        enumerable: false,
        configurable: true
    });
    CustomPaging.prototype.setCurrentSessionPagination = function (pageNbr) {
        var obj = sessionStorage.getItem(this.paginationSessionKey);
        var sessionPagination;
        if (obj == null) {
            sessionPagination = { instanceId: this.instanceId, currentPageNbr: pageNbr };
        }
        else {
            sessionPagination = JSON.parse(obj);
            if (sessionPagination.instanceId == this.instanceId) {
                sessionPagination.currentPageNbr = pageNbr;
            }
            else {
                sessionPagination = {
                    instanceId: this.instanceId,
                    currentPageNbr: pageNbr,
                    previousPageNbr: sessionPagination.currentPageNbr
                };
            }
        }
        sessionStorage.setItem(this.paginationSessionKey, JSON.stringify(sessionPagination));
    };
    CustomPaging.prototype.retrievePreviousPageNbr = function () {
        var obj = sessionStorage.getItem(this.paginationSessionKey);
        var sessionPagination;
        var pageNbr;
        if (obj != null) {
            sessionPagination = JSON.parse(obj);
            if (sessionPagination.instanceId == this.instanceId) {
                pageNbr = sessionPagination.previousPageNbr;
                sessionPagination.previousPageNbr = null;
                sessionStorage.setItem(this.paginationSessionKey, JSON.stringify(sessionPagination));
            }
            else {
                pageNbr = sessionPagination.currentPageNbr;
                this.setCurrentSessionPagination(pageNbr);
            }
        }
        return pageNbr;
    };
    __decorate([
        bindable,
        __metadata("design:type", Object)
    ], CustomPaging.prototype, "gridOptions", void 0);
    __decorate([
        observable,
        __metadata("design:type", Number)
    ], CustomPaging.prototype, "activePage", void 0);
    __decorate([
        observable,
        __metadata("design:type", Number)
    ], CustomPaging.prototype, "totalPages", void 0);
    CustomPaging = __decorate([
        autoinject,
        customElement('custom-paging'),
        __metadata("design:paramtypes", [TaskQueue, Router])
    ], CustomPaging);
    return CustomPaging;
}());
export var LIST_VIEW_BACK_TO_PAGE = "LIST_VIEW_BACK_TO_PAGE";
export var CUSTOM_PAGING_ATTACHED = "Custom-Paging-Attached";

//# sourceMappingURL=custom-paging.js.map
