import { Component, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { SessionsService } from '@aztrix/sdk';
import { BehaviorSubject, tap } from 'rxjs';
import { ModeService } from '../services/mode.service';
import * as i0 from "@angular/core";
import * as i1 from "../services/mode.service";
import * as i2 from "@aztrix/sdk";
import * as i3 from "@angular/common";
import * as i4 from "@aztrix/components/button";
import * as i5 from "@aztrix/components/snackbar";
import * as i6 from "../auth-form-header/auth-form-header.component";
import * as i7 from "./reset-password-new-password-step/reset-password-new-password-step.component";
import * as i8 from "./reset-password-email-step/reset-password-email-step.component";
import * as i9 from "@aztrix/helpers";
export class ResetPasswordFormComponent {
    constructor(modeService, _sessionsService) {
        this.modeService = modeService;
        this._sessionsService = _sessionsService;
        this.error$ = new BehaviorSubject(null);
        this.step = 0;
        this.verification = (code, verificationValue) => {
            this.error$.next(null);
            return this._sessionsService
                .confirmResetPassword({
                loginValue: verificationValue,
                code,
                password: this.passwordForm.value,
            })
                .pipe(tap({
                next: () => {
                    this.modeService.setMode('login');
                },
                error: (err) => {
                    this.error$.next(err.error);
                    this.step = 0;
                },
            }));
        };
        this.retryVerification = (verificationValue) => {
            this.error$.next(null);
            return this._sessionsService.requestPasswordReset({ loginValue: verificationValue }).pipe(tap({
                error: (err) => {
                    this.error$.next(err.error);
                    this.step = 0;
                },
            }));
        };
    }
    ngOnInit() {
        if (this.loginValue) {
            this.usernameForm.setValue(this.loginValue);
        }
    }
    get usernameForm() {
        return this.form.get('username');
    }
    get passwordForm() {
        return this.form.get('password');
    }
    back() {
        if (this.step === 0) {
            this.modeService.setMode('login');
        }
        else {
            this.step = 0;
        }
    }
    sendResetPasswordRequest() {
        if (!this._formValid(this.step)) {
            return;
        }
        this.error$.next(null);
        this._sessionsService.requestPasswordReset({ loginValue: this.usernameForm?.value }).subscribe({
            next: () => (this.step = 1),
            error: (err) => {
                this.error$.next(err.error);
            },
        });
    }
    _formValid(step) {
        this.form.markAsUntouched();
        if (step === 0) {
            this.usernameForm.markAsTouched();
            return this.usernameForm.valid;
        }
        if (step === 1) {
            this.passwordForm.markAsTouched();
            return this.passwordForm.valid;
        }
        return false;
    }
}
ResetPasswordFormComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ResetPasswordFormComponent, deps: [{ token: i1.ModeService }, { token: i2.SessionsService }], target: i0.ɵɵFactoryTarget.Component });
ResetPasswordFormComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.1.5", type: ResetPasswordFormComponent, selector: "ax-reset-password-form", inputs: { form: "form", loginValue: "loginValue" }, ngImport: i0, template: "<ax-auth-form-header\n  title=\"modal.forgotPassword.button\"\n  [subTexts]=\"[\n    'password.reset.explanation' | translate,\n    'password.reset.explanation.2' | translate\n  ]\"\n></ax-auth-form-header>\n\n<div class=\"form\">\n  <ax-snackbar *ngIf=\"error$ | async\" [dismissible]=\"false\" color=\"red\">\n    <ng-container [ngSwitch]=\"error$ | async\">\n      <span *ngSwitchCase=\"'FORBIDDEN'\">{{ 'error.too-many-attempts' | translate }}</span>\n      <span *ngSwitchCase=\"'LOGIN_NOT_REGISTERED'\">{{\n        'error.reset.invalid.email' | translate\n      }}</span>\n      <span *ngSwitchDefault>{{ 'error.default.title' | translate }}</span>\n    </ng-container>\n  </ax-snackbar>\n\n  <ax-reset-password-email-step\n    *ngIf=\"step === 0\"\n    [form]=\"form\"\n    (back)=\"back()\"\n    (continue)=\"sendResetPasswordRequest()\"\n  ></ax-reset-password-email-step>\n  <ax-reset-password-new-password-step\n    *ngIf=\"step === 1\"\n    [form]=\"form\"\n    [verification]=\"verification\"\n    [retryVerification]=\"retryVerification\"\n  ></ax-reset-password-new-password-step>\n</div>\n<ax-button\n  *ngIf=\"step === 0\"\n  class=\"action back\"\n  axButton\n  color=\"accent\"\n  fullWidth\n  type=\"button\"\n  (click)=\"back()\"\n  icon=\"arrow-left\"\n>\n  {{ 'label.login.back-to' | translate }}</ax-button\n>\n", styles: ["", ":host{display:flex;flex-direction:column;gap:.5rem}.form{margin-bottom:20px;background-color:#fff;border-radius:15px;padding:1.5rem 1.5rem 0}.value{display:flex;flex-direction:column;align-items:flex-end;margin-bottom:.5rem}.input{width:100%}.action{margin:1rem 1rem -20px}.action ::ng-deep .ax-button{font-size:1.2rem;text-transform:none;height:2.5rem}.back{margin:1rem auto -.5rem;width:auto}.back ::ng-deep .ax-button{font-size:small;height:2rem;width:auto;margin:0 auto}.back ::ng-deep .ax-button .icon{width:1rem;height:1rem}.verification ::ng-deep .verify{width:100%;display:block;position:relative;bottom:-20px}.verification ::ng-deep .verify .ax-button{font-size:1.2rem;text-transform:none;height:2.5rem;width:100%}.verification ::ng-deep .resend{height:0;position:relative;bottom:-30px}.verification ::ng-deep .error-message{height:0;position:relative;bottom:-30px;display:flex;text-align:center;flex-direction:column;gap:1rem}\n"], dependencies: [{ kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.NgSwitch, selector: "[ngSwitch]", inputs: ["ngSwitch"] }, { kind: "directive", type: i3.NgSwitchCase, selector: "[ngSwitchCase]", inputs: ["ngSwitchCase"] }, { kind: "directive", type: i3.NgSwitchDefault, selector: "[ngSwitchDefault]" }, { kind: "directive", type: i4.ButtonDirective, selector: "[axButton]", inputs: ["color", "labelHideMobile", "smallButton", "bigButton", "fullWidth", "filled", "outline", "icon-button", "hoverLabel", "hover", "lowercase", "disabled"] }, { kind: "component", type: i4.ButtonComponent, selector: "ax-button" }, { kind: "component", type: i5.SnackbarComponent, selector: "ax-snackbar", inputs: ["type", "timeout", "dismissible", "open", "icon", "color"], outputs: ["didClose"] }, { kind: "component", type: i6.AuthFormHeaderComponent, selector: "ax-auth-form-header", inputs: ["title", "subTexts"] }, { kind: "component", type: i7.ResetPasswordNewPasswordStepComponent, selector: "ax-reset-password-new-password-step", inputs: ["verification", "retryVerification"] }, { kind: "component", type: i8.ResetPasswordEmailStepComponent, selector: "ax-reset-password-email-step" }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }, { kind: "pipe", type: i9.TranslatePipe, name: "translate" }] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ResetPasswordFormComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-reset-password-form', template: "<ax-auth-form-header\n  title=\"modal.forgotPassword.button\"\n  [subTexts]=\"[\n    'password.reset.explanation' | translate,\n    'password.reset.explanation.2' | translate\n  ]\"\n></ax-auth-form-header>\n\n<div class=\"form\">\n  <ax-snackbar *ngIf=\"error$ | async\" [dismissible]=\"false\" color=\"red\">\n    <ng-container [ngSwitch]=\"error$ | async\">\n      <span *ngSwitchCase=\"'FORBIDDEN'\">{{ 'error.too-many-attempts' | translate }}</span>\n      <span *ngSwitchCase=\"'LOGIN_NOT_REGISTERED'\">{{\n        'error.reset.invalid.email' | translate\n      }}</span>\n      <span *ngSwitchDefault>{{ 'error.default.title' | translate }}</span>\n    </ng-container>\n  </ax-snackbar>\n\n  <ax-reset-password-email-step\n    *ngIf=\"step === 0\"\n    [form]=\"form\"\n    (back)=\"back()\"\n    (continue)=\"sendResetPasswordRequest()\"\n  ></ax-reset-password-email-step>\n  <ax-reset-password-new-password-step\n    *ngIf=\"step === 1\"\n    [form]=\"form\"\n    [verification]=\"verification\"\n    [retryVerification]=\"retryVerification\"\n  ></ax-reset-password-new-password-step>\n</div>\n<ax-button\n  *ngIf=\"step === 0\"\n  class=\"action back\"\n  axButton\n  color=\"accent\"\n  fullWidth\n  type=\"button\"\n  (click)=\"back()\"\n  icon=\"arrow-left\"\n>\n  {{ 'label.login.back-to' | translate }}</ax-button\n>\n", styles: [":host{display:flex;flex-direction:column;gap:.5rem}.form{margin-bottom:20px;background-color:#fff;border-radius:15px;padding:1.5rem 1.5rem 0}.value{display:flex;flex-direction:column;align-items:flex-end;margin-bottom:.5rem}.input{width:100%}.action{margin:1rem 1rem -20px}.action ::ng-deep .ax-button{font-size:1.2rem;text-transform:none;height:2.5rem}.back{margin:1rem auto -.5rem;width:auto}.back ::ng-deep .ax-button{font-size:small;height:2rem;width:auto;margin:0 auto}.back ::ng-deep .ax-button .icon{width:1rem;height:1rem}.verification ::ng-deep .verify{width:100%;display:block;position:relative;bottom:-20px}.verification ::ng-deep .verify .ax-button{font-size:1.2rem;text-transform:none;height:2.5rem;width:100%}.verification ::ng-deep .resend{height:0;position:relative;bottom:-30px}.verification ::ng-deep .error-message{height:0;position:relative;bottom:-30px;display:flex;text-align:center;flex-direction:column;gap:1rem}\n"] }]
        }], ctorParameters: function () { return [{ type: i1.ModeService }, { type: i2.SessionsService }]; }, propDecorators: { form: [{
                type: Input
            }], loginValue: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 