import * as i0 from '@angular/core';
import { Component, ChangeDetectionStrategy, Input, NgModule } from '@angular/core';
import { ActionFinishedComponent, ActionFinishedModule } from '@aztrix/components/action-finished';
import * as i7 from '@aztrix/helpers';
import { PropertyFormControl, CustomValidators, HelperPipesModule } from '@aztrix/helpers';
import { ErrorType, PropertyType } from '@aztrix/models';
import * as i2 from '@aztrix/sdk';
import { RequestPasswordResetRepresentation } from '@aztrix/sdk';
import * as i1 from '@aztrix/components/overlay';
import * as i3 from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import * as i4 from '@aztrix/components/login-errors';
import { LoginErrorsModule } from '@aztrix/components/login-errors';
import * as i5 from '@aztrix/components/button';
import { ButtonModule } from '@aztrix/components/button';
import * as i6 from '@aztrix/components/property-edit';
import { PropertyEditModule } from '@aztrix/components/property-edit';
import { CommonModule } from '@angular/common';
import { MatLegacyFormFieldModule } from '@angular/material/legacy-form-field';
import { MatLegacyInputModule } from '@angular/material/legacy-input';
import { IconsModule } from '@aztrix/icons';

class ForgotPasswordModalComponent {
    constructor(_changeDetector, _overlay, _elementRef, _sessions) {
        this._changeDetector = _changeDetector;
        this._overlay = _overlay;
        this._elementRef = _elementRef;
        this._sessions = _sessions;
        this.errorType = ErrorType.NONE;
        this.form = PropertyFormControl.createByType(PropertyType.EMAIL, [
            CustomValidators.propertyRequired,
            CustomValidators.propertyValid,
        ]);
        // eslint-disable-next-line @typescript-eslint/naming-convention
        this.ErrorType = ErrorType;
    }
    ngOnChanges(changes) {
        if (changes.username && this.username) {
            this.form.get('value').setValue(this.username);
        }
    }
    executeSendAction() {
        this.errorType = ErrorType.NONE;
        this.form.disable();
        this._sessions
            .requestPasswordReset({
            loginType: RequestPasswordResetRepresentation.LoginTypeEnum.EMAIL,
            loginValue: this.form.get('value').value,
        })
            .subscribe({
            next: (res) => this.openPasswordResetPendingModal(res.loginType),
            error: (response) => {
                this.errorType = response.error in ErrorType ? response.error : ErrorType.DEFAULT;
                this.form.enable();
            },
        });
    }
    resetErrors() {
        this.errorType = ErrorType.NONE;
        this._changeDetector.markForCheck();
    }
    openPasswordResetPendingModal(propertyType) {
        const type = propertyType === PropertyType.EMAIL ? 'email' : 'mobile';
        this._overlay.closeModal();
        this._overlay.createModal(ActionFinishedComponent, {
            title: 'modal.forgotPassword.title',
            init: (resetModal) => {
                resetModal.bodyIconClass = type === 'mobile' ? 'message' : 'email';
                resetModal.bodyTitle = `resetPassword.succeeded.${type}.title`;
                resetModal.bodyExplanation = `resetPassword.succeeded.${type}.explanation`;
                resetModal.didClickButton.subscribe(() => this._overlay.closeModal());
            },
        }, this._elementRef);
    }
}
ForgotPasswordModalComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i1.OverlayService }, { token: i0.ElementRef }, { token: i2.SessionsService }], target: i0.ɵɵFactoryTarget.Component });
ForgotPasswordModalComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "15.1.5", type: ForgotPasswordModalComponent, selector: "ng-component", inputs: { username: "username" }, usesOnChanges: true, ngImport: i0, template: "<div class=\"modal-content-wrapper\">\n  <ax-login-errors [errorType]=\"errorType\"></ax-login-errors>\n  <form class=\"c-login__form m-form m-form--no-padding\">\n    <ax-property-edit [form]=\"form\"></ax-property-edit>\n  </form>\n  <div class=\"h-button-wrapper\">\n    <button\n      axButton\n      (click)=\"executeSendAction()\"\n      color=\"primary\"\n      [disabled]=\"form?.invalid || form?.disabled\"\n      filled\n    >\n      {{ 'modal.forgotPassword.button' | translate }}\n    </button>\n  </div>\n</div>\n", styles: [":host{display:block;padding:0 1rem}:host ax-login-errors{padding-bottom:1rem;display:block}\n"], dependencies: [{ kind: "directive", type: i3.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i3.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "component", type: i4.LoginErrorsComponent, selector: "ax-login-errors", inputs: ["errorType"] }, { kind: "directive", type: i5.ButtonDirective, selector: "[axButton]", inputs: ["color", "labelHideMobile", "smallButton", "bigButton", "fullWidth", "filled", "outline", "icon-button", "hoverLabel", "hover", "lowercase", "disabled"] }, { kind: "component", type: i6.PropertyEditComponent, selector: "ax-property-edit", inputs: ["isIndividual", "isProposalSubscribeForm"] }, { kind: "pipe", type: i7.TranslatePipe, name: "translate" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalComponent, decorators: [{
            type: Component,
            args: [{ changeDetection: ChangeDetectionStrategy.OnPush, template: "<div class=\"modal-content-wrapper\">\n  <ax-login-errors [errorType]=\"errorType\"></ax-login-errors>\n  <form class=\"c-login__form m-form m-form--no-padding\">\n    <ax-property-edit [form]=\"form\"></ax-property-edit>\n  </form>\n  <div class=\"h-button-wrapper\">\n    <button\n      axButton\n      (click)=\"executeSendAction()\"\n      color=\"primary\"\n      [disabled]=\"form?.invalid || form?.disabled\"\n      filled\n    >\n      {{ 'modal.forgotPassword.button' | translate }}\n    </button>\n  </div>\n</div>\n", styles: [":host{display:block;padding:0 1rem}:host ax-login-errors{padding-bottom:1rem;display:block}\n"] }]
        }], ctorParameters: function () { return [{ type: i0.ChangeDetectorRef }, { type: i1.OverlayService }, { type: i0.ElementRef }, { type: i2.SessionsService }]; }, propDecorators: { username: [{
                type: Input
            }] } });

