import * as i0 from '@angular/core';
import { inject, Input, Component } from '@angular/core';
import * as i2 from '@angular/forms';
import { FormBuilder, NgForm, Validators, ControlContainer, ReactiveFormsModule } from '@angular/forms';
import * as i1 from '@c8y/ngx-components';
import { CoreModule, CommonModule, C8yTranslatePipe } from '@c8y/ngx-components';
import * as i3 from '@c8y/ngx-components/datapoint-selector';
import { DatapointSelectorModule } from '@c8y/ngx-components/datapoint-selector';
import { R as RESULT_TYPES, D as DEFAULT_DECIMAL_PLACES } from './c8y-ngx-components-computed-asset-properties-c8y-ngx-components-computed-asset-properties-wNQ45CJ1.mjs';
import * as i2$1 from '@angular/common';

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 };
    };
}
class ComputedPropertyLastMeasurementConfigComponent {
    constructor() {
        this.datapointSelectionConfig = {};
        this.formBuilder = inject(FormBuilder);
        this.form = inject(NgForm);
        this.selectedProperty = [];
        this.minSelectCount = 1;
        this.maxSelectCount = 1;
        this.resultTypes = RESULT_TYPES;
    }
    ngOnInit() {
        if (this.asset) {
            this.datapointSelectionConfig.contextAsset = this.asset;
            this.datapointSelectionConfig.allowChangingContext = false;
        }
        this.initForm();
    }
    onBeforeSave(config) {
        if (this.formGroup.valid) {
            Object.assign(config, this.formGroup.value);
            config.dp = config.dp.filter(d => d.__active);
            config.dp[0].__target = { id: config.dp[0].__target.id, name: config.dp[0].__target.name };
            return true;
        }
        return false;
    }
    initForm() {
        this.formGroup = this.createForm();
        this.form.form.addControl('config', this.formGroup);
        this.formGroup.patchValue(this.config);
    }
    createForm() {
        return this.formBuilder.group({
            resultType: [this.resultTypes.VALUE.value, [Validators.required]],
            numberOfDecimalPlaces: [DEFAULT_DECIMAL_PLACES, [Validators.min(0), Validators.max(10)]],
            dp: this.formBuilder.control(new Array(), [
                Validators.required,
                Validators.minLength(1),
                exactlyASingleDatapointActive()
            ])
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ComputedPropertyLastMeasurementConfigComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.19", type: ComputedPropertyLastMeasurementConfigComponent, isStandalone: true, selector: "c8y-last-measurement-config", inputs: { config: "config", asset: "asset" }, ngImport: i0, template: "<form [formGroup]=\"formGroup\">\n  <div class=\"d-flex-md row\">\n    <div class=\"col-md-6\">\n      <div\n        class=\"form-group\"\n        style=\"padding-top: var(--c8y-font-size-base)\"\n      >\n        <label for=\"resultType\">{{ 'Result type' | translate }}</label>\n        <div class=\"c8y-select-wrapper\">\n          <select\n            class=\"form-control\"\n            id=\"resultType\"\n            formControlName=\"resultType\"\n          >\n            @for (type of resultTypes | keyvalue; track type.key) {\n              <option [ngValue]=\"type.value.value\">\n                {{ type.value.label | translate }}\n              </option>\n            }\n          </select>\n        </div>\n      </div>\n      <div class=\"form-group form-group-sm\">\n        <label\n          [title]=\"'Number of decimal places' | translate\"\n          for=\"numberOfDecimalPlaces\"\n          translate\n        >\n          Number of decimal places\n        </label>\n        <input\n          class=\"form-control\"\n          id=\"numberOfDecimalPlaces\"\n          name=\"numberOfDecimalPlaces\"\n          type=\"number\"\n          formControlName=\"numberOfDecimalPlaces\"\n          [placeholder]=\"'e.g. {{ example }}' | translate: { example: 2 }\"\n        />\n        <c8y-messages\n          [show]=\"\n            formGroup.controls?.numberOfDecimalPlaces?.touched &&\n            formGroup?.controls?.numberOfDecimalPlaces?.errors\n          \"\n        ></c8y-messages>\n      </div>\n    </div>\n    <div class=\"col-md-6 no-card-context\">\n      <c8y-datapoint-selection-list\n        class=\"bg-inherit\"\n        name=\"dp\"\n        [config]=\"datapointSelectionConfig\"\n        [minActiveCount]=\"minSelectCount\"\n        [maxActiveCount]=\"maxSelectCount\"\n        formControlName=\"dp\"\n      ></c8y-datapoint-selection-list>\n    </div>\n  </div>\n</form>\n", dependencies: [{ kind: "ngmodule", type: CoreModule }, { kind: "directive", type: i1.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "directive", type: i2.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i2.NgSelectOption, selector: "option", inputs: ["ngValue", "value"] }, { kind: "directive", type: i2.ɵNgSelectMultipleOption, selector: "option", inputs: ["ngValue", "value"] }, { 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.NumberValueAccessor, selector: "input[type=number][formControlName],input[type=number][formControl],input[type=number][ngModel]" }, { kind: "directive", type: i2.SelectControlValueAccessor, selector: "select:not([multiple])[formControlName],select:not([multiple])[formControl],select:not([multiple])[ngModel]", inputs: ["compareWith"] }, { kind: "directive", type: i2.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i2.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "component", type: i1.MessagesComponent, selector: "c8y-messages", inputs: ["show", "defaults", "helpMessage", "additionalMessages"] }, { kind: "directive", type: i1.RequiredInputPlaceholderDirective, selector: "input[required], input[formControlName]" }, { 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"] }, { kind: "ngmodule", type: DatapointSelectorModule }, { kind: "component", type: i3.DatapointSelectionListComponent, selector: "c8y-datapoint-selection-list", inputs: ["actions", "allowDragAndDrop", "config", "defaultFormOptions", "maxActiveCount", "minActiveCount", "resolveContext", "listTitle", "removeTitle"], outputs: ["isValid", "change"] }, { kind: "ngmodule", type: CommonModule }, { kind: "ngmodule", type: ReactiveFormsModule }, { kind: "pipe", type: i1.C8yTranslatePipe, name: "translate" }, { kind: "pipe", type: i2$1.KeyValuePipe, name: "keyvalue" }], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.19", ngImport: i0, type: ComputedPropertyLastMeasurementConfigComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-last-measurement-config', imports: [
                        CoreModule,
                        DatapointSelectorModule,
                        CommonModule,
                        C8yTranslatePipe,
                        ReactiveFormsModule
                    ], viewProviders: [{ provide: ControlContainer, useExisting: NgForm }], template: "<form [formGroup]=\"formGroup\">\n  <div class=\"d-flex-md row\">\n    <div class=\"col-md-6\">\n      <div\n        class=\"form-group\"\n        style=\"padding-top: var(--c8y-font-size-base)\"\n      >\n        <label for=\"resultType\">{{ 'Result type' | translate }}</label>\n        <div class=\"c8y-select-wrapper\">\n          <select\n            class=\"form-control\"\n            id=\"resultType\"\n            formControlName=\"resultType\"\n          >\n            @for (type of resultTypes | keyvalue; track type.key) {\n              <option [ngValue]=\"type.value.value\">\n                {{ type.value.label | translate }}\n              </option>\n            }\n          </select>\n        </div>\n      </div>\n      <div class=\"form-group form-group-sm\">\n        <label\n          [title]=\"'Number of decimal places' | translate\"\n          for=\"numberOfDecimalPlaces\"\n          translate\n        >\n          Number of decimal places\n        </label>\n        <input\n          class=\"form-control\"\n          id=\"numberOfDecimalPlaces\"\n          name=\"numberOfDecimalPlaces\"\n          type=\"number\"\n          formControlName=\"numberOfDecimalPlaces\"\n          [placeholder]=\"'e.g. {{ example }}' | translate: { example: 2 }\"\n        />\n        <c8y-messages\n          [show]=\"\n            formGroup.controls?.numberOfDecimalPlaces?.touched &&\n            formGroup?.controls?.numberOfDecimalPlaces?.errors\n          \"\n        ></c8y-messages>\n      </div>\n    </div>\n    <div class=\"col-md-6 no-card-context\">\n      <c8y-datapoint-selection-list\n        class=\"bg-inherit\"\n        name=\"dp\"\n        [config]=\"datapointSelectionConfig\"\n        [minActiveCount]=\"minSelectCount\"\n        [maxActiveCount]=\"maxSelectCount\"\n        formControlName=\"dp\"\n      ></c8y-datapoint-selection-list>\n    </div>\n  </div>\n</form>\n" }]
        }], propDecorators: { config: [{
                type: Input
            }], asset: [{
                type: Input
            }] } });

export { ComputedPropertyLastMeasurementConfigComponent, exactlyASingleDatapointActive };
//# sourceMappingURL=c8y-ngx-components-computed-asset-properties-last-measurement-config.component-BGhex-OP.mjs.map
