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

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

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); }
};
const PAGINATION_ELLIPSIS = '...';
let NtPaginationComponent = class NtPaginationComponent {
    constructor(defaultConfig) {
        this._options = new NtPaginationConfig();
        this._totalPage = 1;
        this._pages = [1];
        this._total = 0;
        this._pageIndex = 1;
        this.pageChange = new EventEmitter();
        this._options = Object.assign(Object.assign({}, this._options), defaultConfig || {});
    }
    get options() { return this._options; }
    set options(value) {
        if (typeof value === 'object') {
            this._options = Object.assign(this._options, value);
        }
    }
    get totalPage() { return this._totalPage; }
    get pages() { return this._pages; }
    set pageSize(value) { this._options.pageSize = coerceNumberProperty(value); }
    get pageSize() { return this.options.pageSize; }
    set previousLabel(value) { this._options.previousLabel = value; }
    get previousLabel() { return this.options.previousLabel; }
    set nextLabel(value) { this._options.nextLabel = value; }
    get nextLabel() { return this.options.nextLabel; }
    set total(value) { this._total = coerceNumberProperty(value); this._build(); }
    get total() { return this._total; }
    set pageIndex(value) { this._pageIndex = coerceNumberProperty(value, 1); this._build(); }
    get pageIndex() { return this._pageIndex; }
    _pageChange(index) {
        this._pageIndex = coerceNumberProperty(index, 1);
        this.pageChange.emit(index);
    }
    _build() {
        this._totalPage = Math.ceil(this.total / this.options.pageSize);
        let pages = [1];
        if (this._totalPage > 1) {
            let 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((value, index) => value + index));
            end + 2 <= this.totalPage && (pages.push(PAGINATION_ELLIPSIS));
            this.totalPage > 1 && pages.push(this.totalPage);
        }
        this._pages = pages;
    }
};
NtPaginationComponent.ctorParameters = () => [
    { 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);

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

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