import { Component, EventEmitter, Output, ViewChild } from '@angular/core';
import { FormBuilder, NgForm, Validators } from '@angular/forms';
import { camelCase } from 'lodash-es';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { simpleJsonPathValidator } from '../../forms/simple-json-path-validator.directive';
import { gettext } from '../../i18n/gettext';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "ngx-bootstrap/modal";
import * as i3 from "../../i18n/c8y-translate.directive";
import * as i4 from "../../forms/form-group.component";
import * as i5 from "../../forms/message.directive";
import * as i6 from "../../forms/messages.component";
import * as i7 from "../../forms/required-input-placeholder.directive";
import * as i8 from "../../forms/unique-in-collection-by-path-validation.directive";
import * as i9 from "../../i18n/c8y-translate.pipe";
export class ConfigureCustomColumnComponent {
    constructor(fb, modalRef) {
        this.fb = fb;
        this.modalRef = modalRef;
        this.SIMPLE_JSON_PATH_REGEX = /^[^.\s]+(\.\S+)*$/;
        this.onAddCustomColumn = new EventEmitter();
        this.formGroup = this.fb.group({
            header: [''],
            path: ['', [Validators.required, simpleJsonPathValidator]],
            addAnother: [false]
        });
        this.columns = [];
        this.uniqueHeaderMsg = gettext('The column header name is too similar to "{{ value }}".');
    }
    comparator(val1, val2) {
        return camelCase(val1) === camelCase(val2);
    }
    addColumn() {
        const { header, path } = this.formGroup.value;
        const column = {
            name: camelCase(header),
            header,
            path,
            custom: true,
            visible: true
        };
        this.onAddCustomColumn.emit(column);
        // add new column to columns array since it's used for header uniqueness validation
        this.columns.splice(this.columns.length, 0, column);
        if (this.formGroup.value.addAnother) {
            this.form.resetForm({ addAnother: true });
        }
        else {
            this.modalRef.hide();
        }
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ConfigureCustomColumnComponent, deps: [{ token: i1.FormBuilder }, { token: i2.BsModalRef }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: ConfigureCustomColumnComponent, selector: "c8y-configure-custom-column", outputs: { onAddCustomColumn: "onAddCustomColumn" }, viewQueries: [{ propertyName: "form", first: true, predicate: ["columnConfigForm"], descendants: true }], ngImport: i0, template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header separator-bottom\">\n    <h3 id=\"modal-title\" translate>Configure custom column</h3>\n  </div>\n\n  <form class=\"d-contents\" #columnConfigForm=\"ngForm\" [formGroup]=\"formGroup\">\n    <div class=\"modal-inner-scroll\" id=\"modal-body\">\n      <div class=\"modal-body\">\n        <c8y-form-group>\n          <label for=\"header\" translate>Header</label>\n          <input\n            id=\"header\"\n            data-cy=\"configure-custom-column--header\"\n            formControlName=\"header\"\n            class=\"form-control\"\n            autocomplete=\"off\"\n            [placeholder]=\"'e.g. Agent name' | translate\"\n            [required]=\"true\"\n            uniqueByPath\n            [collection]=\"columns\"\n            path=\"header\"\n            [comparator]=\"comparator\"\n          />\n          <c8y-messages>\n            <c8y-message\n              name=\"uniqueByPath\"\n              [text]=\"uniqueHeaderMsg\"\n            ></c8y-message>\n          </c8y-messages>\n        </c8y-form-group>\n        <c8y-form-group>\n          <label for=\"fragmentPath\" translate>Fragment path</label>\n          <input\n            id=\"fragmentPath\"\n            data-cy=\"configure-custom-column--fragmentPath\"\n            formControlName=\"path\"\n            class=\"form-control\"\n            autocomplete=\"off\"\n            [placeholder]=\"'e.g. {{ example }}' | translate: { example: 'c8y_Agent.name' }\"\n          />\n        </c8y-form-group>\n        <label class=\"c8y-switch\" [title]=\"'Add another column after saving this one' | translate\">\n          <input type=\"checkbox\" formControlName=\"addAnother\" class=\"form-control\" />\n          <span></span>\n          <span translate>Add another column after saving this one</span>\n        </label>\n      </div>\n    </div>\n\n    <div class=\"modal-footer\">\n      <button title=\"{{ 'Cancel' | translate }}\" class=\"btn btn-default\" (click)=\"modalRef.hide()\">\n        {{ 'Cancel' | translate }}\n      </button>\n      <button\n        title=\"{{ 'Save' | translate }}\"\n        class=\"btn btn-primary\"\n        [disabled]=\"columnConfigForm.form.pristine || !columnConfigForm.form.valid\"\n        (click)=\"addColumn()\"\n      >\n        {{ 'Save' | translate }}\n      </button>\n    </div>\n  </form>\n</div>\n", dependencies: [{ kind: "directive", type: i1.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i1.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: i1.CheckboxControlValueAccessor, selector: "input[type=checkbox][formControlName],input[type=checkbox][formControl],input[type=checkbox][ngModel]" }, { 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: i3.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "component", type: i4.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "directive", type: i5.MessageDirective, selector: "c8y-message", inputs: ["name", "text"] }, { kind: "component", type: i6.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage"] }, { kind: "directive", type: i7.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { kind: "directive", type: i8.UniqueInCollectionByPathValidationDirective, selector: "[uniqueByPath]", inputs: ["collection", "path", "comparator"] }, { kind: "pipe", type: i9.C8yTranslatePipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: ConfigureCustomColumnComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-configure-custom-column', template: "<div class=\"viewport-modal\">\n  <div class=\"modal-header separator-bottom\">\n    <h3 id=\"modal-title\" translate>Configure custom column</h3>\n  </div>\n\n  <form class=\"d-contents\" #columnConfigForm=\"ngForm\" [formGroup]=\"formGroup\">\n    <div class=\"modal-inner-scroll\" id=\"modal-body\">\n      <div class=\"modal-body\">\n        <c8y-form-group>\n          <label for=\"header\" translate>Header</label>\n          <input\n            id=\"header\"\n            data-cy=\"configure-custom-column--header\"\n            formControlName=\"header\"\n            class=\"form-control\"\n            autocomplete=\"off\"\n            [placeholder]=\"'e.g. Agent name' | translate\"\n            [required]=\"true\"\n            uniqueByPath\n            [collection]=\"columns\"\n            path=\"header\"\n            [comparator]=\"comparator\"\n          />\n          <c8y-messages>\n            <c8y-message\n              name=\"uniqueByPath\"\n              [text]=\"uniqueHeaderMsg\"\n            ></c8y-message>\n          </c8y-messages>\n        </c8y-form-group>\n        <c8y-form-group>\n          <label for=\"fragmentPath\" translate>Fragment path</label>\n          <input\n            id=\"fragmentPath\"\n            data-cy=\"configure-custom-column--fragmentPath\"\n            formControlName=\"path\"\n            class=\"form-control\"\n            autocomplete=\"off\"\n            [placeholder]=\"'e.g. {{ example }}' | translate: { example: 'c8y_Agent.name' }\"\n          />\n        </c8y-form-group>\n        <label class=\"c8y-switch\" [title]=\"'Add another column after saving this one' | translate\">\n          <input type=\"checkbox\" formControlName=\"addAnother\" class=\"form-control\" />\n          <span></span>\n          <span translate>Add another column after saving this one</span>\n        </label>\n      </div>\n    </div>\n\n    <div class=\"modal-footer\">\n      <button title=\"{{ 'Cancel' | translate }}\" class=\"btn btn-default\" (click)=\"modalRef.hide()\">\n        {{ 'Cancel' | translate }}\n      </button>\n      <button\n        title=\"{{ 'Save' | translate }}\"\n        class=\"btn btn-primary\"\n        [disabled]=\"columnConfigForm.form.pristine || !columnConfigForm.form.valid\"\n        (click)=\"addColumn()\"\n      >\n        {{ 'Save' | translate }}\n      </button>\n    </div>\n  </form>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.FormBuilder }, { type: i2.BsModalRef }], propDecorators: { form: [{
                type: ViewChild,
                args: ['columnConfigForm', { static: false }]
            }], onAddCustomColumn: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 