import { Component, Inject } from '@angular/core';
import { Validators, UntypedFormControl } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import * as i0 from "@angular/core";
import * as i1 from "../core";
import * as i2 from "@angular/forms";
import * as i3 from "../user-config";
import * as i4 from "@angular/material/dialog";
import * as i5 from "./role.service";
import * as i6 from "@angular/common";
import * as i7 from "@angular/material/button";
import * as i8 from "@angular/material/checkbox";
import * as i9 from "@angular/material/form-field";
import * as i10 from "@angular/material/input";
import * as i11 from "@angular/material/core";
import * as i12 from "@angular/material/select";
import * as i13 from "@angular/flex-layout/flex";
const errorMessages = {
    label: {
        required: 'Role is required'
    }
};
export class RoleFormComponent {
    constructor(alertService, credentialsService, fb, formValidatorService, userConfigService, dialogRef, data, roleService) {
        this.alertService = alertService;
        this.credentialsService = credentialsService;
        this.fb = fb;
        this.formValidatorService = formValidatorService;
        this.userConfigService = userConfigService;
        this.dialogRef = dialogRef;
        this.data = data;
        this.roleService = roleService;
        this.modules = [];
        this.isWorking = false;
        this.showModuleSelector = true;
        this.isUpdateMode = false;
        this.config = this.userConfigService.getConfig();
        this.showModuleSelector = this.config.roleConfig.showModuleSelectorOnForm;
    }
    get isAdminUser() {
        return (this.credentialsService.credentials?.user?.user_type_id === 1);
    }
    ngOnInit() {
        this.roleForm = this.fb.group({
            id: [''],
            label: new UntypedFormControl('', Validators.compose([Validators.required])),
            description: new UntypedFormControl(''),
            module_code: [''],
            is_editable: [true],
            is_global: [false],
            client_id: [this.data.client_id ? this.data.client_id : 1]
        });
        if (this.data.role) {
            this.roleForm.patchValue(this.data.role);
            this.isUpdateMode = true;
        }
        if (this.showModuleSelector) {
            this.getSubscribedModules();
        }
    }
    async save() {
        if (!this.roleForm.valid) {
            return;
        }
        this.isWorking = true;
        if (this.data.module_code) {
            this.roleForm.controls.module_code.setValue(this.data.module_code);
        }
        const result = this.isUpdateMode
            ? await this.roleService.updateRole(this.roleForm.value)
            : await this.roleService.createRole(this.roleForm.value);
        this.alertService.addAlert('Successfully saved the role', 'success');
        this.dialogRef.close(result);
        this.isWorking = false;
    }
    async getSubscribedModules() {
        let filters = {};
        filters.client_id = this.data.client_id;
        const result = await this.roleService.getClientSubscriptions(filters);
    }
    /** Method to evaluate form fields*/
    isFieldValid(field) {
        return this.formValidatorService.isFieldValid(field, this.roleForm);
    }
    /** Method to find error in form fields*/
    getErrorMessage(field) {
        return this.formValidatorService.getErrorMessage(field, this.roleForm, errorMessages);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RoleFormComponent, deps: [{ token: i1.AlertService }, { token: i1.CredentialsService }, { token: i2.UntypedFormBuilder }, { token: i1.FormValidatorService }, { token: i3.UserConfigService }, { token: i4.MatDialogRef }, { token: MAT_DIALOG_DATA }, { token: i5.RoleService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: RoleFormComponent, selector: "role-form", ngImport: i0, template: "<form [formGroup]=\"roleForm\" (ngSubmit)=\"save()\" class=\"p-3\">\n    <div fxLayout=\"column\">\n        <div mat-dialog-title class=\"text-center\">\n            <div>Role Form</div>\n        </div>\n        <mat-form-field fxFlex=\"100%\">\n            <input matInput formControlName=\"label\" placeholder=\"Role\" type=\"text\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('label')\">{{getErrorMessage('label')}}</mat-error>\n        </mat-form-field>\n        <mat-form-field fxFlex=\"100%\" *ngIf=\"showModuleSelector\">\n            <mat-label>Module</mat-label>\n            <mat-select formControlName=\"module_code\" required>\n                <mat-option *ngFor=\"let module of modules\" [value]=\"module.package?.module.code\">\n                    {{module.package?.module.name}}</mat-option>\n            </mat-select>\n            <mat-error>Module is required</mat-error>\n        </mat-form-field>\n        <mat-form-field fxFlex=\"100%\">\n            <textarea matInput placeholder=\"Description\" formControlName=\"description\"></textarea>\n        </mat-form-field>\n\n        <ng-container *ngIf=\"isAdminUser\">\n            <mat-checkbox formControlName=\"is_editable\"> Is Editable</mat-checkbox>\n            <mat-checkbox formControlName=\"is_global\"> Is Global</mat-checkbox>\n        </ng-container>\n\n        <div fxFlex=\"100%\" fxLayout fxLayoutAlign=\"end\" fxLayoutGap=\"1rem\">\n            <button mat-button class=\"mt-2\" type=\"button\" (click)=\"dialogRef.close()\">\n                Cancel\n            </button>\n            <button mat-raised-button color=\"primary\" class=\"mt-2\" type=\"submit\">\n                Save\n            </button>\n        </div>\n    </div>\n</form>\n", dependencies: [{ kind: "directive", type: i6.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i6.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "component", type: i7.MatButton, selector: "    button[mat-button], button[mat-raised-button], button[mat-flat-button],    button[mat-stroked-button]  ", exportAs: ["matButton"] }, { kind: "component", type: i8.MatCheckbox, selector: "mat-checkbox", inputs: ["aria-label", "aria-labelledby", "aria-describedby", "id", "required", "labelPosition", "name", "value", "disableRipple", "tabIndex", "color", "disabledInteractive", "checked", "disabled", "indeterminate"], outputs: ["change", "indeterminateChange"], exportAs: ["matCheckbox"] }, { kind: "directive", type: i4.MatDialogTitle, selector: "[mat-dialog-title], [matDialogTitle]", inputs: ["id"], exportAs: ["matDialogTitle"] }, { kind: "component", type: i9.MatFormField, selector: "mat-form-field", inputs: ["hideRequiredMarker", "color", "floatLabel", "appearance", "subscriptSizing", "hintLabel"], exportAs: ["matFormField"] }, { kind: "directive", type: i9.MatLabel, selector: "mat-label" }, { kind: "directive", type: i9.MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "directive", type: i10.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: i11.MatOption, selector: "mat-option", inputs: ["value", "id", "disabled"], outputs: ["onSelectionChange"], exportAs: ["matOption"] }, { kind: "component", type: i12.MatSelect, selector: "mat-select", inputs: ["aria-describedby", "panelClass", "disabled", "disableRipple", "tabIndex", "hideSingleSelectionIndicator", "placeholder", "required", "multiple", "disableOptionCentering", "compareWith", "value", "aria-label", "aria-labelledby", "errorStateMatcher", "typeaheadDebounceInterval", "sortComparator", "id", "panelWidth"], outputs: ["openedChange", "opened", "closed", "selectionChange", "valueChange"], exportAs: ["matSelect"] }, { kind: "directive", type: i13.DefaultLayoutDirective, selector: "  [fxLayout], [fxLayout.xs], [fxLayout.sm], [fxLayout.md],  [fxLayout.lg], [fxLayout.xl], [fxLayout.lt-sm], [fxLayout.lt-md],  [fxLayout.lt-lg], [fxLayout.lt-xl], [fxLayout.gt-xs], [fxLayout.gt-sm],  [fxLayout.gt-md], [fxLayout.gt-lg]", inputs: ["fxLayout", "fxLayout.xs", "fxLayout.sm", "fxLayout.md", "fxLayout.lg", "fxLayout.xl", "fxLayout.lt-sm", "fxLayout.lt-md", "fxLayout.lt-lg", "fxLayout.lt-xl", "fxLayout.gt-xs", "fxLayout.gt-sm", "fxLayout.gt-md", "fxLayout.gt-lg"] }, { kind: "directive", type: i13.DefaultLayoutGapDirective, selector: "  [fxLayoutGap], [fxLayoutGap.xs], [fxLayoutGap.sm], [fxLayoutGap.md],  [fxLayoutGap.lg], [fxLayoutGap.xl], [fxLayoutGap.lt-sm], [fxLayoutGap.lt-md],  [fxLayoutGap.lt-lg], [fxLayoutGap.lt-xl], [fxLayoutGap.gt-xs], [fxLayoutGap.gt-sm],  [fxLayoutGap.gt-md], [fxLayoutGap.gt-lg]", inputs: ["fxLayoutGap", "fxLayoutGap.xs", "fxLayoutGap.sm", "fxLayoutGap.md", "fxLayoutGap.lg", "fxLayoutGap.xl", "fxLayoutGap.lt-sm", "fxLayoutGap.lt-md", "fxLayoutGap.lt-lg", "fxLayoutGap.lt-xl", "fxLayoutGap.gt-xs", "fxLayoutGap.gt-sm", "fxLayoutGap.gt-md", "fxLayoutGap.gt-lg"] }, { kind: "directive", type: i13.DefaultLayoutAlignDirective, selector: "  [fxLayoutAlign], [fxLayoutAlign.xs], [fxLayoutAlign.sm], [fxLayoutAlign.md],  [fxLayoutAlign.lg], [fxLayoutAlign.xl], [fxLayoutAlign.lt-sm], [fxLayoutAlign.lt-md],  [fxLayoutAlign.lt-lg], [fxLayoutAlign.lt-xl], [fxLayoutAlign.gt-xs], [fxLayoutAlign.gt-sm],  [fxLayoutAlign.gt-md], [fxLayoutAlign.gt-lg]", inputs: ["fxLayoutAlign", "fxLayoutAlign.xs", "fxLayoutAlign.sm", "fxLayoutAlign.md", "fxLayoutAlign.lg", "fxLayoutAlign.xl", "fxLayoutAlign.lt-sm", "fxLayoutAlign.lt-md", "fxLayoutAlign.lt-lg", "fxLayoutAlign.lt-xl", "fxLayoutAlign.gt-xs", "fxLayoutAlign.gt-sm", "fxLayoutAlign.gt-md", "fxLayoutAlign.gt-lg"] }, { kind: "directive", type: i13.DefaultFlexDirective, selector: "  [fxFlex], [fxFlex.xs], [fxFlex.sm], [fxFlex.md],  [fxFlex.lg], [fxFlex.xl], [fxFlex.lt-sm], [fxFlex.lt-md],  [fxFlex.lt-lg], [fxFlex.lt-xl], [fxFlex.gt-xs], [fxFlex.gt-sm],  [fxFlex.gt-md], [fxFlex.gt-lg]", inputs: ["fxFlex", "fxFlex.xs", "fxFlex.sm", "fxFlex.md", "fxFlex.lg", "fxFlex.xl", "fxFlex.lt-sm", "fxFlex.lt-md", "fxFlex.lt-lg", "fxFlex.lt-xl", "fxFlex.gt-xs", "fxFlex.gt-sm", "fxFlex.gt-md", "fxFlex.gt-lg"] }, { kind: "directive", type: i2.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i2.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: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i2.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i2.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i2.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: RoleFormComponent, decorators: [{
            type: Component,
            args: [{ selector: 'role-form', template: "<form [formGroup]=\"roleForm\" (ngSubmit)=\"save()\" class=\"p-3\">\n    <div fxLayout=\"column\">\n        <div mat-dialog-title class=\"text-center\">\n            <div>Role Form</div>\n        </div>\n        <mat-form-field fxFlex=\"100%\">\n            <input matInput formControlName=\"label\" placeholder=\"Role\" type=\"text\" autocomplete=\"off\" required />\n            <mat-error *ngIf=\"isFieldValid('label')\">{{getErrorMessage('label')}}</mat-error>\n        </mat-form-field>\n        <mat-form-field fxFlex=\"100%\" *ngIf=\"showModuleSelector\">\n            <mat-label>Module</mat-label>\n            <mat-select formControlName=\"module_code\" required>\n                <mat-option *ngFor=\"let module of modules\" [value]=\"module.package?.module.code\">\n                    {{module.package?.module.name}}</mat-option>\n            </mat-select>\n            <mat-error>Module is required</mat-error>\n        </mat-form-field>\n        <mat-form-field fxFlex=\"100%\">\n            <textarea matInput placeholder=\"Description\" formControlName=\"description\"></textarea>\n        </mat-form-field>\n\n        <ng-container *ngIf=\"isAdminUser\">\n            <mat-checkbox formControlName=\"is_editable\"> Is Editable</mat-checkbox>\n            <mat-checkbox formControlName=\"is_global\"> Is Global</mat-checkbox>\n        </ng-container>\n\n        <div fxFlex=\"100%\" fxLayout fxLayoutAlign=\"end\" fxLayoutGap=\"1rem\">\n            <button mat-button class=\"mt-2\" type=\"button\" (click)=\"dialogRef.close()\">\n                Cancel\n            </button>\n            <button mat-raised-button color=\"primary\" class=\"mt-2\" type=\"submit\">\n                Save\n            </button>\n        </div>\n    </div>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.AlertService }, { type: i1.CredentialsService }, { type: i2.UntypedFormBuilder }, { type: i1.FormValidatorService }, { type: i3.UserConfigService }, { type: i4.MatDialogRef }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [MAT_DIALOG_DATA]
                }] }, { type: i5.RoleService }] });
//# sourceMappingURL=data:application/json;base64, 