import { Component, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { FirstUppercaseLocalizeSAPipe } from '@anglr/common';
import { TitledDialogService } from '@anglr/common/material';
import { FormModelBuilder, NumberInputModule } from '@anglr/common/forms';
import { RelationsNodeBase, RelationNodeInputSAComponent, RelationNodeOutputSAComponent, RelationsNodeHeaderSAComponent } from '@anglr/dynamic/relations-editor';
import { extend, isPresent } from '@jscrpt/common';
import { lastValueFrom } from '@jscrpt/common/rxjs';
import { SyncValuesRelationsOptionsModel } from './syncValuesNode.model';
import { ConfigureSyncPropertySAComponent } from '../misc/components';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/common/material";
import * as i2 from "@anglr/common/forms";
import * as i3 from "@angular/common";
import * as i4 from "@angular/forms";
/**
 * Sync values node component for negation
 */
export class SyncValuesNodeSAComponent extends RelationsNodeBase {
    //######################### constructor #########################
    constructor(changeDetector, element, dialog, formModelBuilder) {
        super(changeDetector, element);
        this.dialog = dialog;
        this.form = formModelBuilder.build(new SyncValuesRelationsOptionsModel(null));
        this.form.valueChanges.subscribe(value => {
            if (this.metadata?.relationsOptions) {
                extend(this.metadata.relationsOptions, value);
                this.history.getNewState();
            }
        });
    }
    //######################### protected methods - template bindings #########################
    /**
     * Adds new property
     */
    async addProperty() {
        await this.configureSyncProperty(this.metadata?.relationsOptions?.syncProperties?.length ?? 0);
        this.changeDetector.detectChanges();
    }
    /**
     * Removes property
     * @param index - Index of property to be removed
     */
    removeProperty(index) {
        if (!this.metadata?.relationsOptions?.syncProperties) {
            return;
        }
        this.metadata.relationsOptions.syncProperties.splice(index, 1);
        this.history.getNewState();
    }
    /**
     * Configures sync property
     * @param index - Index of sync property to be configured
     */
    async configureSyncProperty(index) {
        var _a;
        const result = await lastValueFrom(this.dialog.open(ConfigureSyncPropertySAComponent, {
            title: 'configure sync property',
            width: '60vw',
            data: this.metadata?.relationsOptions?.syncProperties?.[index] ?? ''
        }).afterClosed());
        if (isPresent(result) && this.metadata?.relationsOptions) {
            (_a = this.metadata.relationsOptions).syncProperties ?? (_a.syncProperties = []);
            this.metadata.relationsOptions.syncProperties[index] = result;
            this.history.getNewState();
        }
    }
    //######################### protected methods - overrides #########################
    /**
     * @inheritdoc
     */
    metadataSet() {
        if (!this.metadata?.relationsOptions) {
            return;
        }
        this.form.patchValue(this.metadata.relationsOptions);
    }
}
SyncValuesNodeSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: SyncValuesNodeSAComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i1.TitledDialogService }, { token: i2.FormModelBuilder }], target: i0.ɵɵFactoryTarget.Component });
SyncValuesNodeSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: SyncValuesNodeSAComponent, isStandalone: true, selector: "sync-values-node", providers: [
        FormModelBuilder,
    ], usesInheritance: true, ngImport: i0, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'sync values'|firstUppercaseLocalize\"></relations-node-header>\n\n<div class=\"node-content\" [formGroup]=\"form\">\n    <div class=\"dynamic-form-group dynamic-margin-bottom-extra-small\">\n        <label class=\"dynamic-control-label dynamic-text-uppercase\">{{'sync on'|firstUppercaseLocalize}}</label>\n        \n        <select class=\"dynamic-form-control\" formControlName=\"triggerSync\">\n            <option [ngValue]=\"false\">{{'idle'|firstUppercaseLocalize}}</option>\n            <option [ngValue]=\"true\">{{'trigger'|firstUppercaseLocalize}}</option>\n        </select>\n    </div>\n\n    <div class=\"dynamic-form-group dynamic-margin-bottom-extra-small\" *ngIf=\"!metadata?.relationsOptions?.triggerSync\">\n        <label class=\"dynamic-control-label dynamic-text-uppercase\">{{'idle timeout'|firstUppercaseLocalize}}</label>\n        <input type=\"text\" class=\"dynamic-form-control\" formControlName=\"idleTimeout\" number required>\n    </div>\n\n    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addProperty()\">\n        <span class=\"fas fa-plus\"></span>\n    </button>\n\n    <div class=\"inputs-outputs\">\n        <div class=\"inputs\">\n            <ng-container *ngFor=\"let property of metadata?.relationsOptions?.syncProperties; let index=index\">\n                <div class=\"input\">\n                    <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property\" [parent]=\"this\"></relation-node-input>\n                    <div>{{property}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); configureSyncProperty(index)\">\n                        <span class=\"fas fa-cog\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeProperty(index)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n            </ng-container>\n\n            <div class=\"input\" *ngIf=\"metadata?.relationsOptions?.triggerSync\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"trigger\" [parent]=\"this\"></relation-node-input>\n                <div>{{'trigger'|firstUppercaseLocalize}}</div>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <ng-container *ngFor=\"let property of metadata?.relationsOptions?.syncProperties\">\n                <div class=\"output\">\n                    <div>{{property}}</div>\n                    <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property\" [parent]=\"this\"></relation-node-output>\n                </div>\n            </ng-container>\n        </div>\n    </div>\n</div>", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i3.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i3.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "ngmodule", type: ReactiveFormsModule }, { kind: "directive", type: i4.NgSelectOption, selector: "option", inputs: ["ngValue", "value"] }, { kind: "directive", type: i4.ɵNgSelectMultipleOption, selector: "option", inputs: ["ngValue", "value"] }, { kind: "directive", type: i4.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: i4.SelectControlValueAccessor, selector: "select:not([multiple])[formControlName],select:not([multiple])[formControl],select:not([multiple])[ngModel]", inputs: ["compareWith"] }, { kind: "directive", type: i4.NgControlStatus, selector: "[formControlName],[ngModel],[formControl]" }, { kind: "directive", type: i4.NgControlStatusGroup, selector: "[formGroupName],[formArrayName],[ngModelGroup],[formGroup],form:not([ngNoForm]),[ngForm]" }, { kind: "directive", type: i4.RequiredValidator, selector: ":not([type=checkbox])[required][formControlName],:not([type=checkbox])[required][formControl],:not([type=checkbox])[required][ngModel]", inputs: ["required"] }, { kind: "directive", type: i4.FormGroupDirective, selector: "[formGroup]", inputs: ["formGroup"], outputs: ["ngSubmit"], exportAs: ["ngForm"] }, { kind: "directive", type: i4.FormControlName, selector: "[formControlName]", inputs: ["formControlName", "disabled", "ngModel"], outputs: ["ngModelChange"] }, { kind: "ngmodule", type: NumberInputModule }, { kind: "directive", type: i2.NumberInputControlValueAccessor, selector: "input[number][formControlName],input[number][formControl],input[number][ngModel]" }, { kind: "directive", type: i2.NumberInputValidatorDirective, selector: "input[number][formControlName],input[number][formControl],input[number][ngModel]" }, { kind: "component", type: RelationsNodeHeaderSAComponent, selector: "relations-node-header", inputs: ["parent", "destroySubject", "name"] }, { kind: "component", type: RelationNodeInputSAComponent, selector: "relation-node-input" }, { kind: "component", type: RelationNodeOutputSAComponent, selector: "relation-node-output" }, { kind: "pipe", type: FirstUppercaseLocalizeSAPipe, name: "firstUppercaseLocalize" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: SyncValuesNodeSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'sync-values-node', standalone: true, imports: [
                        CommonModule,
                        ReactiveFormsModule,
                        NumberInputModule,
                        RelationsNodeHeaderSAComponent,
                        RelationNodeInputSAComponent,
                        RelationNodeOutputSAComponent,
                        FirstUppercaseLocalizeSAPipe,
                    ], providers: [
                        FormModelBuilder,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'sync values'|firstUppercaseLocalize\"></relations-node-header>\n\n<div class=\"node-content\" [formGroup]=\"form\">\n    <div class=\"dynamic-form-group dynamic-margin-bottom-extra-small\">\n        <label class=\"dynamic-control-label dynamic-text-uppercase\">{{'sync on'|firstUppercaseLocalize}}</label>\n        \n        <select class=\"dynamic-form-control\" formControlName=\"triggerSync\">\n            <option [ngValue]=\"false\">{{'idle'|firstUppercaseLocalize}}</option>\n            <option [ngValue]=\"true\">{{'trigger'|firstUppercaseLocalize}}</option>\n        </select>\n    </div>\n\n    <div class=\"dynamic-form-group dynamic-margin-bottom-extra-small\" *ngIf=\"!metadata?.relationsOptions?.triggerSync\">\n        <label class=\"dynamic-control-label dynamic-text-uppercase\">{{'idle timeout'|firstUppercaseLocalize}}</label>\n        <input type=\"text\" class=\"dynamic-form-control\" formControlName=\"idleTimeout\" number required>\n    </div>\n\n    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addProperty()\">\n        <span class=\"fas fa-plus\"></span>\n    </button>\n\n    <div class=\"inputs-outputs\">\n        <div class=\"inputs\">\n            <ng-container *ngFor=\"let property of metadata?.relationsOptions?.syncProperties; let index=index\">\n                <div class=\"input\">\n                    <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property\" [parent]=\"this\"></relation-node-input>\n                    <div>{{property}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); configureSyncProperty(index)\">\n                        <span class=\"fas fa-cog\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeProperty(index)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n            </ng-container>\n\n            <div class=\"input\" *ngIf=\"metadata?.relationsOptions?.triggerSync\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"trigger\" [parent]=\"this\"></relation-node-input>\n                <div>{{'trigger'|firstUppercaseLocalize}}</div>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <ng-container *ngFor=\"let property of metadata?.relationsOptions?.syncProperties\">\n                <div class=\"output\">\n                    <div>{{property}}</div>\n                    <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property\" [parent]=\"this\"></relation-node-output>\n                </div>\n            </ng-container>\n        </div>\n    </div>\n</div>" }]
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: i1.TitledDialogService }, { type: i2.FormModelBuilder }] });
//# sourceMappingURL=syncValuesNode.component.js.map