import { Component, EventEmitter, Input, Output } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { map, startWith, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "@c8y/ngx-components";
import * as i3 from "@angular/common";
import * as i4 from "../alarm-event-attributes-form/alarm-event-attributes-form.component";
export class CustomAlarmEventFormComponent {
    constructor(formBuilder) {
        this.formBuilder = formBuilder;
        this.omitProperties = {};
        this.selectedItems = [];
        this.added = new EventEmitter();
        this.cancel = new EventEmitter();
        this.destroy$ = new Subject();
        this.formGroup = this.formBuilder.group({
            details: [],
            color: [],
            __active: [],
            __target: []
        });
        this.valid$ = this.formGroup.statusChanges.pipe(takeUntil(this.destroy$), map(val => val === 'VALID'), startWith(false));
    }
    ngOnInit() {
        this.formGroup.patchValue({
            color: this.defaultColor,
            __target: this.target,
            details: { timelineType: this.timelineType }
        });
    }
    ngOnDestroy() {
        this.destroy$.next();
        this.destroy$.complete();
    }
    add() {
        if (this.formGroup.valid) {
            const formValue = this.transformFormValue(this.formGroup.value);
            this.added.emit(formValue);
        }
    }
    transformFormValue(formValue) {
        const obj = Object.assign({}, formValue.details || {}, formValue);
        delete obj.details;
        return obj;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: CustomAlarmEventFormComponent, deps: [{ token: i1.FormBuilder }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: CustomAlarmEventFormComponent, selector: "c8y-custom-alarm-event-form", inputs: { timelineType: "timelineType", target: "target", omitProperties: "omitProperties", selectedItems: "selectedItems", defaultColor: "defaultColor" }, outputs: { added: "added", cancel: "cancel" }, ngImport: i0, template: "<form [formGroup]=\"formGroup\" class=\"p-16\">\n  <div\n    *ngIf=\"!omitProperties.color\"\n    class=\"form-group d-flex a-i-center gap-8\"\n    [title]=\"'Change color' | translate\"\n  >\n    <label class=\"m-0\">{{ 'Color' | translate }}</label>\n    <div class=\"c8y-colorpicker\"\n      [ngClass]=\"{\n        'c8y-colorpicker--event': timelineType === 'EVENT',\n        'c8y-colorpicker--alarm': timelineType !== 'EVENT'\n      }\"\n    >\n      <input\n        type=\"color\"\n        formControlName=\"color\"\n        (click)=\"$event.stopPropagation()\"\n      />\n      <span [style.background-color]=\"formGroup.value.color\">\n        <i [c8yIcon]=\"timelineType === 'EVENT' ? 'c8y-events' : 'bell'\"></i>\n      </span>\n    </div>\n  </div>\n\n  <c8y-alarm-event-attributes-form\n    formControlName=\"details\"\n    [timelineType]=\"timelineType\"\n    [omitProperties]=\"omitProperties\"\n    [selectedItems]=\"selectedItems\"\n  ></c8y-alarm-event-attributes-form>\n\n  <div class=\"d-flex p-t-16\">\n    <button class=\"btn btn-default btn-sm\" (click)=\"cancel.emit()\">\n      {{ 'Cancel' | translate }}\n    </button>\n    <button\n      class=\"btn btn-primary btn-sm\"\n      [disabled]=\"(valid$ | async) === false\"\n      (click)=\"add()\"\n    >\n      <i c8yIcon=\"plus-circle\"></i>\n      {{ 'Select' | translate }}\n    </button>\n  </div>\n</form>\n", dependencies: [{ kind: "directive", type: i2.IconDirective, selector: "[c8yIcon]", inputs: ["c8yIcon"] }, { kind: "directive", type: i3.NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { 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.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.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i1.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i2.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { 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: "component", type: i4.AlarmEventAttributesFormComponent, selector: "c8y-alarm-event-attributes-form", inputs: ["timelineType", "omitProperties", "selectedItems", "target", "datapoints"] }, { kind: "pipe", type: i2.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: CustomAlarmEventFormComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-custom-alarm-event-form', template: "<form [formGroup]=\"formGroup\" class=\"p-16\">\n  <div\n    *ngIf=\"!omitProperties.color\"\n    class=\"form-group d-flex a-i-center gap-8\"\n    [title]=\"'Change color' | translate\"\n  >\n    <label class=\"m-0\">{{ 'Color' | translate }}</label>\n    <div class=\"c8y-colorpicker\"\n      [ngClass]=\"{\n        'c8y-colorpicker--event': timelineType === 'EVENT',\n        'c8y-colorpicker--alarm': timelineType !== 'EVENT'\n      }\"\n    >\n      <input\n        type=\"color\"\n        formControlName=\"color\"\n        (click)=\"$event.stopPropagation()\"\n      />\n      <span [style.background-color]=\"formGroup.value.color\">\n        <i [c8yIcon]=\"timelineType === 'EVENT' ? 'c8y-events' : 'bell'\"></i>\n      </span>\n    </div>\n  </div>\n\n  <c8y-alarm-event-attributes-form\n    formControlName=\"details\"\n    [timelineType]=\"timelineType\"\n    [omitProperties]=\"omitProperties\"\n    [selectedItems]=\"selectedItems\"\n  ></c8y-alarm-event-attributes-form>\n\n  <div class=\"d-flex p-t-16\">\n    <button class=\"btn btn-default btn-sm\" (click)=\"cancel.emit()\">\n      {{ 'Cancel' | translate }}\n    </button>\n    <button\n      class=\"btn btn-primary btn-sm\"\n      [disabled]=\"(valid$ | async) === false\"\n      (click)=\"add()\"\n    >\n      <i c8yIcon=\"plus-circle\"></i>\n      {{ 'Select' | translate }}\n    </button>\n  </div>\n</form>\n" }]
        }], ctorParameters: () => [{ type: i1.FormBuilder }], propDecorators: { timelineType: [{
                type: Input
            }], target: [{
                type: Input
            }], omitProperties: [{
                type: Input
            }], selectedItems: [{
                type: Input
            }], defaultColor: [{
                type: Input
            }], added: [{
                type: Output
            }], cancel: [{
                type: Output
            }] } });
//# sourceMappingURL=data:application/json;base64, 