import { __decorate, __extends } from "tslib";
import Component from 'vue-class-component';
import { Emit, Model, Prop } from 'vue-property-decorator';
import { MediaQueries } from '../../mixins/media-queries/media-queries';
import { FormatMode } from '../../utils/i18n/i18n';
import { ModulVue } from '../../utils/vue/vue';
import { PAGINATION_NAME } from '../component-names';
import { MIconButton } from '../icon-button/icon-button';
import { MIcon } from '../icon/icon';
import { MLink } from '../link/link';
import WithRender from './pagination.html?style=./pagination.scss';
var FIRST_PAGE = 1;
var DELTA_DESKTOP = 4;
var DELTA_MOBILE = 1;
var MPagination = /** @class */ (function (_super) {
    __extends(MPagination, _super);
    function MPagination() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    MPagination.prototype.goToPage = function (value) { };
    Object.defineProperty(MPagination.prototype, "pagesTotal", {
        get: function () {
            return Math.ceil(this.itemsTotal / this.itemsPerPage);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MPagination.prototype, "paginationItems", {
        get: function () {
            var items = [];
            var delta = this.as().isMqMinS ? DELTA_DESKTOP : DELTA_MOBILE;
            var minDelta;
            var maxDelta;
            if (this.value <= FIRST_PAGE + delta) {
                var adjustDelta = (delta * 2) - (this.value - (FIRST_PAGE + 1));
                minDelta = FIRST_PAGE + 1;
                maxDelta = this.value + adjustDelta > this.pagesTotal - 1 ? this.pagesTotal - 1 : this.value + adjustDelta;
            }
            else if (this.value >= this.pagesTotal - delta) {
                var adjustDelta = (delta * 2) - ((this.pagesTotal - 1) - this.value);
                minDelta = this.value - adjustDelta < FIRST_PAGE + 1 ? FIRST_PAGE + 1 : this.value - adjustDelta;
                maxDelta = this.pagesTotal - 1;
            }
            else {
                minDelta = this.value - delta < FIRST_PAGE + 1 ? FIRST_PAGE + 1 : this.value - delta;
                maxDelta = this.value + delta > this.pagesTotal - 1 ? this.pagesTotal - 1 : this.value + delta;
            }
            items.push({ label: FIRST_PAGE.toString(), clickable: this.isPageActive(FIRST_PAGE) ? false : true });
            if (minDelta !== FIRST_PAGE + 1) {
                items.push({ label: this.$i18n.translate('m-pagination:ellipsis'), clickable: false, ellipsis: true });
            }
            for (var i = minDelta; i <= maxDelta; i++) {
                items.push({ label: i.toString(), clickable: this.isPageActive(i) ? false : true });
            }
            if (maxDelta !== this.pagesTotal - 1) {
                items.push({ label: this.$i18n.translate('m-pagination:ellipsis'), clickable: false, ellipsis: true });
            }
            items.push({ label: this.pagesTotal.toString(), clickable: this.isPageActive(this.pagesTotal) ? false : true });
            return items;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MPagination.prototype, "firstPageSelected", {
        get: function () {
            return this.value === 1;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MPagination.prototype, "lastPageSelected", {
        get: function () {
            return this.value === this.pagesTotal;
        },
        enumerable: false,
        configurable: true
    });
    MPagination.prototype.pageFirstItemIndex = function () {
        return ((this.value - 1) * this.itemsPerPage) + 1;
    };
    MPagination.prototype.pageLastItemIndex = function () {
        return Math.min(this.value * this.itemsPerPage, this.itemsTotal);
    };
    Object.defineProperty(MPagination.prototype, "status", {
        get: function () {
            var key = this.itemsTotal <= this.itemsPerPage ?
                'm-pagination:status-single' : 'm-pagination:status';
            return this.$i18n.translate(key, {
                idxFirst: this.pageFirstItemIndex,
                idxLast: this.pageLastItemIndex,
                nbResults: this.itemsTotal
            }, this.itemsTotal, undefined, undefined, FormatMode.Sprintf);
        },
        enumerable: false,
        configurable: true
    });
    MPagination.prototype.isPageActive = function (page) {
        return page === this.value;
    };
    __decorate([
        Model('change'),
        Prop({ default: FIRST_PAGE })
    ], MPagination.prototype, "value", void 0);
    __decorate([
        Prop({ required: true })
    ], MPagination.prototype, "itemsTotal", void 0);
    __decorate([
        Prop({ default: 20 })
    ], MPagination.prototype, "itemsPerPage", void 0);
    __decorate([
        Prop({ default: false })
    ], MPagination.prototype, "loading", void 0);
    __decorate([
        Emit('change')
    ], MPagination.prototype, "goToPage", null);
    MPagination = __decorate([
        WithRender,
        Component({
            components: {
                MIcon: MIcon,
                MLink: MLink,
                MIconButton: MIconButton
            },
            mixins: [MediaQueries]
        })
    ], MPagination);
    return MPagination;
}(ModulVue));
export { MPagination };
var PaginationPlugin = {
    install: function (v, options) {
        v.component(PAGINATION_NAME, MPagination);
    }
};
export default PaginationPlugin;
//# sourceMappingURL=pagination.js.map