import { Component } from '@angular/core';
import { FormGroup, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { TenantService } from '@c8y/client';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { AlertService, CoreModule, FormsModule, gettext } from '@c8y/ngx-components';
import { CustomPropertiesService } from '../custom-properties/custom-properties.service';
import { CustomPropertyFieldComponent } from '../custom-properties/custom-property-field/custom-property-field.component';
import * as i0 from "@angular/core";
import * as i1 from "@c8y/client";
import * as i2 from "@c8y/ngx-components";
import * as i3 from "@angular/router";
import * as i4 from "../custom-properties/custom-properties.service";
import * as i5 from "@angular/common";
import * as i6 from "@angular/forms";
export class CustomPropertiesComponent {
    constructor(tenantService, alertService, activatedRoute, customPropertiesService) {
        this.tenantService = tenantService;
        this.alertService = alertService;
        this.activatedRoute = activatedRoute;
        this.customPropertiesService = customPropertiesService;
        this.customPropsForm = new FormGroup({});
        this.tenant = null;
        this.initialized = false;
    }
    async ngOnInit() {
        await this.loadTenantDetails();
        const { form, fields } = await this.customPropertiesService.getFormAndFieldList();
        this.customPropsForm = form;
        this.fieldDefinitions = fields;
        this.applyValuesFromTenant();
        this.initialized = true;
    }
    async onSubmit() {
        if (this.customPropsForm.invalid || !this.tenant) {
            return;
        }
        const updatedTenant = {
            ...this.tenant,
            customProperties: {
                ...this.tenant.customProperties,
                ...this.getDirtyValues()
            }
        };
        try {
            await this.tenantService.update(updatedTenant);
            this.alertService.success(gettext('Custom properties values saved.'));
        }
        catch (error) {
            this.alertService.addServerFailure(error);
        }
    }
    async loadTenantDetails() {
        try {
            const result = await this.tenantService.detail(this.activatedRoute.snapshot.parent.data.contextData.id);
            this.tenant = result.data;
        }
        catch (error) {
            this.alertService.addServerFailure(error);
        }
    }
    applyValuesFromTenant() {
        const customProps = this.tenant?.customProperties || {};
        this.customPropsForm.patchValue(customProps);
    }
    getDirtyValues() {
        const dirtyValues = {};
        Object.keys(this.customPropsForm.controls).forEach(key => {
            const control = this.customPropsForm.controls[key];
            if (control && control.dirty) {
                dirtyValues[key] = control.value;
            }
        });
        return dirtyValues;
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: CustomPropertiesComponent, deps: [{ token: i1.TenantService }, { token: i2.AlertService }, { token: i3.ActivatedRoute }, { token: i4.CustomPropertiesService }], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "18.2.9", type: CustomPropertiesComponent, isStandalone: true, selector: "c8y-custom-properties", ngImport: i0, template: "<c8y-title *ngIf=\"tenant\">\n  {{ tenant.company }}\n</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-layers'\"\n    [label]=\"'Tenants' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-layers'\"\n    [label]=\"'Subtenants' | translate\"\n    [path]=\"'/tenants'\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<ng-container>\n  <form\n    [formGroup]=\"customPropsForm\"\n    (ngSubmit)=\"onSubmit()\"\n  >\n    <div class=\"card card--fullpage m-b-0\">\n      <div class=\"card-header separator\">\n        <div\n          class=\"card-title\"\n          translate\n        >\n          Custom properties\n        </div>\n      </div>\n\n      <div class=\"inner-scroll\">\n        <div\n          class=\"card-block\"\n          *ngIf=\"!initialized\"\n        >\n          <c8y-loading></c8y-loading>\n        </div>\n\n        <c8y-help src=\"/docs/enterprise-tenant/managing-tenants/#custom-properties\"></c8y-help>\n\n        <!-- empty state -->\n        <c8y-ui-empty-state\n          [icon]=\"'property-script'\"\n          [title]=\"'No custom properties to display.' | translate\"\n          [subtitle]=\"'Add a new tenant property in Properties library.' | translate\"\n          *ngIf=\"fieldDefinitions?.length === 0 && initialized\"\n        >\n          <p c8y-guide-docs>\n            <small translate>\n              Find out more in the\n              <a c8y-guide-href=\"/docs/enterprise-tenant/managing-tenants/#custom-properties\">\n                User guide\n              </a>\n              .\n            </small>\n          </p>\n        </c8y-ui-empty-state>\n\n        <div\n          class=\"card-block\"\n          *ngIf=\"initialized\"\n        >\n          <ng-container *ngFor=\"let field of fieldDefinitions\">\n            <c8y-custom-property-field\n              [fieldDefinition]=\"field\"\n              [form]=\"customPropsForm\"\n            ></c8y-custom-property-field>\n          </ng-container>\n        </div>\n      </div>\n\n      <div\n        class=\"card-footer separator\"\n        *ngIf=\"initialized\"\n      >\n        <button\n          class=\"btn btn-default\"\n          type=\"button\"\n          [routerLink]=\"['/tenants']\"\n          translate\n          data-cy=\"custom-properties--cancel-button\"\n        >\n          Cancel\n        </button>\n        <button\n          class=\"btn btn-primary\"\n          type=\"submit\"\n          [disabled]=\"!(!customPropsForm.invalid && customPropsForm.dirty)\"\n          translate\n          data-cy=\"custom-properties--save-button\"\n        >\n          Save\n        </button>\n      </div>\n    </div>\n  </form>\n</ng-container>\n", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i5.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i5.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "ngmodule", type: ReactiveFormsModule }, { kind: "directive", type: i6.ɵNgNoValidate, selector: "form:not([ngNoForm]):not([ngNativeValidate])" }, { kind: "directive", type: i6.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i6.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "ngmodule", type: FormsModule }, { kind: "ngmodule", type: CoreModule }, { kind: "component", type: i2.BreadcrumbComponent, selector: "c8y-breadcrumb" }, { kind: "component", type: i2.BreadcrumbItemComponent, selector: "c8y-breadcrumb-item", inputs: ["icon", "translate", "label", "path", "injector"] }, { kind: "component", type: i2.EmptyStateComponent, selector: "c8y-ui-empty-state", inputs: ["icon", "title", "subtitle", "horizontal"] }, { kind: "pipe", type: i2.C8yTranslatePipe, name: "translate" }, { kind: "directive", type: i2.C8yTranslateDirective, selector: "[translate],[ngx-translate]" }, { kind: "component", type: i2.LoadingComponent, selector: "c8y-loading", inputs: ["layout", "progress", "message"] }, { kind: "component", type: i2.TitleComponent, selector: "c8y-title", inputs: ["pageTitleUpdate"] }, { kind: "directive", type: i2.GuideHrefDirective, selector: "[c8y-guide-href]", inputs: ["c8y-guide-href"] }, { kind: "component", type: i2.GuideDocsComponent, selector: "[c8y-guide-docs]" }, { kind: "component", type: i2.HelpComponent, selector: "c8y-help", inputs: ["src", "isCollapsed", "priority", "icon"] }, { kind: "directive", type: RouterLink, selector: "[routerLink]", inputs: ["target", "queryParams", "fragment", "queryParamsHandling", "state", "info", "relativeTo", "preserveFragment", "skipLocationChange", "replaceUrl", "routerLink"] }, { kind: "component", type: CustomPropertyFieldComponent, selector: "c8y-custom-property-field", inputs: ["fieldDefinition", "form"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.9", ngImport: i0, type: CustomPropertiesComponent, decorators: [{
            type: Component,
            args: [{ selector: 'c8y-custom-properties', standalone: true, imports: [
                        CommonModule,
                        ReactiveFormsModule,
                        FormsModule,
                        CoreModule,
                        RouterLink,
                        CustomPropertyFieldComponent
                    ], template: "<c8y-title *ngIf=\"tenant\">\n  {{ tenant.company }}\n</c8y-title>\n\n<c8y-breadcrumb>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-layers'\"\n    [label]=\"'Tenants' | translate\"\n  ></c8y-breadcrumb-item>\n  <c8y-breadcrumb-item\n    [icon]=\"'c8y-layers'\"\n    [label]=\"'Subtenants' | translate\"\n    [path]=\"'/tenants'\"\n  ></c8y-breadcrumb-item>\n</c8y-breadcrumb>\n\n<ng-container>\n  <form\n    [formGroup]=\"customPropsForm\"\n    (ngSubmit)=\"onSubmit()\"\n  >\n    <div class=\"card card--fullpage m-b-0\">\n      <div class=\"card-header separator\">\n        <div\n          class=\"card-title\"\n          translate\n        >\n          Custom properties\n        </div>\n      </div>\n\n      <div class=\"inner-scroll\">\n        <div\n          class=\"card-block\"\n          *ngIf=\"!initialized\"\n        >\n          <c8y-loading></c8y-loading>\n        </div>\n\n        <c8y-help src=\"/docs/enterprise-tenant/managing-tenants/#custom-properties\"></c8y-help>\n\n        <!-- empty state -->\n        <c8y-ui-empty-state\n          [icon]=\"'property-script'\"\n          [title]=\"'No custom properties to display.' | translate\"\n          [subtitle]=\"'Add a new tenant property in Properties library.' | translate\"\n          *ngIf=\"fieldDefinitions?.length === 0 && initialized\"\n        >\n          <p c8y-guide-docs>\n            <small translate>\n              Find out more in the\n              <a c8y-guide-href=\"/docs/enterprise-tenant/managing-tenants/#custom-properties\">\n                User guide\n              </a>\n              .\n            </small>\n          </p>\n        </c8y-ui-empty-state>\n\n        <div\n          class=\"card-block\"\n          *ngIf=\"initialized\"\n        >\n          <ng-container *ngFor=\"let field of fieldDefinitions\">\n            <c8y-custom-property-field\n              [fieldDefinition]=\"field\"\n              [form]=\"customPropsForm\"\n            ></c8y-custom-property-field>\n          </ng-container>\n        </div>\n      </div>\n\n      <div\n        class=\"card-footer separator\"\n        *ngIf=\"initialized\"\n      >\n        <button\n          class=\"btn btn-default\"\n          type=\"button\"\n          [routerLink]=\"['/tenants']\"\n          translate\n          data-cy=\"custom-properties--cancel-button\"\n        >\n          Cancel\n        </button>\n        <button\n          class=\"btn btn-primary\"\n          type=\"submit\"\n          [disabled]=\"!(!customPropsForm.invalid && customPropsForm.dirty)\"\n          translate\n          data-cy=\"custom-properties--save-button\"\n        >\n          Save\n        </button>\n      </div>\n    </div>\n  </form>\n</ng-container>\n" }]
        }], ctorParameters: () => [{ type: i1.TenantService }, { type: i2.AlertService }, { type: i3.ActivatedRoute }, { type: i4.CustomPropertiesService }] });
//# sourceMappingURL=data:application/json;base64, 