import { ChangeDetectorRef, Component, EventEmitter, Output, ViewChild } from '@angular/core';
import { ControlContainer, NgForm, NgModel } from '@angular/forms';
import { LoginService } from '../login/login.service';
import * as i0 from "@angular/core";
import * as i1 from "../login/login.service";
import * as i2 from "../forms/default-validation.directive";
import * as i3 from "@angular/forms";
import * as i4 from "../forms/form-group.component";
import * as i5 from "../i18n/c8y-translate.directive";
import * as i6 from "@angular/common";
import * as i7 from "./password-confirm.directive";
import * as i8 from "./password-check-list.component";
import * as i9 from "./password-input.component";
import * as i10 from "../i18n/c8y-translate.pipe";
export class NewPasswordComponent {
    set _newPasswordModel(ngModel) {
        if (ngModel) {
            this.newPasswordModel = ngModel;
            ngModel.control.addValidators(this.passwordChecklistValidator);
        }
    }
    constructor(loginService, cdRef) {
        this.loginService = loginService;
        this.cdRef = cdRef;
        this.password = new EventEmitter();
        this.model = {};
        this.changePassword = false;
        this.passwordEnforced = false;
        this.passwordChecklistValidator = () => this.requirementsFulfilled ? null : { passwordStrengthChecklist: true };
    }
    ngOnInit() {
        this.loadPasswordStrengthSettings();
        this.loginService.getGreenMinLength().then(value => {
            this.minlength = value;
        });
    }
    newPasswordChanged() {
        this.password.emit({
            password: this.model.newPassword,
            passwordStrength: this.model.strength
        });
    }
    updateValidity(requirementsFulfilled) {
        this.requirementsFulfilled = requirementsFulfilled;
        this.cdRef.detectChanges();
        this.newPasswordModel.control.updateValueAndValidity();
        // There are two validators checking password validity, but we only want to show one of these errors at a time,
        // where checklist validator takes priority.
        if (!this.requirementsFulfilled) {
            delete this.newPasswordModel.control.errors['password'];
        }
    }
    async loadPasswordStrengthSettings() {
        this.passwordEnforced = await this.loginService.getPasswordStrengthEnforced({ refresh: true });
    }
    toggleChangePassword() {
        this.changePassword = !this.changePassword;
        if (!this.changePassword) {
            this.password.emit({});
            this.model = {};
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: NewPasswordComponent, deps: [{ token: i1.LoginService }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: NewPasswordComponent, selector: "c8y-new-password", outputs: { password: "password" }, viewQueries: [{ propertyName: "_newPasswordModel", first: true, predicate: ["newPassword"], descendants: true }], ngImport: i0, template: "<div class=\"form-group\">\n  <button\n    class=\"btn btn-default\"\n    type=\"button\"\n    (click)=\"toggleChangePassword()\"\n  >\n    <ng-container *ngIf=\"!changePassword\">\n      {{ 'Change password' | translate }}\n    </ng-container>\n    <ng-container *ngIf=\"changePassword\">\n      {{ 'Cancel password change' | translate }}\n    </ng-container>\n  </button>\n</div>\n\n<div\n  class=\"row content-flex-50\"\n  *ngIf=\"changePassword\"\n>\n  <div class=\"col-6\">\n    <c8y-form-group>\n      <label\n        for=\"newPassword\"\n        translate\n      >\n        Password\n      </label>\n      <c8y-password-input\n        name=\"newPassword\"\n        required\n        [id]=\"'newPassword'\"\n        #newPassword=\"ngModel\"\n        [(ngModel)]=\"model.newPassword\"\n        (change)=\"newPasswordChanged()\"\n        (input)=\"newPasswordConfirm.control.updateValueAndValidity()\"\n        c8yDefaultValidation=\"password\"\n        [autocomplete]=\"'new-password'\"\n      ></c8y-password-input>\n    </c8y-form-group>\n\n    <c8y-form-group>\n      <label\n        for=\"newConfirmPassword\"\n        translate\n      >\n        Confirm password\n      </label>\n      <c8y-password-input\n        name=\"newPasswordConfirm\"\n        required\n        [id]=\"'newConfirmPassword'\"\n        #newPasswordConfirm=\"ngModel\"\n        [(ngModel)]=\"model.newPasswordConfirm\"\n        passwordConfirm=\"newPassword\"\n        [autocomplete]=\"'new-password'\"\n      ></c8y-password-input>\n    </c8y-form-group>\n  </div>\n  <div class=\"col-6\">\n    <c8y-password-check-list\n      [password]=\"model.newPassword\"\n      [strengthEnforced]=\"passwordEnforced\"\n      (onRequirementsFulfilled)=\"updateValidity($event)\"\n    ></c8y-password-check-list>\n  </div>\n</div>\n", dependencies: [{ kind: "directive", type: i2.DefaultValidationDirective, selector: "[c8yDefaultValidation]", inputs: ["c8yDefaultValidation"] }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i3.NgModel, selector: "[ngModel]:not([formControlName]):not([formControl])", inputs: ["name", "disabled", "ngModel", "ngModelOptions"], outputs: ["ngModelChange"], exportAs: ["ngModel"] }, { kind: "component", type: i4.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i5.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i6.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i7.PasswordConfirm, selector: "[passwordConfirm]" }, { kind: "component", type: i8.PasswordCheckListComponent, selector: "c8y-password-check-list", inputs: ["strengthEnforced", "password"], outputs: ["onRequirementsFulfilled"] }, { kind: "component", type: i9.PasswordInputComponent, selector: "c8y-password-input", inputs: ["id", "autocomplete"] }, { kind: "pipe", type: i10.C8yTranslatePipe, name: "translate" }], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: NewPasswordComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-new-password', viewProviders: [{ provide: ControlContainer, useExisting: NgForm }], template: "<div class=\"form-group\">\n  <button\n    class=\"btn btn-default\"\n    type=\"button\"\n    (click)=\"toggleChangePassword()\"\n  >\n    <ng-container *ngIf=\"!changePassword\">\n      {{ 'Change password' | translate }}\n    </ng-container>\n    <ng-container *ngIf=\"changePassword\">\n      {{ 'Cancel password change' | translate }}\n    </ng-container>\n  </button>\n</div>\n\n<div\n  class=\"row content-flex-50\"\n  *ngIf=\"changePassword\"\n>\n  <div class=\"col-6\">\n    <c8y-form-group>\n      <label\n        for=\"newPassword\"\n        translate\n      >\n        Password\n      </label>\n      <c8y-password-input\n        name=\"newPassword\"\n        required\n        [id]=\"'newPassword'\"\n        #newPassword=\"ngModel\"\n        [(ngModel)]=\"model.newPassword\"\n        (change)=\"newPasswordChanged()\"\n        (input)=\"newPasswordConfirm.control.updateValueAndValidity()\"\n        c8yDefaultValidation=\"password\"\n        [autocomplete]=\"'new-password'\"\n      ></c8y-password-input>\n    </c8y-form-group>\n\n    <c8y-form-group>\n      <label\n        for=\"newConfirmPassword\"\n        translate\n      >\n        Confirm password\n      </label>\n      <c8y-password-input\n        name=\"newPasswordConfirm\"\n        required\n        [id]=\"'newConfirmPassword'\"\n        #newPasswordConfirm=\"ngModel\"\n        [(ngModel)]=\"model.newPasswordConfirm\"\n        passwordConfirm=\"newPassword\"\n        [autocomplete]=\"'new-password'\"\n      ></c8y-password-input>\n    </c8y-form-group>\n  </div>\n  <div class=\"col-6\">\n    <c8y-password-check-list\n      [password]=\"model.newPassword\"\n      [strengthEnforced]=\"passwordEnforced\"\n      (onRequirementsFulfilled)=\"updateValidity($event)\"\n    ></c8y-password-check-list>\n  </div>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.LoginService }, { type: i0.ChangeDetectorRef }], propDecorators: { password: [{
                type: Output
            }], _newPasswordModel: [{
                type: ViewChild,
                args: ['newPassword']
            }] } });
//# sourceMappingURL=data:application/json;base64, 