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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { MdbTablePaginationComponent, MdbTableDirective, MDBModalService } from 'angular-bootstrap-md';
import { Component, ViewChild, HostListener, ChangeDetectorRef } from '@angular/core';
import { UsersService } from '../services/users.service';
import { ApikeyModalComponent } from '../modals/edit-apikey.component';
import { ModalConfirmComponent } from '../modals/confirm.component';
var TableApiKeysComponent = /** @class */ (function () {
    function TableApiKeysComponent(modalService, cdRef, _usersService) {
        this.modalService = modalService;
        this.cdRef = cdRef;
        this._usersService = _usersService;
        this.previous = [];
        this.searchText = '';
        this.maxVisibleItems = 25;
        this.loading = false;
        this.elements = [];
    }
    TableApiKeysComponent.prototype.oninput = function () {
        this.mdbTablePagination.searchText = this.searchText;
    };
    TableApiKeysComponent.prototype._initElements = function () {
        this.mdbTable.setDataSource(this.elements);
        this.elements = this.mdbTable.getDataSource();
        this.previous = this.mdbTable.getDataSource();
    };
    TableApiKeysComponent.prototype.ngOnInit = function () {
        this.refreshData();
    };
    TableApiKeysComponent.prototype.refreshData = function () {
        var _this = this;
        this.loading = true;
        this.searchText = "";
        var sub = this._usersService.getApiKeys().subscribe(function (res) {
            sub.unsubscribe();
            _this.elements = res.results;
            _this.loading = false;
            _this._initElements();
        });
    };
    TableApiKeysComponent.prototype.ngAfterViewInit = function () {
        this.mdbTablePagination.setMaxVisibleItemsNumberTo(this.maxVisibleItems);
        this.mdbTablePagination.calculateFirstItemIndex();
        this.mdbTablePagination.calculateLastItemIndex();
        this.cdRef.detectChanges();
    };
    TableApiKeysComponent.prototype.searchItems = function () {
        var _this = this;
        var prev = this.mdbTable.getDataSource();
        if (!this.searchText) {
            this.mdbTable.setDataSource(this.previous);
            this.elements = this.mdbTable.getDataSource();
        }
        if (this.searchText) {
            this.elements = this.mdbTable.searchLocalDataBy(this.searchText);
            this.mdbTable.setDataSource(prev);
        }
        this.mdbTablePagination.calculateFirstItemIndex();
        this.mdbTablePagination.calculateLastItemIndex();
        this.mdbTable.searchDataObservable(this.searchText).subscribe(function () {
            _this.mdbTablePagination.calculateFirstItemIndex();
            _this.mdbTablePagination.calculateLastItemIndex();
        });
    };
    TableApiKeysComponent.prototype.openNewApiKeyModal = function (apiKey) {
        var _this = this;
        var modalRef = this.modalService.show(ApikeyModalComponent, {
            class: 'modal-lg modal-notify',
            containerClass: '',
            data: {
                apiKey: apiKey
            }
        });
        var sub = modalRef.content.action.subscribe(function (data) {
            sub.unsubscribe();
            console.log("new api key data", data);
            _this.refreshData();
        });
    };
    TableApiKeysComponent.prototype.openConfirmDeleteModal = function (apiKey) {
        var _this = this;
        var modalRef = this.modalService.show(ModalConfirmComponent, {
            class: 'modal-sm modal-notify modal-danger',
            containerClass: '',
            data: {
                info: {
                    title: 'Confirm revoke?',
                    icon: ''
                }
            }
        });
        var sub = modalRef.content.action.subscribe(function (result) {
            sub.unsubscribe();
            _this._usersService.delApikey(apiKey._id).subscribe(function (res) {
                console.log("done", res);
                _this.refreshData();
            });
        });
    };
    __decorate([
        ViewChild(MdbTablePaginationComponent, { static: true }),
        __metadata("design:type", MdbTablePaginationComponent)
    ], TableApiKeysComponent.prototype, "mdbTablePagination", void 0);
    __decorate([
        ViewChild(MdbTableDirective, { static: true }),
        __metadata("design:type", MdbTableDirective)
    ], TableApiKeysComponent.prototype, "mdbTable", void 0);
    __decorate([
        HostListener('input'),
        __metadata("design:type", Function),
        __metadata("design:paramtypes", []),
        __metadata("design:returntype", void 0)
    ], TableApiKeysComponent.prototype, "oninput", null);
    TableApiKeysComponent = __decorate([
        Component({
            selector: 'table-apikeys',
            templateUrl: './table-apikeys.component.html',
            styleUrls: ['./table-apikeys.component.scss']
        }),
        __metadata("design:paramtypes", [MDBModalService, ChangeDetectorRef, UsersService])
    ], TableApiKeysComponent);
    return TableApiKeysComponent;
}());
export { TableApiKeysComponent };
//# sourceMappingURL=table-apikeys.component.js.map