import { CommonModule } from '@angular/common';
import { InjectionToken, EventEmitter, Optional, Inject, Input, Output, Component, ViewEncapsulation, NgModule } from '@angular/core';
import { coerceNumberProperty } from '@angular/cdk/coercion';

var NtPaginationConfig = (function () {
    function NtPaginationConfig() {
        this.previousLabel = 'Previous';
        this.nextLabel = 'Next';
        this.size = 3;
        this.pageSize = 10;
    }
    return NtPaginationConfig;
}());
var NT_PAGINATION_CONFIG = new InjectionToken('nt-pagination-config');

var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var PAGINATION_ELLIPSIS = '...';
var NtPaginationComponent = (function () {
    function NtPaginationComponent(defaultConfig) {
        this._options = new NtPaginationConfig();
        this._totalPage = 1;
        this._pages = [1];
        this._total = 0;
        this._pageIndex = 1;
        this.pageChange = new EventEmitter();
        this._options = __assign(__assign({}, this._options), defaultConfig || {});
    }
    Object.defineProperty(NtPaginationComponent.prototype, "options", {
        get: function () { return this._options; },
        set: function (value) {
            if (typeof value === 'object') {
                this._options = Object.assign(this._options, value);
            }
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "totalPage", {
        get: function () { return this._totalPage; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "pages", {
        get: function () { return this._pages; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "pageSize", {
        get: function () { return this.options.pageSize; },
        set: function (value) { this._options.pageSize = coerceNumberProperty(value); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "previousLabel", {
        get: function () { return this.options.previousLabel; },
        set: function (value) { this._options.previousLabel = value; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "nextLabel", {
        get: function () { return this.options.nextLabel; },
        set: function (value) { this._options.nextLabel = value; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "total", {
        get: function () { return this._total; },
        set: function (value) { this._total = coerceNumberProperty(value); this._build(); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "pageIndex", {
        get: function () { return this._pageIndex; },
        set: function (value) { this._pageIndex = coerceNumberProperty(value, 1); this._build(); },
        enumerable: true,
        configurable: true
    });
    NtPaginationComponent.prototype._pageChange = function (index) {
        this._pageIndex = coerceNumberProperty(index, 1);
        this.pageChange.emit(index);
    };
    NtPaginationComponent.prototype._build = function () {
        this._totalPage = Math.ceil(this.total / this.options.pageSize);
        var pages = [1];
        if (this._totalPage > 1) {
            var start = this.pageIndex - this.options.size, end = this.pageIndex + this.options.size;
            start = start < 2 ? 2 : start;
            end = end > this.totalPage - 1 ? this.totalPage - 1 : end;
            start - 2 >= 1 && (pages.push(PAGINATION_ELLIPSIS));
            pages = pages.concat(Array(end - start + 1).fill(start).map(function (value, index) { return value + index; }));
            end + 2 <= this.totalPage && (pages.push(PAGINATION_ELLIPSIS));
            this.totalPage > 1 && pages.push(this.totalPage);
        }
        this._pages = pages;
    };
    NtPaginationComponent.ctorParameters = function () { return [
        { type: NtPaginationConfig, decorators: [{ type: Optional }, { type: Inject, args: [NT_PAGINATION_CONFIG,] }] }
    ]; };
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "options", null);
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "pageSize", null);
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "previousLabel", null);
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "nextLabel", null);
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "total", null);
    __decorate([
        Input()
    ], NtPaginationComponent.prototype, "pageIndex", null);
    __decorate([
        Output()
    ], NtPaginationComponent.prototype, "pageChange", void 0);
    NtPaginationComponent = __decorate([
        Component({
            selector: 'nt-pagination, [nt-pagination]',
            template: "<ul class=\"pagination\">\n  <li class=\"pagination-previous disabled\" *ngIf=\"pageIndex <= 1\">{{ previousLabel }}</li>\n  <li class=\"pagination-previous\" *ngIf=\"pageIndex > 1\"><a (click)=\"_pageChange(pageIndex - 1)\">{{ previousLabel }}</a></li>\n\n  <ng-container *ngFor=\"let page of pages\">\n    <li *ngIf=\"page === pageIndex\" [class.current]=\"page === pageIndex\">{{ page }}</li>\n    <li *ngIf=\"page !== pageIndex && page !== '...'\"><a (click)=\"_pageChange(page)\">{{ page }}</a></li>\n    <li class=\"ellipsis\" *ngIf=\"page === '...'\"></li>\n  </ng-container>\n\n  <li class=\"pagination-next disabled\" *ngIf=\"pageIndex >= totalPage\">{{ nextLabel }}</li>\n  <li class=\"pagination-next\" *ngIf=\"pageIndex < totalPage\"><a (click)=\"_pageChange(pageIndex + 1)\">{{ nextLabel }}</a></li>\n</ul>\n",
            encapsulation: ViewEncapsulation.None
        }),
        __param(0, Optional()), __param(0, Inject(NT_PAGINATION_CONFIG))
    ], NtPaginationComponent);
    return NtPaginationComponent;
}());

var __decorate$1 = (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 NtPaginationModule = (function () {
    function NtPaginationModule() {
    }
    NtPaginationModule_1 = NtPaginationModule;
    NtPaginationModule.withConfig = function (config) {
        return {
            ngModule: NtPaginationModule_1,
            providers: [
                { provide: NT_PAGINATION_CONFIG, useValue: config || new NtPaginationConfig() }
            ]
        };
    };
    var NtPaginationModule_1;
    NtPaginationModule = NtPaginationModule_1 = __decorate$1([
        NgModule({
            imports: [CommonModule],
            exports: [NtPaginationComponent],
            declarations: [NtPaginationComponent]
        })
    ], NtPaginationModule);
    return NtPaginationModule;
}());

export { NT_PAGINATION_CONFIG, NtPaginationComponent, NtPaginationConfig, NtPaginationModule, PAGINATION_ELLIPSIS };
//# sourceMappingURL=pagination.js.map
