import { Component, Output, EventEmitter, Input, ViewChild } from '@angular/core';
import { ControlContainer, NgForm } from '@angular/forms';
import { LoginService } from '../login/login.service';
import { UserService } from '@c8y/client';
import { AlertService } from '../alert/alert.service';
import { ModalComponent } from '../modal/modal.component';
import * as i0 from "@angular/core";
import * as i1 from "../login/login.service";
import * as i2 from "@c8y/client";
import * as i3 from "../alert/alert.service";
import * as i4 from "@angular/forms";
import * as i5 from "../forms/form-group.component";
import * as i6 from "../forms/required-input-placeholder.directive";
import * as i7 from "../i18n/c8y-translate.directive";
import * as i8 from "@angular/common";
import * as i9 from "../i18n/c8y-translate.pipe";
export class TotpChallengeComponent {
    constructor(loginService, users, alert) {
        this.loginService = loginService;
        this.users = users;
        this.alert = alert;
        /**
         * Calls the verify endpoint if set to true (default true)
         */
        this.verify = true;
        /**
         * Emits the token on success.
         */
        this.onSuccess = new EventEmitter();
        /**
         * Emits if set up two-factor authentication is canceled.
         */
        this.totpUnconfirmedEmitter = new EventEmitter();
        this.loading = false;
        this.hasError = false;
        this.isModal = false;
        this.model = {
            token: ''
        };
    }
    async verifyCode() {
        try {
            this.loading = true;
            this.hasError = false;
            if (this.verify) {
                await this.users.verifyTotpCode(this.model.token);
            }
            this.onSuccess.emit(this.model.token);
        }
        catch (e) {
            this.hasError = true;
            this.alert.removeLastDanger();
            this.loading = false;
        }
    }
    cancel() {
        this.totpUnconfirmedEmitter.emit();
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: TotpChallengeComponent, deps: [{ token: i1.LoginService }, { token: i2.UserService }, { token: i3.AlertService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: TotpChallengeComponent, selector: "c8y-totp-challenge", inputs: { verify: "verify", loading: "loading", hasError: "hasError", isModal: "isModal" }, outputs: { onSuccess: "onSuccess", totpUnconfirmedEmitter: "totpUnconfirmedEmitter" }, viewQueries: [{ propertyName: "modal", first: true, predicate: ["modal"], descendants: true }], ngImport: i0, template: "<form #totpForm=\"ngForm\" class=\"loginForm\" (ngSubmit)=\"verifyCode()\" novalidate>\n  <div class=\"d-block p-b-0\" [ngClass]=\"isModal ? 'p-24' : 'p-t-24'\">\n    <c8y-form-group\n      [hasError]=\"hasError\"\n      [novalidation]=\"true\"\n      [ngClass]=\"{ 'p-b-24': hasError }\"\n    >\n      <label translate for=\"totpToken\">Verification code</label>\n\n      <input\n        id=\"totpToken\"\n        [(ngModel)]=\"model.token\"\n        name=\"totpToken\"\n        type=\"text\"\n        autofocus\n        autocapitalize=\"off\"\n        autocorrect=\"off\"\n        autocomplete=\"off\"\n        class=\"form-control\"\n        placeholder=\"{{ 'e.g.' | translate }} 624327\"\n        required\n      />\n\n      <p id=\"helpinput\" *ngIf=\"!hasError\" class=\"help-block\" translate>\n        In case of key loss, please contact your platform administrator.\n      </p>\n    </c8y-form-group>\n  </div>\n  <div [ngClass]=\"isModal ? 'modal-footer separator-top bg-level-0 sticky-bottom' : 'text-center'\">\n    <button\n      class=\"btn btn-default\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"button\"\n      title=\"{{ 'Cancel' | translate }}\"\n      (click)=\"cancel()\"\n    >\n      {{ 'Cancel' | translate }}\n    </button>\n    <button\n      class=\"btn btn-primary\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"submit\"\n      title=\"{{ 'Verify' | translate }}\"\n      *ngIf=\"!loading\"\n      [disabled]=\"!totpForm.form.valid\"\n    >\n      {{ 'Verify' | translate }}\n    </button>\n\n    <button\n      class=\"btn btn-primary btn-pending\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"submit\"\n      title=\"{{ 'Verifying\u2026' | translate }}\"\n      *ngIf=\"loading\"\n    >\n      {{ 'Verifying\u2026' | translate }}\n    </button>\n  </div>\n</form>\n", dependencies: [{ kind: "directive", type: i4.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i4.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i4.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i4.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i4.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i4.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i5.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i6.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i7.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i8.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i8.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "pipe", type: i9.C8yTranslatePipe, name: "translate" }], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: TotpChallengeComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-totp-challenge', viewProviders: [{ provide: ControlContainer, useExisting: NgForm }], template: "<form #totpForm=\"ngForm\" class=\"loginForm\" (ngSubmit)=\"verifyCode()\" novalidate>\n  <div class=\"d-block p-b-0\" [ngClass]=\"isModal ? 'p-24' : 'p-t-24'\">\n    <c8y-form-group\n      [hasError]=\"hasError\"\n      [novalidation]=\"true\"\n      [ngClass]=\"{ 'p-b-24': hasError }\"\n    >\n      <label translate for=\"totpToken\">Verification code</label>\n\n      <input\n        id=\"totpToken\"\n        [(ngModel)]=\"model.token\"\n        name=\"totpToken\"\n        type=\"text\"\n        autofocus\n        autocapitalize=\"off\"\n        autocorrect=\"off\"\n        autocomplete=\"off\"\n        class=\"form-control\"\n        placeholder=\"{{ 'e.g.' | translate }} 624327\"\n        required\n      />\n\n      <p id=\"helpinput\" *ngIf=\"!hasError\" class=\"help-block\" translate>\n        In case of key loss, please contact your platform administrator.\n      </p>\n    </c8y-form-group>\n  </div>\n  <div [ngClass]=\"isModal ? 'modal-footer separator-top bg-level-0 sticky-bottom' : 'text-center'\">\n    <button\n      class=\"btn btn-default\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"button\"\n      title=\"{{ 'Cancel' | translate }}\"\n      (click)=\"cancel()\"\n    >\n      {{ 'Cancel' | translate }}\n    </button>\n    <button\n      class=\"btn btn-primary\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"submit\"\n      title=\"{{ 'Verify' | translate }}\"\n      *ngIf=\"!loading\"\n      [disabled]=\"!totpForm.form.valid\"\n    >\n      {{ 'Verify' | translate }}\n    </button>\n\n    <button\n      class=\"btn btn-primary btn-pending\"\n      [ngClass]=\"{ 'btn-lg': !isModal }\"\n      type=\"submit\"\n      title=\"{{ 'Verifying\u2026' | translate }}\"\n      *ngIf=\"loading\"\n    >\n      {{ 'Verifying\u2026' | translate }}\n    </button>\n  </div>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.LoginService }, { type: i2.UserService }, { type: i3.AlertService }], propDecorators: { verify: [{
                type: Input
            }], onSuccess: [{
                type: Output
            }], totpUnconfirmedEmitter: [{
                type: Output
            }], loading: [{
                type: Input
            }], hasError: [{
                type: Input
            }], isModal: [{
                type: Input
            }], modal: [{
                type: ViewChild,
                args: ['modal', { static: false }]
            }] } });
//# sourceMappingURL=data:application/json;base64, 