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

class NtPaginationConfig {
    constructor() {
        this.previousLabel = '«';
        this.nextLabel = '»';
        this.itemSize = 2;
        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(_changeDetectorRef, defaultConfig) {
        this._changeDetectorRef = _changeDetectorRef;
        this._totalPage = 1;
        this._pageItems = [1];
        this._options = new NtPaginationConfig();
        this._total = 0;
        this._pageIndex = 1;
        this.pageChange = new EventEmitter();
        this._options = Object.assign(Object.assign({}, this._options), defaultConfig || {});
    }
    get totalPage() { return this._totalPage; }
    get pageItems() { return this._pageItems; }
    get options() { return this._options; }
    set options(value) {
        if (typeof value === 'object') {
            this._options = Object.assign({}, this._options, value);
        }
    }
    get itemSize() { return this.options.itemSize; }
    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); }
    get total() { return this._total; }
    set pageIndex(value) { this._pageIndex = coerceNumberProperty(value, 1); }
    get pageIndex() { return this._pageIndex; }
    ngOnInit() {
        this._calcPageItems();
    }
    ngOnChanges(changes) {
        const change = changes.total || changes.pageIndex;
        if (change && !change.firstChange) {
            this._calcPageItems();
        }
    }
    _pageChange(index) {
        this.pageIndex = coerceNumberProperty(index, 1);
        this.pageChange.emit(this.pageIndex);
    }
    _calcPageItems() {
        this._totalPage = Math.ceil(this.total / this.pageSize);
        let pageItems = [1];
        if (this.pageIndex > this._totalPage) {
            this._pageIndex = this._totalPage;
        }
        if (this._totalPage > 1) {
            let [start, end] = [
                Math.max(this.pageIndex - this.itemSize, 2),
                Math.min(this.pageIndex + this.itemSize, this.totalPage - 1)
            ];
            if (start - 2 >= 1) {
                pageItems.push(PAGINATION_ELLIPSIS);
            }
            pageItems = pageItems.concat(Array(end - start + 1).fill(start).map((v, i) => v + i));
            if (end + 2 <= this.totalPage) {
                pageItems.push(PAGINATION_ELLIPSIS);
            }
            if (this.totalPage > 1) {
                pageItems.push(this.totalPage);
            }
        }
        this._pageItems = pageItems;
    }
};
NtPaginationComponent.ctorParameters = () => [
    { type: ChangeDetectorRef },
    { 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',
        template: "<ul class=\"nt-pagination\">\n  <li class=\"nt-pagination-previous\">\n    <a [class.disabled]=\"pageIndex === 1\" (click)=\"_pageChange(pageIndex - 1)\">{{ previousLabel }}</a>\n  </li>\n\n  <ng-container *ngFor=\"let item of pageItems\">\n    <li *ngIf=\"item === pageIndex\" [class.current]=\"item === pageIndex\">{{ item }}</li>\n    <li *ngIf=\"item !== pageIndex && item !== '...'\"><a (click)=\"_pageChange(item)\">{{ item }}</a></li>\n    <li class=\"ellipsis\" *ngIf=\"item === '...'\">{{item}}</li>\n  </ng-container>\n\n  <li class=\"nt-pagination-next\">\n    <a [class.disabled]=\"pageIndex === totalPage\" (click)=\"_pageChange(pageIndex + 1)\">{{ nextLabel }}</a>\n  </li>\n</ul>\n",
        encapsulation: ViewEncapsulation.None,
        changeDetection: ChangeDetectionStrategy.OnPush
    }),
    __param(1, Optional()), __param(1, 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
