import * as i0 from '@angular/core';
import { EventEmitter, Component, Input, Output, NgModule } from '@angular/core';
import * as i6 from '@aztrix/helpers';
import { PropertyFormControl, CustomValidators, isOfType, HelperPipesModule } from '@aztrix/helpers';
import { ProfileContext } from '@aztrix/models';
import * as i1 from '@aztrix/sdk';
import { ProfilePropertyRepresentation, ContextTypeRepresentation } from '@aztrix/sdk';
import { BehaviorSubject, ReplaySubject, filter, map, shareReplay, first, switchMap, of } from 'rxjs';
import * as i2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i3 from '@aztrix/components/button';
import { ButtonModule } from '@aztrix/components/button';
import * as i4 from '@aztrix/components/property-edit';
import { PropertyEditModule } from '@aztrix/components/property-edit';
import * as i5 from '@aztrix/components/property-otp-verification';
import { PropertyOtpVerificationModule } from '@aztrix/components/property-otp-verification';

class ProfilePropertyOtpVerificationComponent {
    constructor(_profile, _contexts) {
        this._profile = _profile;
        this._contexts = _contexts;
        this.done = new EventEmitter();
        this.step = 0;
        this.propertyForm = (PropertyFormControl.createByType(ProfilePropertyRepresentation.TypeEnum.EMAIL, [
            CustomValidators.propertyRequired,
            CustomValidators.propertyValid,
        ]));
        this.loading$ = new BehaviorSubject(false);
        this.error$ = new BehaviorSubject(null);
        this.profileProperty$ = new ReplaySubject(0);
        this._profile$ = this._profile.getMeProfile('FULL');
        this._contextId$ = this._profile$.pipe(filter((profile) => !!profile), map((profile) => {
            return profile.propertyContexts?.find((context) => profile.profileContext === ProfileContext.INDIVIDUAL
                ? context.type === ContextTypeRepresentation.NameEnum.PERSONAL
                : context.type === ContextTypeRepresentation.NameEnum.GENERAL)?.id;
        }));
        this._accountProperties$ = this._profile$.pipe(map((profile) => profile.propertyContexts
            ?.filter((context) => !isOfType(context, ContextTypeRepresentation.NameEnum.GROUP, ContextTypeRepresentation.NameEnum.PROPOSAL, ContextTypeRepresentation.NameEnum.MISC))
            .flatMap((context) => context.properties)
            .filter((property) => property?.type === this.type)));
        this.accountProperties$ = this._accountProperties$.pipe(shareReplay(1));
    }
    ngOnInit() {
        this._accountProperties$.pipe(first()).subscribe((properties) => {
            const property = properties?.find((property) => property?.value === this.value);
            if (property) {
                this.profileProperty$.next(property);
                PropertyFormControl.update(this.propertyForm, property, [
                    CustomValidators.propertyRequired,
                    CustomValidators.propertyValid,
                ]);
            }
        });
    }
    continue() {
        if (this.propertyForm.valid) {
            this.error$.next(null);
            this.loading$.next(true);
            if (this.propertyForm.value.value === this.value) {
                this.loading$.next(false);
                this.done.emit(this.propertyForm.value);
            }
            else {
                this._accountProperties$
                    .pipe(first(), map((properties) => properties?.find((property) => property?.value === this.propertyForm.value.value)), switchMap((property) => {
                    if (property) {
                        return of(property);
                    }
                    else {
                        return this._contextId$.pipe(first(), switchMap((contextId) => this._contexts.addProperty(contextId, {
                            ...this.propertyForm.value,
                            id: undefined,
                        })), switchMap((contextProperty) => this._accountProperties$.pipe(map((property) => property?.find((property) => property?.id === contextProperty.id)))));
                    }
                }))
                    .subscribe((property) => {
                    this.profileProperty$.next(property);
                    this.step = this.step + 1;
                    if (property?.verification === ProfilePropertyRepresentation.VerificationEnum.VERIFIED) {
                        this.done.emit(property);
                    }
                    this.loading$.next(false);
                });
            }
        }
    }
    verificationComplete() {
        this.profileProperty$.pipe(first()).subscribe((property) => {
            this.done.emit(property);
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationComponent, deps: [{ token: i1.ProfileService }, { token: i1.ContextsService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: ProfilePropertyOtpVerificationComponent, selector: "ax-profile-property-otp-verification", inputs: { type: "type", value: "value", description: "description" }, outputs: { done: "done" }, ngImport: i0, template: "<ng-container *ngIf=\"step === 0\">\n  <p>{{ description | translate }}</p>\n  <ax-property-edit\n    *ngIf=\"propertyForm?.value\"\n    [form]=\"propertyForm\"\n    [autofocus]=\"false\"\n    [required]=\"true\"\n    [autocompleteProperties]=\"(accountProperties$ | async) || undefined\"\n  ></ax-property-edit>\n\n  <div class=\"button-wrapper\">\n    <ax-button\n      filled\n      color=\"primary\"\n      [loading]=\"loading$ | async\"\n      [disabled]=\"(loading$ | async) ?? true\"\n      (click)=\"continue()\"\n    >\n      {{\n        value === propertyForm.value.value\n          ? ('label.close' | translate)\n          : ('label.next' | translate)\n      }}\n    </ax-button>\n  </div>\n</ng-container>\n\n<ng-container *ngIf=\"step === 1\">\n  <ax-property-otp-verification\n    [property]=\"profileProperty$ | async\"\n    (verificationComplete)=\"verificationComplete()\"\n  ></ax-property-otp-verification>\n</ng-container>\n", styles: [":host{display:block;padding:1rem}.button-wrapper{display:flex;justify-content:flex-end;gap:1rem;padding-top:.75rem}p{margin-top:0}ax-property-otp-verification{padding:0}\n"], dependencies: [{ kind: "directive", type: i2.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i3.ButtonComponent, selector: "ax-button", inputs: ["url", "ariaLabel", "target"] }, { kind: "component", type: i4.PropertyEditComponent, selector: "ax-property-edit", inputs: ["isIndividual", "isProposalSubscribeForm"] }, { kind: "component", type: i5.PropertyOtpVerificationComponent, selector: "ax-property-otp-verification", inputs: ["property", "canChangeValue"], outputs: ["propertyUpdate", "verificationComplete"] }, { kind: "pipe", type: i2.AsyncPipe, name: "async" }, { kind: "pipe", type: i6.TranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationComponent, decorators: [{
            type: Component,
            args: [{ selector: 'ax-profile-property-otp-verification', template: "<ng-container *ngIf=\"step === 0\">\n  <p>{{ description | translate }}</p>\n  <ax-property-edit\n    *ngIf=\"propertyForm?.value\"\n    [form]=\"propertyForm\"\n    [autofocus]=\"false\"\n    [required]=\"true\"\n    [autocompleteProperties]=\"(accountProperties$ | async) || undefined\"\n  ></ax-property-edit>\n\n  <div class=\"button-wrapper\">\n    <ax-button\n      filled\n      color=\"primary\"\n      [loading]=\"loading$ | async\"\n      [disabled]=\"(loading$ | async) ?? true\"\n      (click)=\"continue()\"\n    >\n      {{\n        value === propertyForm.value.value\n          ? ('label.close' | translate)\n          : ('label.next' | translate)\n      }}\n    </ax-button>\n  </div>\n</ng-container>\n\n<ng-container *ngIf=\"step === 1\">\n  <ax-property-otp-verification\n    [property]=\"profileProperty$ | async\"\n    (verificationComplete)=\"verificationComplete()\"\n  ></ax-property-otp-verification>\n</ng-container>\n", styles: [":host{display:block;padding:1rem}.button-wrapper{display:flex;justify-content:flex-end;gap:1rem;padding-top:.75rem}p{margin-top:0}ax-property-otp-verification{padding:0}\n"] }]
        }], ctorParameters: () => [{ type: i1.ProfileService }, { type: i1.ContextsService }], propDecorators: { type: [{
                type: Input
            }], value: [{
                type: Input
            }], description: [{
                type: Input
            }], done: [{
                type: Output
            }] } });

class ProfilePropertyOtpVerificationModule {
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
    static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationModule, declarations: [ProfilePropertyOtpVerificationComponent], imports: [CommonModule,
            ButtonModule,
            HelperPipesModule,
            PropertyEditModule,
            PropertyOtpVerificationModule], exports: [ProfilePropertyOtpVerificationComponent] }); }
    static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationModule, imports: [CommonModule,
            ButtonModule,
            HelperPipesModule,
            PropertyEditModule,
            PropertyOtpVerificationModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ProfilePropertyOtpVerificationModule, decorators: [{
            type: NgModule,
            args: [{
                    declarations: [ProfilePropertyOtpVerificationComponent],
                    imports: [
                        CommonModule,
                        ButtonModule,
                        HelperPipesModule,
                        PropertyEditModule,
                        PropertyOtpVerificationModule,
                    ],
                    exports: [ProfilePropertyOtpVerificationComponent],
                }]
        }] });

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

export { ProfilePropertyOtpVerificationComponent, ProfilePropertyOtpVerificationModule };
//# sourceMappingURL=aztrix-components-profile-property-otp-verification.mjs.map
