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';

var NtPaginationConfig = (function () {
    function NtPaginationConfig() {
        this.previousLabel = '«';
        this.nextLabel = '»';
        this.itemSize = 2;
        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 __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var PAGINATION_ELLIPSIS = '...';
var NtPaginationComponent = (function () {
    function NtPaginationComponent(_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 = __assign(__assign({}, this._options), defaultConfig || {});
    }
    Object.defineProperty(NtPaginationComponent.prototype, "totalPage", {
        get: function () { return this._totalPage; },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "pageItems", {
        get: function () { return this._pageItems; },
        enumerable: true,
        configurable: true
    });
    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, "itemSize", {
        get: function () { return this.options.itemSize; },
        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); },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(NtPaginationComponent.prototype, "pageIndex", {
        get: function () { return this._pageIndex; },
        set: function (value) { this._pageIndex = coerceNumberProperty(value, 1); },
        enumerable: true,
        configurable: true
    });
    NtPaginationComponent.prototype.ngOnInit = function () {
        this._calcPageItems();
    };
    NtPaginationComponent.prototype.ngOnChanges = function (changes) {
        var change = changes.total || changes.pageIndex;
        if (change && !change.firstChange) {
            this._calcPageItems();
        }
    };
    NtPaginationComponent.prototype._pageChange = function (index) {
        this.pageIndex = coerceNumberProperty(index, 1);
        this.pageChange.emit(this.pageIndex);
    };
    NtPaginationComponent.prototype._calcPageItems = function () {
        this._totalPage = Math.ceil(this.total / this.pageSize);
        var pageItems = [1];
        if (this.pageIndex > this._totalPage) {
            this._pageIndex = this._totalPage;
        }
        if (this._totalPage > 1) {
            var _a = __read([
                Math.max(this.pageIndex - this.itemSize, 2),
                Math.min(this.pageIndex + this.itemSize, this.totalPage - 1)
            ], 2), start = _a[0], end = _a[1];
            if (start - 2 >= 1) {
                pageItems.push(PAGINATION_ELLIPSIS);
            }
            pageItems = pageItems.concat(Array(end - start + 1).fill(start).map(function (v, i) { return v + i; }));
            if (end + 2 <= this.totalPage) {
                pageItems.push(PAGINATION_ELLIPSIS);
            }
            if (this.totalPage > 1) {
                pageItems.push(this.totalPage);
            }
        }
        this._pageItems = pageItems;
    };
    NtPaginationComponent.ctorParameters = function () { return [
        { 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);
    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
