import { Component, Output, EventEmitter, Input } from '@angular/core';
import { LoginService } from '../login/login.service';
import { AlertService } from '../alert/alert.service';
import { LoginViews } from '../login/login.model';
import { UserService } from '@c8y/client';
import * as i0 from "@angular/core";
import * as i1 from "../login/login.service";
import * as i2 from "../alert/alert.service";
import * as i3 from "@c8y/client";
import * as i4 from "../forms/phone-validation.directive";
import * as i5 from "@angular/forms";
import * as i6 from "../forms/form-group.component";
import * as i7 from "../forms/required-input-placeholder.directive";
import * as i8 from "../i18n/c8y-translate.directive";
import * as i9 from "@angular/common";
import * as i10 from "../i18n/c8y-translate.pipe";
export class ProvidePhoneNumberComponent {
    constructor(loginService, alert, userService) {
        this.loginService = loginService;
        this.alert = alert;
        this.userService = userService;
        this.onCancel = new EventEmitter();
        this.onChangeView = new EventEmitter();
        this.requestInProgress = false;
        this.sendTfa = '0';
    }
    async save() {
        try {
            this.requestInProgress = true;
            await this.userService.savePhoneNumber(this.phoneNumber);
            await this.sendTFASms();
            this.onChangeView.emit({
                view: LoginViews.SmsChallenge,
                credentials: this.credentials
            });
        }
        catch (e) {
            this.alert.addServerFailure(e);
        }
        finally {
            this.requestInProgress = false;
        }
    }
    async sendTFASms() {
        try {
            await this.userService.verifyTFACode(this.sendTfa);
        }
        catch (e) {
            if (e.res.status === 403) {
                this.loginService.cleanMessages();
                this.loginService.addSuccessMessage('send_sms');
            }
            else {
                throw e;
            }
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ProvidePhoneNumberComponent, deps: [{ token: i1.LoginService }, { token: i2.AlertService }, { token: i3.UserService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: ProvidePhoneNumberComponent, selector: "c8y-provide-phone-number", inputs: { credentials: "credentials" }, outputs: { onCancel: "onCancel", onChangeView: "onChangeView" }, ngImport: i0, template: "<form #twoFactorForm=\"ngForm\" class=\"loginForm\" (ngSubmit)=\"save()\" novalidate>\n  <div class=\"legend form-block center\" translate>Two-factor authentication</div>\n\n  <c8y-form-group [ngClass]=\"requestInProgress || twoFactorForm.invalid ? 'p-b-8' : ''\">\n    <label translate>Provide your phone number</label>\n\n    <input\n      class=\"form-control\"\n      [(ngModel)]=\"phoneNumber\"\n      #contactPhone=\"ngModel\"\n      type=\"text\"\n      name=\"phone\"\n      autocomplete=\"off\"\n      placeholder=\"{{ 'e.g. +49 9 876 543 210`LOCALIZE`' | translate }}\"\n      c8yPhoneValidation\n      required\n    />\n  </c8y-form-group>\n\n  <button\n    title=\"{{ 'Save and continue' | translate }}\"\n    type=\"submit\"\n    class=\"btn btn-primary btn-lg btn-block form-group\"\n    [disabled]=\"requestInProgress || twoFactorForm.invalid\"\n  >\n    {{ 'Save and continue' | translate }}\n  </button>\n\n  <div class=\"d-flex m-t-8\">\n    <a\n      title=\"{{ 'Login' | translate }}\"\n      class=\"small pointer m-l-auto\"\n      href=\"#\"\n      (click)=\"onCancel.emit()\"\n    >\n      {{ 'Login' | translate }}\n    </a>\n  </div>\n</form>\n", dependencies: [{ kind: "directive", type: i4.PhoneValidationDirective, selector: "[c8yPhoneValidation]" }, { kind: "directive", type: i5.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i5.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: i5.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i5.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i5.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i5.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "directive", type: i5.NgForm, selector: "form:not([ngNoForm]):not([formGroup]),ng-form,[ngForm]", inputs: ["ngFormOptions"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "component", type: i6.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i7.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i8.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i9.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "pipe", type: i10.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ProvidePhoneNumberComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-provide-phone-number', template: "<form #twoFactorForm=\"ngForm\" class=\"loginForm\" (ngSubmit)=\"save()\" novalidate>\n  <div class=\"legend form-block center\" translate>Two-factor authentication</div>\n\n  <c8y-form-group [ngClass]=\"requestInProgress || twoFactorForm.invalid ? 'p-b-8' : ''\">\n    <label translate>Provide your phone number</label>\n\n    <input\n      class=\"form-control\"\n      [(ngModel)]=\"phoneNumber\"\n      #contactPhone=\"ngModel\"\n      type=\"text\"\n      name=\"phone\"\n      autocomplete=\"off\"\n      placeholder=\"{{ 'e.g. +49 9 876 543 210`LOCALIZE`' | translate }}\"\n      c8yPhoneValidation\n      required\n    />\n  </c8y-form-group>\n\n  <button\n    title=\"{{ 'Save and continue' | translate }}\"\n    type=\"submit\"\n    class=\"btn btn-primary btn-lg btn-block form-group\"\n    [disabled]=\"requestInProgress || twoFactorForm.invalid\"\n  >\n    {{ 'Save and continue' | translate }}\n  </button>\n\n  <div class=\"d-flex m-t-8\">\n    <a\n      title=\"{{ 'Login' | translate }}\"\n      class=\"small pointer m-l-auto\"\n      href=\"#\"\n      (click)=\"onCancel.emit()\"\n    >\n      {{ 'Login' | translate }}\n    </a>\n  </div>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.LoginService }, { type: i2.AlertService }, { type: i3.UserService }], propDecorators: { credentials: [{
                type: Input
            }], onCancel: [{
                type: Output
            }], onChangeView: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 