import { Component, Output, EventEmitter } from '@angular/core';
import { Validators, UntypedFormControl } from '@angular/forms';
import * as i0 from "@angular/core";
import * as i1 from "../core";
import * as i2 from "../user";
import * as i3 from "@angular/forms";
import * as i4 from "@angular/common";
import * as i5 from "@angular/material/button";
import * as i6 from "@angular/material/form-field";
import * as i7 from "@angular/material/input";
import * as i8 from "@angular/material/divider";
const errorMessages = {
    name: {
        required: 'Name is required'
    },
    email: {
        required: 'Email is required',
        email: 'Invalid email'
    },
};
/**
 * Component to edit a user
 *
 * @author Saravana Kumar
 * @since 20190719
 */
export class ProfileEditComponent {
    constructor(http, alertService, errorHandler, currentUserService, fb, formValidatorService) {
        this.http = http;
        this.alertService = alertService;
        this.errorHandler = errorHandler;
        this.currentUserService = currentUserService;
        this.fb = fb;
        this.formValidatorService = formValidatorService;
        this.isWorking = false;
        this.onSave = new EventEmitter();
        this.profileForm = fb.group({
            name: new UntypedFormControl('', Validators.required),
            email: new UntypedFormControl('', [Validators.required, Validators.email]),
        });
    }
    ngOnInit() {
        this.user = this.currentUserService.getUser();
        this.profileForm.patchValue(this.user);
    }
    save() {
        if (!this.profileForm.valid) {
            return;
        }
        this.isWorking = true;
        this.http.put('api/users/profile/update', this.profileForm.value).toPromise().then((res) => {
            this.onSave.emit(res.item);
            this.currentUserService.updateCurrentUser(res.item);
            this.alertService.addAlert('Successfully updated the user. ', 'success');
            this.isWorking = false;
        });
    }
    /** Method to evaluate form fields*/
    isFieldValid(field) {
        return this.formValidatorService.isFieldValid(field, this.profileForm);
    }
    /** Method to find error in form fields*/
    getErrorMessage(field) {
        return this.formValidatorService.getErrorMessage(field, this.profileForm, errorMessages);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ProfileEditComponent, deps: [{ token: i1.HttpService }, { token: i1.AlertService }, { token: i1.ErrorHandlerService }, { token: i2.CurrentUserService }, { token: i3.UntypedFormBuilder }, { token: i1.FormValidatorService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: ProfileEditComponent, selector: "profile-edit", outputs: { onSave: "onSave" }, ngImport: i0, template: "<form class=\"form-horizontal form-bordered form-medium mx-auto mt-4\" [formGroup]=\"profileForm\" (ngSubmit)=\"save()\">\n    <h3 class=\"section title text-center\">\n        Edit Profile\n    </h3>\n    <div class=\"form-group\" *ngIf=\"user.temporarily_invited\">\n        <small class=\"form-text text-muted\">\n            Please fill your name and you\u2019re currently using a temporary password, please change it to proceed\n        </small>\n    </div>\n    <mat-divider></mat-divider>\n\n    <div class=\"form-group row mt-3\">\n        <mat-form-field class=\"offset-2 col-8\">\n            <input matInput formControlName=\"name\" placeholder=\"Full Name\" type=\"text\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('name')\">{{getErrorMessage('name')}}</mat-error>\n        </mat-form-field>\n    </div>\n\n    <div class=\"form-group row\">\n        <mat-form-field class=\"offset-2 col-8\">\n            <input matInput formControlName=\"email\" placeholder=\"Email\" type=\"email\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('email')\">{{getErrorMessage('email')}}</mat-error>\n        </mat-form-field>\n    </div>\n\n    <mat-divider></mat-divider>\n\n    <div class=\"form-group mt-3\">\n        <div class=\"col-sm-12 text-center\">\n            <button mat-button class=\"btn btn-md btn-primary\" type=\"submit\" [disabled]=\"isWorking\">Save</button>\n        </div>\n    </div>\n\n</form>", dependencies: [{ kind: "directive", type: i4.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i3.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i3.DefaultValueAccessor, selector: "input:not([type=checkbox])[formControlName],textarea[formControlName],input:not([type=checkbox])[formControl],textarea[formControl],input:not([type=checkbox])[ngModel],textarea[ngModel],[ngDefaultControl]" }, { kind: "directive", type: i3.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i3.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { 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.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i3.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "component", type: i5.MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "component", type: i6.MatFormField, selector: "mat-form-field", inputs: ["hideRequiredMarker", "color", "floatLabel", "appearance", "subscriptSizing", "hintLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: i6.MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "directive", type: i7.MatInput, selector: "input[matInput], textarea[matInput], select[matNativeControl],      input[matNativeControl], textarea[matNativeControl]", inputs: ["disabled", "id", "placeholder", "name", "required", "type", "errorStateMatcher", "aria-describedby", "value", "readonly"], exportAs: ["matInput"] }, { kind: "component", type: i8.MatDivider, selector: "mat-divider", inputs: ["vertical", "inset"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ProfileEditComponent, decorators: [{
            type: Component,
            args: [{ selector: 'profile-edit', template: "<form class=\"form-horizontal form-bordered form-medium mx-auto mt-4\" [formGroup]=\"profileForm\" (ngSubmit)=\"save()\">\n    <h3 class=\"section title text-center\">\n        Edit Profile\n    </h3>\n    <div class=\"form-group\" *ngIf=\"user.temporarily_invited\">\n        <small class=\"form-text text-muted\">\n            Please fill your name and you\u2019re currently using a temporary password, please change it to proceed\n        </small>\n    </div>\n    <mat-divider></mat-divider>\n\n    <div class=\"form-group row mt-3\">\n        <mat-form-field class=\"offset-2 col-8\">\n            <input matInput formControlName=\"name\" placeholder=\"Full Name\" type=\"text\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('name')\">{{getErrorMessage('name')}}</mat-error>\n        </mat-form-field>\n    </div>\n\n    <div class=\"form-group row\">\n        <mat-form-field class=\"offset-2 col-8\">\n            <input matInput formControlName=\"email\" placeholder=\"Email\" type=\"email\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('email')\">{{getErrorMessage('email')}}</mat-error>\n        </mat-form-field>\n    </div>\n\n    <mat-divider></mat-divider>\n\n    <div class=\"form-group mt-3\">\n        <div class=\"col-sm-12 text-center\">\n            <button mat-button class=\"btn btn-md btn-primary\" type=\"submit\" [disabled]=\"isWorking\">Save</button>\n        </div>\n    </div>\n\n</form>" }]
        }], ctorParameters: () => [{ type: i1.HttpService }, { type: i1.AlertService }, { type: i1.ErrorHandlerService }, { type: i2.CurrentUserService }, { type: i3.UntypedFormBuilder }, { type: i1.FormValidatorService }], propDecorators: { onSave: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 