import { __awaiter } from "tslib";
import { Component, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FirstUppercaseLocalizeSAPipe } from '@anglr/common';
import { TitledDialogService } from '@anglr/common/material';
import { RelationsNodeBase, RelationNodeOutputSAComponent, RelationsNodeHeaderSAComponent, RelationNodeInputSAComponent } from '@anglr/dynamic/relations-editor';
import { ConfigureNodeEndpointSAComponent } from '@anglr/dynamic/layout-relations';
import { lastValueFrom } from '@jscrpt/common/rxjs';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/common/material";
import * as i2 from "@angular/common";
/**
 * Relations node component for deconstruct
 */
export class DeconstructNodeSAComponent extends RelationsNodeBase {
    //######################### protected properties - template bindings #########################
    /**
     * Gets added property names
     */
    get properties() {
        var _a, _b;
        var _c;
        if (!this.metadata) {
            return [];
        }
        (_a = (_c = this.metadata).relationsOptions) !== null && _a !== void 0 ? _a : (_c.relationsOptions = {
            properties: [],
        });
        return (_b = this.metadata.relationsOptions.properties) !== null && _b !== void 0 ? _b : [];
    }
    //######################### constructor #########################
    constructor(changeDetector, element, dialog) {
        super(changeDetector, element);
        this.dialog = dialog;
    }
    //######################### protected methods - template bindings #########################
    /**
     * Adds new property
     */
    addProperty() {
        return __awaiter(this, void 0, void 0, function* () {
            const name = {
                name: `property${this.properties.length + 1}`,
                defaultValue: undefined,
                noDefaultValue: true,
                skipInit: false,
                allowSkipInit: true,
            };
            yield this.configureEndpoint(name);
        });
    }
    /**
     * Removes property
     * @param name - Name of property to be removed
     */
    removeProperty(name) {
        var _a, _b;
        let index;
        if (((_b = (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.relationsOptions) === null || _b === void 0 ? void 0 : _b.properties) && (index = this.metadata.relationsOptions.properties.findIndex(itm => itm.name == name)) >= 0) {
            this.metadata.relationsOptions.properties.splice(index, 1);
            this.history.getNewState();
        }
    }
    /**
     * Configures endpoint
     * @param endpoint - Endpoint to be configured
     */
    configureEndpoint(endpoint) {
        var _a, _b;
        var _c;
        return __awaiter(this, void 0, void 0, function* () {
            const newEndpoint = JSON.parse(JSON.stringify(endpoint));
            const result = yield lastValueFrom(this.dialog.open(ConfigureNodeEndpointSAComponent, {
                title: 'configure property',
                width: '60vw',
                data: newEndpoint,
            }).afterClosed());
            //rename
            if (result && ((_a = this.metadata) === null || _a === void 0 ? void 0 : _a.relationsOptions)) {
                let index;
                if (this.metadata.relationsOptions.properties && (index = this.metadata.relationsOptions.properties.findIndex(itm => itm.name == endpoint.name)) >= 0) {
                    this.metadata.relationsOptions.properties.splice(index, 1);
                    this.metadata.relationsOptions.properties.splice(index, 0, newEndpoint);
                }
                else {
                    (_b = (_c = this.metadata.relationsOptions).properties) !== null && _b !== void 0 ? _b : (_c.properties = []);
                    this.metadata.relationsOptions.properties.push(newEndpoint);
                }
                this.history.getNewState();
            }
            return result !== null && result !== void 0 ? result : false;
        });
    }
}
DeconstructNodeSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DeconstructNodeSAComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i1.TitledDialogService }], target: i0.ɵɵFactoryTarget.Component });
DeconstructNodeSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: DeconstructNodeSAComponent, isStandalone: true, selector: "deconstruct-node", usesInheritance: true, ngImport: i0, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'deconstruct'|firstUppercaseLocalize\"></relations-node-header>\n\n<div class=\"node-content\">\n    <div class=\"inputs-outputs\">\n        <div class=\"inputs\">\n            <div class=\"input\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"object\" [parent]=\"this\"></relation-node-input>\n                <div>{{'object'|firstUppercaseLocalize}}</div>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <div class=\"output\" *ngFor=\"let property of properties\">\n                <div class=\"dynamic-flex-row\">\n                    <div>{{property.name}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); configureEndpoint(property)\">\n                        <span class=\"fas fa-pen\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeProperty(property.name)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n\n                <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property.name\" [parent]=\"this\"></relation-node-output>\n            </div>\n\n            <div class=\"dynamic-flex-row dynamic-flex-end\">\n                <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-left-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addProperty()\">\n                    <span class=\"fas fa-plus\"></span>\n                </button>\n            </div>\n        </div>\n    </div>\n</div>", dependencies: [{ 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: "ngmodule", type: CommonModule }, { kind: "directive", type: i2.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "pipe", type: FirstUppercaseLocalizeSAPipe, name: "firstUppercaseLocalize" }], changeDetection: i0.ChangeDetectionStrategy.OnPush });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: DeconstructNodeSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'deconstruct-node', standalone: true, imports: [
                        RelationsNodeHeaderSAComponent,
                        RelationNodeInputSAComponent,
                        RelationNodeOutputSAComponent,
                        CommonModule,
                        FirstUppercaseLocalizeSAPipe,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'deconstruct'|firstUppercaseLocalize\"></relations-node-header>\n\n<div class=\"node-content\">\n    <div class=\"inputs-outputs\">\n        <div class=\"inputs\">\n            <div class=\"input\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"object\" [parent]=\"this\"></relation-node-input>\n                <div>{{'object'|firstUppercaseLocalize}}</div>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <div class=\"output\" *ngFor=\"let property of properties\">\n                <div class=\"dynamic-flex-row\">\n                    <div>{{property.name}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); configureEndpoint(property)\">\n                        <span class=\"fas fa-pen\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeProperty(property.name)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n\n                <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"property.name\" [parent]=\"this\"></relation-node-output>\n            </div>\n\n            <div class=\"dynamic-flex-row dynamic-flex-end\">\n                <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-left-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addProperty()\">\n                    <span class=\"fas fa-plus\"></span>\n                </button>\n            </div>\n        </div>\n    </div>\n</div>" }]
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: i1.TitledDialogService }] });
//# sourceMappingURL=deconstructNode.component.js.map