import { Component, EventEmitter, Input, Output } from '@angular/core';
import { UntypedFormControl, Validators } from '@angular/forms';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "../../core/form-validator.service";
import * as i3 from "@angular/common";
import * as i4 from "@angular/flex-layout/flex";
import * as i5 from "@angular/material/button";
import * as i6 from "@angular/material/form-field";
import * as i7 from "@angular/material/icon";
import * as i8 from "@angular/material/core";
import * as i9 from "@angular/material/select";
import * as i10 from "@angular/material/divider";
import * as i11 from "@angular/material/tooltip";
const errorMessages = {
    property: {
        required: "Property is required",
        unavailable: "Duplicate entry"
    },
};
export class FieldMappingComponent {
    constructor(fb, formValidatorService, changeDetectorRef, location) {
        this.fb = fb;
        this.formValidatorService = formValidatorService;
        this.changeDetectorRef = changeDetectorRef;
        this.location = location;
        this.firstDataArray = [];
        this.properties = [];
        this.onSave = new EventEmitter();
        this.onReUpload = new EventEmitter();
        this.createImportMappingForm();
    }
    ngOnInit() {
        this.properties = this.config.properties;
        ;
        this.reFectorAttributes(this.data[0]);
    }
    createImportMappingForm() {
        this.importMappingForm = this.fb.group({
            fields: this.fb.array([]),
        });
    }
    get fieldsArray() {
        return this.importMappingForm.get('fields');
    }
    createField(field) {
        var fieldForm = this.fb.group({
            id: new UntypedFormControl(field.id),
            label: new UntypedFormControl(field.label),
            value: new UntypedFormControl(field.value),
            property: new UntypedFormControl(field.property, Validators.compose([
                Validators.required
            ])),
            propertyType: new UntypedFormControl(field.propertyType)
        });
        return fieldForm;
    }
    reFectorAttributes(data) {
        for (var k in data) {
            if (data.hasOwnProperty(k)) {
                var dataValue = {
                    id: '',
                    label: k,
                    value: data[k],
                    property: '',
                    propertyType: ''
                };
                this.firstDataArray.push(dataValue);
            }
        }
        this.firstDataArray.forEach((element, mainIndex) => {
            element.id = mainIndex;
            this.properties.find((property, index) => {
                if (property.label.toLowerCase().includes(element.label.toLowerCase())) {
                    element.property = property.value;
                    element.propertyType = property.type;
                    return element;
                }
            });
        });
    }
    next() {
        this.importMappingForm.markAllAsTouched();
        if (!this.importMappingForm.valid) {
            return;
        }
        this.onSave.emit(this.importMappingForm.value);
    }
    /** Method to evaluate form fields*/
    isFieldValid(field, form) {
        return this.formValidatorService.isFieldValid(field, form);
    }
    /** Method to find error in form fields*/
    getErrorMessage(field, form) {
        return this.formValidatorService.getErrorMessage(field, form, errorMessages);
    }
    ngAfterViewInit() {
        //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
        //Add 'implements AfterViewInit' to the class.
        setTimeout(x => {
            this.firstDataArray.forEach((element) => {
                this.fieldsArray.push(this.createField(element));
            });
            this.changeDetectorRef.detectChanges();
        }, 500);
    }
    updatePropertyType(event, index) {
        var field = this.properties.find((field) => field.value == event.value);
        if (field) {
            this.fieldsArray.controls[index].get('propertyType').setValue(field.type);
            this.uniqueValidator();
        }
    }
    uniqueValidator() {
        this.fieldsArray.value.forEach((field, index) => {
            var control = this.fieldsArray.controls[index].get('property');
            const isNameUnique = this.fieldsArray.value.find(propertyData => propertyData.property === control.value
                && propertyData.id != this.fieldsArray.controls[index].get('id').value)
                ? false
                : true;
            if (!isNameUnique) {
                control.setErrors({ unavailable: true });
            }
            else {
                control.setErrors(null);
            }
        });
    }
    removeField(index) {
        if (index == -1) {
            return;
        }
        this.fieldsArray.removeAt(index);
    }
    reUpload() {
        this.onReUpload.emit(true);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FieldMappingComponent, deps: [{ token: i1.UntypedFormBuilder }, { token: i2.FormValidatorService }, { token: i0.ChangeDetectorRef }, { token: i3.Location }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: FieldMappingComponent, selector: "app-field-mapping", inputs: { data: "data", config: "config" }, outputs: { onSave: "onSave", onReUpload: "onReUpload" }, ngImport: i0, template: "<div fxLayout=\"column\" class=\"mt-2\">\n  <div fxLayout=\"row wrap\" class=\"pb-3\">\n    <h5 fxFlex=\"30%\" class=\"primary-color\">Summary</h5>\n    <div fxFlex=\"70%\" fxLayoutAlign=\"center center\">\n      <h5 class=\"secondary-color\">\n        <span class=\"font-weight-bold\">{{data.length}}</span>\n      </h5>\n      <span class=\"text-muted ml-2\"> Records Found In Uploaded File</span>\n    </div>\n  </div>\n\n  <div>\n    <mat-divider></mat-divider>\n  </div>\n\n  <form [formGroup]=\"importMappingForm\" (ngSubmit)=\"next()\" fxLayout=\"column\" class=\"pt-3\">\n    <h5 class=\"primary-color pb-2\">Properties</h5>\n    <table style=\"width: 100%;\" class=\"table table-sm table-striped\">\n      <tr class=\"table-header-row\">\n        <th>Column Headers From File</th>\n        <th>Preview Information</th>\n        <th>Property</th>\n      </tr>\n      <ng-container formArrayName=\"fields\">\n        <tr *ngFor=\"let field of fieldsArray.controls; let i = index\" [formGroupName]=\"i\">\n          <td>{{ field.get('label').value }}</td>\n          <td>{{ field.get('value').value }}</td>\n          <td>\n            <mat-form-field fxFlex=\"100%\" appearance=\"outline\">\n              <mat-label>Select Property</mat-label>\n              <mat-select placeholder=\"Select Property\" formControlName=\"property\" required\n                (selectionChange)=\"updatePropertyType($event, i)\">\n                <mat-option *ngFor=\"let property of properties\" [value]=\"property.value\">\n                  {{property.label}}\n                </mat-option>\n              </mat-select>\n              <mat-error *ngIf=\"isFieldValid('property', fieldsArray.controls[i])\">\n                {{getErrorMessage('property', fieldsArray.controls[i])}}\n              </mat-error>\n            </mat-form-field>\n            <button type=\"button\" mat-icon-button (click)=\"removeField(i)\" *ngIf=\"fieldsArray.length > 1\"\n              matTooltip=\"Remove column\" matTooltipPosition=\"right\">\n              <mat-icon class=\"text-danger\">remove_circle</mat-icon>\n            </button>\n          </td>\n        </tr>\n      </ng-container>\n    </table>\n\n    <div class=\"mt-1 d-flex flex-row gap-2 justify-content-end align-items-center\">\n      <button mat-button class=\"mt-2 mr-2\" type=\"button\" (click)=\"location.back()\">\n        Cancel\n      </button>\n      <button mat-stroked-button class=\"mt-2 mr-2 primary-color\" type=\"button\" (click)=\"reUpload()\">\n        <mat-icon>\n          <span class=\"material-icons-outlined\">\n            file_upload\n          </span>\n        </mat-icon>\n        Re-upload\n      </button>\n      <button mat-stroked-button class=\"mt-2 primary-bg\" type=\"submit\">\n        Next\n        <mat-icon>\n          chevron_right\n        </mat-icon>\n      </button>\n    </div>\n  </form>\n</div>", styles: ["table{width:100%}th,td{padding:1%}.table-header-row{background-color:#e6e4e4;color:#000;font-weight:bolder}\n"], dependencies: [{ kind: "directive", type: i3.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i1.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i1.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i1.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i1.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "directive", type: i1.FormGroupName, selector: "[formGroupName]", inputs: ["formGroupName"] }, { kind: "directive", type: i1.FormArrayName, selector: "[formArrayName]", inputs: ["formArrayName"] }, { kind: "directive", type: i4.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: i4.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: i4.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: "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: i5.MatIconButton, selector: "button[mat-icon-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.MatLabel, selector: "mat-label" }, { kind: "directive", type: i6.MatError, selector: "mat-error, [matError]", inputs: ["id"] }, { kind: "component", type: i7.MatIcon, selector: "mat-icon", inputs: ["color", "inline", "svgIcon", "fontSet", "fontIcon"], exportAs: ["matIcon"] }, { kind: "component", type: i8.MatOption, selector: "mat-option", inputs: ["value", "id", "disabled"], outputs: ["onSelectionChange"], exportAs: ["matOption"] }, { kind: "component", type: i9.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: "component", type: i10.MatDivider, selector: "mat-divider", inputs: ["vertical", "inset"] }, { kind: "directive", type: i11.MatTooltip, selector: "[matTooltip]", inputs: ["matTooltipPosition", "matTooltipPositionAtOrigin", "matTooltipDisabled", "matTooltipShowDelay", "matTooltipHideDelay", "matTooltipTouchGestures", "matTooltip", "matTooltipClass"], exportAs: ["matTooltip"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: FieldMappingComponent, decorators: [{
            type: Component,
            args: [{ selector: 'app-field-mapping', template: "<div fxLayout=\"column\" class=\"mt-2\">\n  <div fxLayout=\"row wrap\" class=\"pb-3\">\n    <h5 fxFlex=\"30%\" class=\"primary-color\">Summary</h5>\n    <div fxFlex=\"70%\" fxLayoutAlign=\"center center\">\n      <h5 class=\"secondary-color\">\n        <span class=\"font-weight-bold\">{{data.length}}</span>\n      </h5>\n      <span class=\"text-muted ml-2\"> Records Found In Uploaded File</span>\n    </div>\n  </div>\n\n  <div>\n    <mat-divider></mat-divider>\n  </div>\n\n  <form [formGroup]=\"importMappingForm\" (ngSubmit)=\"next()\" fxLayout=\"column\" class=\"pt-3\">\n    <h5 class=\"primary-color pb-2\">Properties</h5>\n    <table style=\"width: 100%;\" class=\"table table-sm table-striped\">\n      <tr class=\"table-header-row\">\n        <th>Column Headers From File</th>\n        <th>Preview Information</th>\n        <th>Property</th>\n      </tr>\n      <ng-container formArrayName=\"fields\">\n        <tr *ngFor=\"let field of fieldsArray.controls; let i = index\" [formGroupName]=\"i\">\n          <td>{{ field.get('label').value }}</td>\n          <td>{{ field.get('value').value }}</td>\n          <td>\n            <mat-form-field fxFlex=\"100%\" appearance=\"outline\">\n              <mat-label>Select Property</mat-label>\n              <mat-select placeholder=\"Select Property\" formControlName=\"property\" required\n                (selectionChange)=\"updatePropertyType($event, i)\">\n                <mat-option *ngFor=\"let property of properties\" [value]=\"property.value\">\n                  {{property.label}}\n                </mat-option>\n              </mat-select>\n              <mat-error *ngIf=\"isFieldValid('property', fieldsArray.controls[i])\">\n                {{getErrorMessage('property', fieldsArray.controls[i])}}\n              </mat-error>\n            </mat-form-field>\n            <button type=\"button\" mat-icon-button (click)=\"removeField(i)\" *ngIf=\"fieldsArray.length > 1\"\n              matTooltip=\"Remove column\" matTooltipPosition=\"right\">\n              <mat-icon class=\"text-danger\">remove_circle</mat-icon>\n            </button>\n          </td>\n        </tr>\n      </ng-container>\n    </table>\n\n    <div class=\"mt-1 d-flex flex-row gap-2 justify-content-end align-items-center\">\n      <button mat-button class=\"mt-2 mr-2\" type=\"button\" (click)=\"location.back()\">\n        Cancel\n      </button>\n      <button mat-stroked-button class=\"mt-2 mr-2 primary-color\" type=\"button\" (click)=\"reUpload()\">\n        <mat-icon>\n          <span class=\"material-icons-outlined\">\n            file_upload\n          </span>\n        </mat-icon>\n        Re-upload\n      </button>\n      <button mat-stroked-button class=\"mt-2 primary-bg\" type=\"submit\">\n        Next\n        <mat-icon>\n          chevron_right\n        </mat-icon>\n      </button>\n    </div>\n  </form>\n</div>", styles: ["table{width:100%}th,td{padding:1%}.table-header-row{background-color:#e6e4e4;color:#000;font-weight:bolder}\n"] }]
        }], ctorParameters: () => [{ type: i1.UntypedFormBuilder }, { type: i2.FormValidatorService }, { type: i0.ChangeDetectorRef }, { type: i3.Location }], propDecorators: { data: [{
                type: Input
            }], config: [{
                type: Input
            }], onSave: [{
                type: Output
            }], onReUpload: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 