import { Component, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { Validators } from '@angular/forms';
import { AppRegex } from '../../validations/AppRegex';
import { AppValidators } from '../../validations/AppValidators';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "../../services/util/util.service";
import * as i3 from "../../services/login/login.service";
import * as i4 from "@ionic/angular";
import * as i5 from "@angular/common";
import * as i6 from "primeng/api";
import * as i7 from "@angular/material/form-field";
import * as i8 from "primeng/dialog";
import * as i9 from "primeng/inputtext";
import * as i10 from "../error/error.component";
import * as i11 from "../../pipes/mask/mask.email.pipe";
const _c0 = ["authCodeFormDirective"];
function EmailUpdateComponent_div_5_mat_error_15_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "mat-error");
    i0.ɵɵtext(1);
    i0.ɵɵelement(2, "br")(3, "br");
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r6 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance(1);
    i0.ɵɵtextInterpolate(ctx_r6.errorDtls);
} }
function EmailUpdateComponent_div_5_p_16_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "p", 15);
    i0.ɵɵtext(1);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r7 = i0.ɵɵnextContext(2);
    i0.ɵɵadvance(1);
    i0.ɵɵtextInterpolate(ctx_r7.successDtls);
} }
function EmailUpdateComponent_div_5_p_17_Template(rf, ctx) { if (rf & 1) {
    const _r11 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "p");
    i0.ɵɵtext(1, "If you didn't get the verification code ");
    i0.ɵɵelementStart(2, "a", 16);
    i0.ɵɵlistener("click", function EmailUpdateComponent_div_5_p_17_Template_a_click_2_listener() { i0.ɵɵrestoreView(_r11); const ctx_r10 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r10.getNewCode()); });
    i0.ɵɵtext(3, "click here");
    i0.ɵɵelementEnd();
    i0.ɵɵtext(4, " and we'll send it again.");
    i0.ɵɵelementEnd();
} }
function EmailUpdateComponent_div_5_p_18_Template(rf, ctx) { if (rf & 1) {
    const _r13 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "p")(1, "a", 16);
    i0.ɵɵlistener("click", function EmailUpdateComponent_div_5_p_18_Template_a_click_1_listener() { i0.ɵɵrestoreView(_r13); const ctx_r12 = i0.ɵɵnextContext(2); return i0.ɵɵresetView(ctx_r12.setMode("2")); });
    i0.ɵɵtext(2, "Update email address");
    i0.ɵɵelementEnd()();
} }
const _c1 = () => ({ required: "Code is required." });
function EmailUpdateComponent_div_5_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 8)(1, "p");
    i0.ɵɵtext(2, "A verification code has been sent to your email address:\u00A0 ");
    i0.ɵɵelementStart(3, "b");
    i0.ɵɵtext(4);
    i0.ɵɵpipe(5, "maskEmail");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(6, "p");
    i0.ɵɵtext(7, " Check your junk folder if you don't see it in your inbox. ");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(8, "div", 9)(9, "div", 10)(10, "label", 11);
    i0.ɵɵtext(11, "Enter code");
    i0.ɵɵelementEnd();
    i0.ɵɵelement(12, "br")(13, "input", 12)(14, "app-error", 13);
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(15, EmailUpdateComponent_div_5_mat_error_15_Template, 4, 1, "mat-error", 7)(16, EmailUpdateComponent_div_5_p_16_Template, 2, 1, "p", 14);
    i0.ɵɵelementEnd();
    i0.ɵɵtemplate(17, EmailUpdateComponent_div_5_p_17_Template, 5, 0, "p", 7)(18, EmailUpdateComponent_div_5_p_18_Template, 3, 0, "p", 7);
    i0.ɵɵelementEnd();
} if (rf & 2) {
    const ctx_r1 = i0.ɵɵnextContext();
    const _r0 = i0.ɵɵreference(2);
    i0.ɵɵadvance(4);
    i0.ɵɵtextInterpolate(i0.ɵɵpipeBind1(5, 9, ctx_r1.email));
    i0.ɵɵadvance(9);
    i0.ɵɵproperty("id", "authCode");
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("field", ctx_r1.authCode)("form", _r0)("errDef", i0.ɵɵpureFunction0(11, _c1));
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r1.errorDtls);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", ctx_r1.successDtls);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !ctx_r1.blocked);
    i0.ɵɵadvance(1);
    i0.ɵɵproperty("ngIf", !ctx_r1.blocked);
} }
const _c2 = () => ({ required: "New email is required.", email: "Invalid email." });
function EmailUpdateComponent_div_6_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 8)(1, "div", 9)(2, "div", 10)(3, "label");
    i0.ɵɵtext(4, "Enter new email address");
    i0.ɵɵelementEnd();
    i0.ɵɵelement(5, "br")(6, "input", 17)(7, "app-error", 13);
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(8, "label", 18);
    i0.ɵɵtext(9, "A new verification code will be sent to above email address.");
    i0.ɵɵelementEnd()()();
} if (rf & 2) {
    const ctx_r2 = i0.ɵɵnextContext();
    const _r0 = i0.ɵɵreference(2);
    i0.ɵɵadvance(7);
    i0.ɵɵproperty("field", ctx_r2.newEmail)("form", _r0)("errDef", i0.ɵɵpureFunction0(3, _c2));
} }
function EmailUpdateComponent_div_9_Template(rf, ctx) { if (rf & 1) {
    i0.ɵɵelementStart(0, "div", 19);
    i0.ɵɵelement(1, "ion-spinner", 20);
    i0.ɵɵelementEnd();
} }
function EmailUpdateComponent_div_10_Template(rf, ctx) { if (rf & 1) {
    const _r15 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "div", 21)(1, "ion-button", 22);
    i0.ɵɵlistener("click", function EmailUpdateComponent_div_10_Template_ion_button_click_1_listener() { i0.ɵɵrestoreView(_r15); const ctx_r14 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r14.close()); });
    i0.ɵɵtext(2, "Close");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(3, "ion-button", 23);
    i0.ɵɵlistener("click", function EmailUpdateComponent_div_10_Template_ion_button_click_3_listener() { i0.ɵɵrestoreView(_r15); const ctx_r16 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r16.verifyEmail()); });
    i0.ɵɵtext(4, "Verify");
    i0.ɵɵelementEnd()();
} }
function EmailUpdateComponent_ion_row_11_Template(rf, ctx) { if (rf & 1) {
    const _r18 = i0.ɵɵgetCurrentView();
    i0.ɵɵelementStart(0, "ion-row")(1, "ion-col", 24)(2, "ion-button", 25);
    i0.ɵɵlistener("click", function EmailUpdateComponent_ion_row_11_Template_ion_button_click_2_listener() { i0.ɵɵrestoreView(_r18); const ctx_r17 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r17.setMode("1")); });
    i0.ɵɵtext(3, "Back");
    i0.ɵɵelementEnd()();
    i0.ɵɵelementStart(4, "ion-col", 21)(5, "ion-button", 22);
    i0.ɵɵlistener("click", function EmailUpdateComponent_ion_row_11_Template_ion_button_click_5_listener() { i0.ɵɵrestoreView(_r18); const ctx_r19 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r19.close()); });
    i0.ɵɵtext(6, "Close");
    i0.ɵɵelementEnd();
    i0.ɵɵelementStart(7, "ion-button", 23);
    i0.ɵɵlistener("click", function EmailUpdateComponent_ion_row_11_Template_ion_button_click_7_listener() { i0.ɵɵrestoreView(_r18); const ctx_r20 = i0.ɵɵnextContext(); return i0.ɵɵresetView(ctx_r20.updateEmailAndGetNewCode()); });
    i0.ɵɵtext(8, "Get code");
    i0.ɵɵelementEnd()()();
} }
const _c3 = () => ({ width: "50vw" });
const _c4 = () => ({ "960px": "60vw", "640px": "100vw" });
export class EmailUpdateComponent {
    constructor(fb, util, ls) {
        this.fb = fb;
        this.util = util;
        this.ls = ls;
        //1 - Verification
        //2 - Email update
        this.mode = '1';
        this.onSuccess = new EventEmitter();
        this.onClose = new EventEmitter();
    }
    ngOnInit() {
        this.authCodeForm = this.fb.group({
            'authCode': [null, Validators.required],
            'newEmail': [null]
        });
    }
    get authCode() {
        return this.authCodeForm.controls['authCode'];
    }
    get newEmail() {
        return this.authCodeForm.controls['newEmail'];
    }
    close() {
        this.onClose.emit();
    }
    setMode(mode) {
        if (mode == '1') {
            this.util.clearValidation(this.newEmail);
            this.util.setRequiredValidator(this.authCode);
            this.mode = mode;
        }
        if (mode == '2') {
            this.util.clearValidation(this.authCode);
            this.util.setValidation(this.newEmail, [Validators.required, AppValidators.match(AppRegex.EMAIL_REGEX, { email: true })]);
            this.mode = mode;
        }
    }
    verifyEmail() {
        if (this.authCodeForm.valid) {
            this.resetMsg();
            this.blocked = true;
            this.ls.verifyEmail(this.authCode.value)
                .subscribe(res => {
                this.successDtls = res['message'];
                this.ls
                    .refreshToken(this.ls.getUser().refresh_token)
                    .then(async (res) => {
                    await this.ls.setUser(res);
                    setTimeout(() => this.onSuccess.emit(), 1000);
                })
                    .catch(err => {
                    this.blocked = false;
                    this.errorDtls = 'Could not process your request. Please try again later.';
                });
            }, err => {
                this.errorDtls = err.msg[0];
                this.blocked = false;
            });
        }
    }
    resetMsg() {
        this.errorDtls = null;
        this.successDtls = null;
        this.blocked = false;
    }
    getNewCode() {
        this.resetMsg();
        this.blocked = true;
        this.ls.getNewCode()
            .subscribe(res => {
            this.successDtls = res['message'];
            this.blocked = false;
        }, err => {
            this.errorDtls = 'Could not get new code. Please try again.';
            this.blocked = false;
        });
    }
    updateEmailAndGetNewCode() {
        if (this.newEmail.valid) {
            this.resetMsg();
            this.blocked = true;
            this.ls.updateEmailAndGetNewCode(this.newEmail.value.trim())
                .subscribe(res => {
                this.successDtls = res['message'];
                this.setMode('1');
                this.email = this.newEmail.value;
                this.blocked = false;
            }, err => {
                this.errorDtls = err.msg[0];
                this.blocked = false;
            });
        }
    }
    static { this.ɵfac = function EmailUpdateComponent_Factory(t) { return new (t || EmailUpdateComponent)(i0.ɵɵdirectiveInject(i1.UntypedFormBuilder), i0.ɵɵdirectiveInject(i2.UtilService), i0.ɵɵdirectiveInject(i3.LoginService)); }; }
    static { this.ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: EmailUpdateComponent, selectors: [["app-email-update"]], viewQuery: function EmailUpdateComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.authCodeFormDirective = _t.first);
        } }, inputs: { email: "email" }, outputs: { onSuccess: "onSuccess", onClose: "onClose" }, decls: 12, vars: 12, consts: [["visible", "true", "modal", "true", "appendTo", "body", "position", "top", 3, "breakpoints"], [3, "formGroup"], ["authCodeFormDirective", "ngForm"], [1, "heading", "ion-padding-left"], ["class", "ion-padding-left", 4, "ngIf"], ["class", "ion-text-center", 4, "ngIf"], ["class", "ion-text-right", 4, "ngIf"], [4, "ngIf"], [1, "ion-padding-left"], [1, "p-formgrid"], [1, "field"], ["for", "authCode"], ["type", "text", "pInputText", "", "formControlName", "authCode", "size", "8", "maxlength", "8", 3, "id"], [3, "field", "form", "errDef"], ["style", "color: var(--ion-color-green); font-weight: bold;", 4, "ngIf"], [2, "color", "var(--ion-color-green)", "font-weight", "bold"], [1, "link", 3, "click"], ["id", "email", "formControlName", "newEmail", "pInputText", "", "type", "email", "maxlength", "80", "minlength", "6", "size", "30"], [1, "ion-padding-top"], [1, "ion-text-center"], ["name", "dots"], [1, "ion-text-right"], ["shape", "round", "type", "button", "color", "primary", "fill", "outline", 3, "click"], ["shape", "round", "type", "submit", "color", "primary", 3, "click"], ["size", "3", 1, "ion-text-left", 2, "padding-left", "0px"], ["shape", "round", "type", "reset", "color", "primary", "fill", "outline", 3, "click"]], template: function EmailUpdateComponent_Template(rf, ctx) { if (rf & 1) {
            i0.ɵɵelementStart(0, "p-dialog", 0)(1, "form", 1, 2)(3, "h4", 3);
            i0.ɵɵtext(4);
            i0.ɵɵelementEnd();
            i0.ɵɵtemplate(5, EmailUpdateComponent_div_5_Template, 19, 12, "div", 4)(6, EmailUpdateComponent_div_6_Template, 10, 4, "div", 4);
            i0.ɵɵelementStart(7, "p-footer")(8, "div");
            i0.ɵɵtemplate(9, EmailUpdateComponent_div_9_Template, 2, 0, "div", 5)(10, EmailUpdateComponent_div_10_Template, 5, 0, "div", 6)(11, EmailUpdateComponent_ion_row_11_Template, 9, 0, "ion-row", 7);
            i0.ɵɵelementEnd()()()();
        } if (rf & 2) {
            i0.ɵɵstyleMap(i0.ɵɵpureFunction0(10, _c3));
            i0.ɵɵproperty("breakpoints", i0.ɵɵpureFunction0(11, _c4));
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("formGroup", ctx.authCodeForm);
            i0.ɵɵadvance(3);
            i0.ɵɵtextInterpolate1(" ", ctx.mode == "1" ? "Activate your account" : "Email update", " ");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", ctx.mode == "1");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", ctx.mode == "2");
            i0.ɵɵadvance(3);
            i0.ɵɵproperty("ngIf", ctx.blocked);
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", !ctx.blocked && ctx.mode == "1");
            i0.ɵɵadvance(1);
            i0.ɵɵproperty("ngIf", !ctx.blocked && ctx.mode == "2");
        } }, dependencies: [i4.IonButton, i4.IonCol, i4.IonRow, i4.IonSpinner, i5.NgIf, i6.Footer, i7.MatError, i1.ɵNgNoValidate, i1.DefaultValueAccessor, i1.NgControlStatus, i1.NgControlStatusGroup, i1.MinLengthValidator, i1.MaxLengthValidator, i1.FormGroupDirective, i1.FormControlName, i8.Dialog, i9.InputText, i10.ErrorComponent, i11.MaskEmailPipe] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(EmailUpdateComponent, [{
        type: Component,
        args: [{ selector: 'app-email-update', template: "<p-dialog visible=\"true\" modal=\"true\" appendTo=\"body\" [breakpoints]=\"{'960px': '60vw', '640px': '100vw'}\" [style]=\"{width: '50vw'}\" position=\"top\">\r\n    <form [formGroup]=\"authCodeForm\" #authCodeFormDirective=\"ngForm\">\r\n        <h4 class=\"heading ion-padding-left\">\r\n            {{mode == '1' ? 'Activate your account' : 'Email update'}}\r\n        </h4>\r\n\r\n        <div class=\"ion-padding-left\" *ngIf=\"mode == '1'\">\r\n            <p>A verification code has been sent to your email address:&nbsp; <b>{{email | maskEmail}}</b>\r\n            </p>\r\n            <p>\r\n                Check your junk folder if you don't see it in your inbox.\r\n            </p>\r\n    \r\n            <div class=\"p-formgrid\">\r\n                <div class=\"field\">\r\n                    <label for=\"authCode\">Enter code</label><br/>\r\n                    <input type=\"text\" pInputText formControlName=\"authCode\" size=\"8\" maxlength=\"8\" [id]=\"'authCode'\"/>\r\n                    <app-error [field]='authCode' [form]=\"authCodeFormDirective\" [errDef]=\"{ \r\n                        required: 'Code is required.'\r\n                      }\"></app-error>\r\n                </div>\r\n                <mat-error *ngIf=\"errorDtls\">{{errorDtls}}<br/><br/></mat-error>\r\n                <p *ngIf=\"successDtls\" style=\"color: var(--ion-color-green); font-weight: bold;\">{{successDtls}}</p>\r\n            </div>\r\n            <p *ngIf=\"!blocked\">If you didn't get the verification code <a class=\"link\" (click)=\"getNewCode()\">click here</a> and we'll send it again.</p>\r\n            <p *ngIf=\"!blocked\"><a class=\"link\" (click)=\"setMode('2')\">Update email address</a></p>\r\n        </div>\r\n    \r\n        <div class=\"ion-padding-left\" *ngIf=\"mode == '2'\">\r\n            <div class=\"p-formgrid\">\r\n                <div class=\"field\">\r\n                    <label>Enter new email address</label><br/>\r\n                    <input id=\"email\" formControlName=\"newEmail\" pInputText type=\"email\" maxlength=\"80\" minlength=\"6\" size=\"30\"/>\r\n                    <app-error [field]='newEmail' [form]=\"authCodeFormDirective\" [errDef]=\"{ \r\n                        required: 'New email is required.',\r\n                        email: 'Invalid email.'\r\n                      }\"></app-error>\r\n                </div>\r\n                <label class=\"ion-padding-top\">A new verification code will be sent to above email address.</label>\r\n            </div>\r\n        </div>\r\n    \r\n        <p-footer >\r\n            <div>\r\n                <div *ngIf=\"blocked\" class=\"ion-text-center\">\r\n                    <ion-spinner name=\"dots\"></ion-spinner>\r\n                </div>\r\n                <div class=\"ion-text-right\" *ngIf=\"!blocked && mode == '1'\">\r\n                    <ion-button shape=\"round\" type=\"button\"  color=\"primary\" fill=\"outline\" (click)=\"close()\">Close</ion-button>\r\n                    <ion-button shape=\"round\" type=\"submit\"  color=\"primary\" (click)=\"verifyEmail()\">Verify</ion-button>\r\n                </div>\r\n                <ion-row *ngIf=\"!blocked && mode == '2'\">\r\n                    <ion-col class=\"ion-text-left\" style=\"padding-left: 0px;\" size=\"3\">\r\n                        <ion-button shape=\"round\" type=\"reset\"  color=\"primary\" fill=\"outline\" (click)=\"setMode('1')\">Back</ion-button>\r\n                    </ion-col>\r\n                    <ion-col class=\"ion-text-right\">\r\n                        <ion-button shape=\"round\" type=\"button\"  color=\"primary\" fill=\"outline\" (click)=\"close()\">Close</ion-button>\r\n                    <ion-button shape=\"round\" type=\"submit\"  color=\"primary\" (click)=\"updateEmailAndGetNewCode()\">Get code</ion-button>\r\n                    </ion-col>\r\n                </ion-row>\r\n            </div>\r\n        </p-footer>\r\n    </form>\r\n</p-dialog>" }]
    }], () => [{ type: i1.UntypedFormBuilder }, { type: i2.UtilService }, { type: i3.LoginService }], { authCodeFormDirective: [{
            type: ViewChild,
            args: ['authCodeFormDirective', { static: false }]
        }], onSuccess: [{
            type: Output
        }], onClose: [{
            type: Output
        }], email: [{
            type: Input
        }] }); })();
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(EmailUpdateComponent, { className: "EmailUpdateComponent", filePath: "lib\\components\\email-update\\email-update.component.ts", lineNumber: 13 }); })();
//# sourceMappingURL=data:application/json;base64, 