import { Component, Input } from '@angular/core';
import { ControlContainer, FormBuilder, NgForm, Validators } from '@angular/forms';
import { CoreModule } from '@c8y/ngx-components';
import { WidgetConfigComponent } from '@c8y/ngx-components/context-dashboard';
import { DatapointSelectorModule } from '@c8y/ngx-components/datapoint-selector';
import { IconSelectorModule } from '@c8y/ngx-components/icon-selector';
import { PopoverModule } from 'ngx-bootstrap/popover';
import * as i0 from "@angular/core";
import * as i1 from "@angular/forms";
import * as i2 from "@c8y/ngx-components/context-dashboard";
import * as i3 from "@c8y/ngx-components";
import * as i4 from "@c8y/ngx-components/datapoint-selector";
import * as i5 from "@c8y/ngx-components/icon-selector";
import * as i6 from "ngx-bootstrap/popover";
export function exactlyASingleDatapointActive() {
    return (control) => {
        const datapoints = control.value;
        if (!datapoints || !datapoints.length) {
            return null;
        }
        const activeDatapoints = datapoints.filter(datapoint => datapoint.__active);
        if (activeDatapoints.length === 1) {
            return null;
        }
        return { exactlyOneDatapointNeedsToBeActive: true };
    };
}
export class KpiWidgetConfigComponent {
    constructor(formBuilder, form, widgetConfig) {
        this.formBuilder = formBuilder;
        this.form = form;
        this.widgetConfig = widgetConfig;
        this.datapointSelectionConfig = {};
        this.defaultFormOptions = {
            showRedRange: true,
            showYellowRange: true
        };
        this.availableIcons = [];
        this.limits = {
            fontSizeMax: 72,
            fontSizeMin: 18,
            numberOfDecimalPlacesMax: 10,
            numberOfDecimalPlacesMin: 0
        };
    }
    onBeforeSave(config) {
        if (this.formGroup.valid) {
            Object.assign(config, this.formGroup.value);
            return true;
        }
        return false;
    }
    async ngOnInit() {
        if (this.widgetConfig.context?.id) {
            this.datapointSelectionConfig.contextAsset = this.widgetConfig?.context;
        }
        this.initForm();
        if (this.config?.datapoints) {
            this.formGroup.patchValue({ datapoints: this.config.datapoints });
        }
    }
    initForm() {
        this.formGroup = this.createForm();
        this.form.form.addControl('config', this.formGroup);
        this.formGroup.patchValue(this.config);
    }
    createForm() {
        return this.formBuilder.group({
            numberOfDecimalPlaces: [
                2,
                [
                    Validators.required,
                    Validators.min(this.limits.numberOfDecimalPlacesMin),
                    Validators.max(this.limits.numberOfDecimalPlacesMax)
                ]
            ],
            showTimestamp: [true, []],
            showTrend: [true, []],
            showIcon: [true, []],
            icon: ['water', [Validators.required, Validators.minLength(1)]],
            fontSize: [
                36,
                [
                    Validators.required,
                    Validators.min(this.limits.fontSizeMin),
                    Validators.max(this.limits.fontSizeMax)
                ]
            ],
            datapoints: this.formBuilder.control(new Array(), [
                Validators.required,
                Validators.minLength(1),
                exactlyASingleDatapointActive()
            ])
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: KpiWidgetConfigComponent, deps: [{ token: i1.FormBuilder }, { token: i1.NgForm }, { token: i2.WidgetConfigComponent }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.13", type: KpiWidgetConfigComponent, isStandalone: true, selector: "c8y-kpi-widget-config", inputs: { config: "config" }, ngImport: i0, template: "<div class=\"p-l-24 p-r-24\">\n  <form [formGroup]=\"formGroup\" class=\"row no-card-context\">\n    <div class=\"col-sm-6 bg-level-0\">\n      <c8y-datapoint-selection-list\n        [defaultFormOptions]=\"defaultFormOptions\"\n        [config]=\"datapointSelectionConfig\"\n        [minActiveCount]=\"1\"\n        [maxActiveCount]=\"1\"\n        formControlName=\"datapoints\"\n        name=\"datapoints\"\n        class=\"bg-inherit\"\n      ></c8y-datapoint-selection-list>\n    </div>\n    <div class=\"col-sm-6\">\n      <div class=\"card-header separator\">\n        <div class=\"card-title h4\">{{ 'Layout' | translate }}</div>\n      </div>\n      <label translate>Icon</label>\n      <div class=\"d-flex a-i-center\">\n        <c8y-icon-selector-wrapper name=\"icon\" formControlName=\"icon\"></c8y-icon-selector-wrapper>\n      </div>\n      <c8y-form-group>\n        <label [title]=\"'Number of decimal places' | translate\" translate>\n          Number of decimal places\n        </label>\n        <input\n          class=\"form-control\"\n          formControlName=\"numberOfDecimalPlaces\"\n          name=\"numberOfDecimalPlaces\"\n          type=\"number\"\n          [placeholder]=\"'e.g. {{ example }}' | translate: { example: 1 }\"\n        />\n        <c8y-messages\n          [show]=\"\n            formGroup.controls?.numberOfDecimalPlaces?.touched &&\n            formGroup?.controls?.numberOfDecimalPlaces?.errors\n          \"\n        ></c8y-messages>\n      </c8y-form-group>\n      <div>\n        <label>{{ 'Display' | translate }}</label>\n        <div class=\"d-flex gap-16 flex-wrap\">\n          <c8y-form-group>\n            <label [title]=\"'Show timestamp' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showTimestamp\" name=\"showTimestamp\" />\n              <span></span>\n              <span translate>Show timestamp</span>\n            </label>\n          </c8y-form-group>\n\n          <c8y-form-group>\n            <label [title]=\"'Show icon' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showIcon\" name=\"showIcon\" />\n              <span></span>\n              <span translate>Show icon</span>\n            </label>\n          </c8y-form-group>\n\n          <c8y-form-group>\n            <label [title]=\"'Show trend icon' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showTrend\" name=\"showTrend\" />\n              <span></span>\n              <span translate>Show trend icon</span>\n              <button\n                class=\"btn-help btn-help--sm\"\n                type=\"button\"\n                [attr.aria-label]=\"'Help' | translate\"\n                popover=\"{{\n                  'Indicates the trend between the last two measurement values.' | translate\n                }}\"\n                placement=\"right\"\n                triggers=\"focus\"\n                container=\"body\"\n              ></button>\n            </label>\n          </c8y-form-group>\n        </div>\n\n        <c8y-form-group>\n          <label [title]=\"'Font size of measurement value (px)' | translate\" translate>\n            Font size of measurement value (px)\n          </label>\n          <input\n            class=\"form-control\"\n            formControlName=\"fontSize\"\n            name=\"fontSize\"\n            type=\"number\"\n            [placeholder]=\"'e.g. {{ example }}' | translate: { example: 36 }\"\n          />\n          <c8y-messages\n            [show]=\"formGroup.controls?.fontSize?.touched && formGroup?.controls?.fontSize?.errors\"\n          ></c8y-messages>\n        </c8y-form-group>\n      </div>\n    </div>\n  </form>\n</div>\n", dependencies: [{ kind: "ngmodule", type: CoreModule }, { kind: "pipe", type: i3.C8yTranslatePipe, name: "translate" }, { kind: "directive", type: i3.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { 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.NumberValueAccessor, selector: "input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]" }, { 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: "component", type: i3.FormGroupComponent, selector: "c8y-form-group", inputs: ["hasError", "hasWarning", "hasSuccess", "novalidation", "status"] }, { kind: "component", type: i3.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage"] }, { kind: "directive", type: i3.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: "ngmodule", type: DatapointSelectorModule }, { kind: "component", type: i4.DatapointSelectionListComponent, selector: "c8y-datapoint-selection-list", inputs: ["actions", "allowDragAndDrop", "config", "defaultFormOptions", "maxActiveCount", "minActiveCount", "resolveContext", "listTitle"], outputs: ["isValid", "change"] }, { kind: "ngmodule", type: IconSelectorModule }, { kind: "component", type: i5.IconSelectorWrapperComponent, selector: "c8y-icon-selector-wrapper", inputs: ["canRemoveIcon", "selectedIcon", "iconSize"], outputs: ["onSelect"] }, { kind: "ngmodule", type: PopoverModule }, { kind: "directive", type: i6.PopoverDirective, selector: "[popover]", inputs: ["adaptivePosition", "boundariesElement", "popover", "popoverContext", "popoverTitle", "placement", "outsideClick", "triggers", "container", "containerClass", "isOpen", "delay"], outputs: ["onShown", "onHidden"], exportAs: ["bs-popover"] }], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImport: i0, type: KpiWidgetConfigComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-kpi-widget-config', standalone: true, imports: [CoreModule, DatapointSelectorModule, IconSelectorModule, PopoverModule], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }], template: "<div class=\"p-l-24 p-r-24\">\n  <form [formGroup]=\"formGroup\" class=\"row no-card-context\">\n    <div class=\"col-sm-6 bg-level-0\">\n      <c8y-datapoint-selection-list\n        [defaultFormOptions]=\"defaultFormOptions\"\n        [config]=\"datapointSelectionConfig\"\n        [minActiveCount]=\"1\"\n        [maxActiveCount]=\"1\"\n        formControlName=\"datapoints\"\n        name=\"datapoints\"\n        class=\"bg-inherit\"\n      ></c8y-datapoint-selection-list>\n    </div>\n    <div class=\"col-sm-6\">\n      <div class=\"card-header separator\">\n        <div class=\"card-title h4\">{{ 'Layout' | translate }}</div>\n      </div>\n      <label translate>Icon</label>\n      <div class=\"d-flex a-i-center\">\n        <c8y-icon-selector-wrapper name=\"icon\" formControlName=\"icon\"></c8y-icon-selector-wrapper>\n      </div>\n      <c8y-form-group>\n        <label [title]=\"'Number of decimal places' | translate\" translate>\n          Number of decimal places\n        </label>\n        <input\n          class=\"form-control\"\n          formControlName=\"numberOfDecimalPlaces\"\n          name=\"numberOfDecimalPlaces\"\n          type=\"number\"\n          [placeholder]=\"'e.g. {{ example }}' | translate: { example: 1 }\"\n        />\n        <c8y-messages\n          [show]=\"\n            formGroup.controls?.numberOfDecimalPlaces?.touched &&\n            formGroup?.controls?.numberOfDecimalPlaces?.errors\n          \"\n        ></c8y-messages>\n      </c8y-form-group>\n      <div>\n        <label>{{ 'Display' | translate }}</label>\n        <div class=\"d-flex gap-16 flex-wrap\">\n          <c8y-form-group>\n            <label [title]=\"'Show timestamp' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showTimestamp\" name=\"showTimestamp\" />\n              <span></span>\n              <span translate>Show timestamp</span>\n            </label>\n          </c8y-form-group>\n\n          <c8y-form-group>\n            <label [title]=\"'Show icon' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showIcon\" name=\"showIcon\" />\n              <span></span>\n              <span translate>Show icon</span>\n            </label>\n          </c8y-form-group>\n\n          <c8y-form-group>\n            <label [title]=\"'Show trend icon' | translate\" class=\"c8y-checkbox\">\n              <input type=\"checkbox\" formControlName=\"showTrend\" name=\"showTrend\" />\n              <span></span>\n              <span translate>Show trend icon</span>\n              <button\n                class=\"btn-help btn-help--sm\"\n                type=\"button\"\n                [attr.aria-label]=\"'Help' | translate\"\n                popover=\"{{\n                  'Indicates the trend between the last two measurement values.' | translate\n                }}\"\n                placement=\"right\"\n                triggers=\"focus\"\n                container=\"body\"\n              ></button>\n            </label>\n          </c8y-form-group>\n        </div>\n\n        <c8y-form-group>\n          <label [title]=\"'Font size of measurement value (px)' | translate\" translate>\n            Font size of measurement value (px)\n          </label>\n          <input\n            class=\"form-control\"\n            formControlName=\"fontSize\"\n            name=\"fontSize\"\n            type=\"number\"\n            [placeholder]=\"'e.g. {{ example }}' | translate: { example: 36 }\"\n          />\n          <c8y-messages\n            [show]=\"formGroup.controls?.fontSize?.touched && formGroup?.controls?.fontSize?.errors\"\n          ></c8y-messages>\n        </c8y-form-group>\n      </div>\n    </div>\n  </form>\n</div>\n" }]
        }], ctorParameters: () => [{ type: i1.FormBuilder }, { type: i1.NgForm }, { type: i2.WidgetConfigComponent }], propDecorators: { config: [{
                type: Input
            }] } });
//# sourceMappingURL=data:application/json;base64, 