class ForgotPasswordModalModule {
}
ForgotPasswordModalModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
ForgotPasswordModalModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalModule, declarations: [ForgotPasswordModalComponent], imports: [CommonModule,
        MatLegacyFormFieldModule,
        MatLegacyInputModule,
        IconsModule,
        ReactiveFormsModule,
        LoginErrorsModule,
        HelperPipesModule,
        ButtonModule,
        ActionFinishedModule,
        PropertyEditModule], exports: [ForgotPasswordModalComponent] });
ForgotPasswordModalModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalModule, imports: [CommonModule,
        MatLegacyFormFieldModule,
        MatLegacyInputModule,
        IconsModule,
        ReactiveFormsModule,
        LoginErrorsModule,
        HelperPipesModule,
        ButtonModule,
        ActionFinishedModule,
        PropertyEditModule] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.5", ngImport: i0, type: ForgotPasswordModalModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ForgotPasswordModalComponent],
                    imports: [
                        CommonModule,
                        MatLegacyFormFieldModule,
                        MatLegacyInputModule,
                        IconsModule,
                        ReactiveFormsModule,
                        LoginErrorsModule,
                        HelperPipesModule,
                        ButtonModule,
                        ActionFinishedModule,
                        PropertyEditModule,
                    ],
                    exports: [ForgotPasswordModalComponent],
                }]
        }] });

/**
 * Generated bundle index. Do not edit.
 */

export { ForgotPasswordModalComponent, ForgotPasswordModalModule };
//# sourceMappingURL=aztrix-components-forgot-password-modal.mjs.map
//# sourceMappingURL=aztrix-components-forgot-password-modal.mjs.map
