import { Component, Output, Input, EventEmitter } from '@angular/core';
import { LoginService } from './login.service';
import { UserService, PasswordStrength } from '@c8y/client';
import { OptionsService } from '../common/options.service';
import { AlertService } from '../alert/alert.service';
import { LoginViews } from './login.model';
import * as i0 from "@angular/core";
import * as i1 from "./login.service";
import * as i2 from "@c8y/client";
import * as i3 from "../common/options.service";
import * as i4 from "../alert/alert.service";
import * as i5 from "@angular/forms";
import * as i6 from "../i18n/c8y-translate.directive";
import * as i7 from "@angular/common";
import * as i8 from "../authentication/password-check-list.component";
import * as i9 from "../authentication/password-confirm.directive";
import * as i10 from "../forms/form-group.component";
import * as i11 from "../forms/message.directive";
import * as i12 from "../forms/messages.component";
import * as i13 from "../forms/required-input-placeholder.directive";
import * as i14 from "./password-strength-validator.directive";
import * as i15 from "../i18n/c8y-translate.pipe";
export class ChangePasswordComponent {
    constructor(loginService, users, options, alert) {
        this.loginService = loginService;
        this.users = users;
        this.options = options;
        this.alert = alert;
        this.onChangeView = new EventEmitter();
        this.passwordPattern = /^[a-zA-Z0-9`~!@#$%^&*()_|+\-=?;:'",.<>{}[\]\\/]{8,32}$/;
        this.isLoading = false;
        this.model = {
            tenantId: '',
            email: '',
            newPassword: '',
            newPasswordConfirm: ''
        };
        this.emailReadOnly = false;
        this.passwordStrengthEnforced = false;
        this.TOKEN_PARAM = 'token';
        this.EMAIL_PARAM = 'email';
    }
    async ngOnInit() {
        this.model.tenantId = this.loginService.getTenant();
        this.model.email = this.options.get(this.EMAIL_PARAM, '');
        this.emailReadOnly = !!this.model.email;
        this.passwordStrengthEnforced = await this.loginService.getPasswordStrengthEnforced();
    }
    async changePassword() {
        const resetPassword = {
            token: this.credentials.token,
            email: this.model.email,
            newPassword: this.model.newPassword,
            passwordStrength: PasswordStrength.GREEN // @TODO: MTM-58234 - Deprecated - currently Backend requires this parameter.
        };
        try {
            this.isLoading = true;
            const { res } = await this.users.resetPassword(resetPassword, this.model.tenantId);
            if (res.status === 200) {
                this.loginService.addSuccessMessage('password_changed');
                this.credentials.token = undefined;
                this.options.set(this.TOKEN_PARAM, undefined);
                if (this.loginService.showTenantSetup()) {
                    this.onChangeView.emit({ view: LoginViews.TenantIdSetup });
                }
                else {
                    this.onChangeView.emit({ view: LoginViews.Credentials });
                }
            }
        }
        catch (e) {
            this.alert.addServerFailure(e);
        }
        finally {
            this.loginService.reset();
            this.isLoading = false;
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ChangePasswordComponent, deps: [{ token: i1.LoginService }, { token: i2.UserService }, { token: i3.OptionsService }, { token: i4.AlertService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: ChangePasswordComponent, selector: "c8y-change-password", inputs: { credentials: "credentials" }, outputs: { onChangeView: "onChangeView" }, ngImport: i0, template: "<form class=\"loginForm\" (ngSubmit)=\"changePassword()\" #changePasswordForm=\"ngForm\" novalidate>\n  <div class=\"legend form-block center\" translate>Change password</div>\n\n  <c8y-form-group class=\"tenantField\" id=\"tenantField\" *ngIf=\"loginService.showTenant()\">\n    <label translate>Tenant ID</label>\n    <input\n      [(ngModel)]=\"model.tenantId\"\n      #tenantId=\"ngModel\"\n      type=\"text\"\n      name=\"tenantId\"\n      autocapitalize=\"off\"\n      autocorrect=\"off\"\n      class=\"form-control\"\n      placeholder=\"{{ 'Tenant ID' | translate }}\"\n      required\n    />\n  </c8y-form-group>\n\n  <c8y-form-group>\n    <label translate>Email address</label>\n    <input\n      [(ngModel)]=\"model.email\"\n      #email=\"ngModel\"\n      type=\"text\"\n      name=\"email\"\n      autocapitalize=\"off\"\n      autocorrect=\"off\"\n      class=\"form-control\"\n      placeholder=\"{{ 'Email address' | translate }}\"\n      email\n      required\n      [readonly]=\"emailReadOnly\"\n    />\n  </c8y-form-group>\n\n  <div class=\"row content-flex-50\">\n    <div class=\"col-6\">\n      <c8y-form-group>\n        <label translate>New password</label>\n        <input\n          [(ngModel)]=\"model.newPassword\"\n          #newPassword=\"ngModel\"\n          type=\"password\"\n          name=\"newPassword\"\n          class=\"form-control\"\n          placeholder=\"{{ 'New password' | translate }}\"\n          [pattern]=\"passwordPattern\"\n          autocomplete=\"new-password\"\n          [passwordStrengthEnforced]=\"passwordStrengthEnforced\"\n          required\n        />\n        <c8y-messages>\n          <c8y-message\n            name=\"pattern\"\n            [text]=\"loginService.ERROR_MESSAGES.pattern_newPassword\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n\n      <c8y-form-group>\n        <label translate>Confirm password</label>\n        <input\n          [(ngModel)]=\"model.newPasswordConfirm\"\n          #newPasswordConfirm=\"ngModel\"\n          type=\"password\"\n          name=\"newPasswordConfirm\"\n          class=\"form-control\"\n          placeholder=\"{{ 'Confirm password' | translate }}\"\n          passwordConfirm=\"newPassword\"\n          autocomplete=\"new-password\"\n          required\n        />\n        <c8y-messages>\n          <c8y-message\n            name=\"passwordConfirm\"\n            [text]=\"loginService.ERROR_MESSAGES.passwordConfirm\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n    </div>\n    <div class=\"col-6\">\n      <c8y-password-check-list\n        [password]=\"model.newPassword\"\n        [strengthEnforced]=\"passwordStrengthEnforced\"\n      ></c8y-password-check-list>\n    </div>\n  </div>\n\n  <button\n    title=\"{{ 'Set password' | translate }}\"\n    [disabled]=\"!changePasswordForm.form.valid || isLoading\"\n    type=\"submit\"\n    class=\"btn btn-primary btn-lg btn-block form-group\"\n  >\n    {{ 'Set password' | translate }}\n  </button>\n</form>\n", dependencies: [{ 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.PatternValidator, selector: "[pattern][formControlName],[pattern][formControl],[pattern][ngModel]", inputs: ["pattern"] }, { kind: "directive", type: i5.EmailValidator, selector: "[email][formControlName],[email][formControl],[email][ngModel]", inputs: ["email"] }, { 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: "directive", type: i6.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i7.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i8.PasswordCheckListComponent, selector: "c8y-password-check-list", inputs: ["strengthEnforced", "password"], outputs: ["onRequirementsFulfilled"] }, { kind: "directive", type: i9.PasswordConfirm, selector: "[passwordConfirm]" }, { kind: "component", type: i10.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i11.MessageDirective, selector: "c8y-message", inputs: ["name", "text"] }, { kind: "component", type: i12.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage"] }, { kind: "directive", type: i13.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i14.PasswordStrengthValidatorDirective, selector: "[passwordStrengthEnforced]", inputs: ["passwordStrengthEnforced"] }, { kind: "pipe", type: i15.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: ChangePasswordComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-change-password', template: "<form class=\"loginForm\" (ngSubmit)=\"changePassword()\" #changePasswordForm=\"ngForm\" novalidate>\n  <div class=\"legend form-block center\" translate>Change password</div>\n\n  <c8y-form-group class=\"tenantField\" id=\"tenantField\" *ngIf=\"loginService.showTenant()\">\n    <label translate>Tenant ID</label>\n    <input\n      [(ngModel)]=\"model.tenantId\"\n      #tenantId=\"ngModel\"\n      type=\"text\"\n      name=\"tenantId\"\n      autocapitalize=\"off\"\n      autocorrect=\"off\"\n      class=\"form-control\"\n      placeholder=\"{{ 'Tenant ID' | translate }}\"\n      required\n    />\n  </c8y-form-group>\n\n  <c8y-form-group>\n    <label translate>Email address</label>\n    <input\n      [(ngModel)]=\"model.email\"\n      #email=\"ngModel\"\n      type=\"text\"\n      name=\"email\"\n      autocapitalize=\"off\"\n      autocorrect=\"off\"\n      class=\"form-control\"\n      placeholder=\"{{ 'Email address' | translate }}\"\n      email\n      required\n      [readonly]=\"emailReadOnly\"\n    />\n  </c8y-form-group>\n\n  <div class=\"row content-flex-50\">\n    <div class=\"col-6\">\n      <c8y-form-group>\n        <label translate>New password</label>\n        <input\n          [(ngModel)]=\"model.newPassword\"\n          #newPassword=\"ngModel\"\n          type=\"password\"\n          name=\"newPassword\"\n          class=\"form-control\"\n          placeholder=\"{{ 'New password' | translate }}\"\n          [pattern]=\"passwordPattern\"\n          autocomplete=\"new-password\"\n          [passwordStrengthEnforced]=\"passwordStrengthEnforced\"\n          required\n        />\n        <c8y-messages>\n          <c8y-message\n            name=\"pattern\"\n            [text]=\"loginService.ERROR_MESSAGES.pattern_newPassword\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n\n      <c8y-form-group>\n        <label translate>Confirm password</label>\n        <input\n          [(ngModel)]=\"model.newPasswordConfirm\"\n          #newPasswordConfirm=\"ngModel\"\n          type=\"password\"\n          name=\"newPasswordConfirm\"\n          class=\"form-control\"\n          placeholder=\"{{ 'Confirm password' | translate }}\"\n          passwordConfirm=\"newPassword\"\n          autocomplete=\"new-password\"\n          required\n        />\n        <c8y-messages>\n          <c8y-message\n            name=\"passwordConfirm\"\n            [text]=\"loginService.ERROR_MESSAGES.passwordConfirm\"\n          ></c8y-message>\n        </c8y-messages>\n      </c8y-form-group>\n    </div>\n    <div class=\"col-6\">\n      <c8y-password-check-list\n        [password]=\"model.newPassword\"\n        [strengthEnforced]=\"passwordStrengthEnforced\"\n      ></c8y-password-check-list>\n    </div>\n  </div>\n\n  <button\n    title=\"{{ 'Set password' | translate }}\"\n    [disabled]=\"!changePasswordForm.form.valid || isLoading\"\n    type=\"submit\"\n    class=\"btn btn-primary btn-lg btn-block form-group\"\n  >\n    {{ 'Set password' | translate }}\n  </button>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.LoginService }, { type: i2.UserService }, { type: i3.OptionsService }, { type: i4.AlertService }], propDecorators: { credentials: [{
                type: Input
            }], onChangeView: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 