import { Component, ChangeDetectionStrategy, ChangeDetectorRef, ElementRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CodeEditorDialogComponent, TypescriptLanguageModel } from '@anglr/dynamic';
import { RelationsNodeBase, RelationNodeOutputSAComponent, RelationsNodeHeaderSAComponent, RelationNodeInputSAComponent } from '@anglr/dynamic/relations-editor';
import { ConfigureNodeEndpointSAComponent } from '@anglr/dynamic/layout-relations';
import { TitledDialogService } from '@anglr/common/material';
import { FirstUppercaseLocalizeSAPipe } from '@anglr/common';
import { generateId } from '@jscrpt/common';
import { lastValueFrom } from '@jscrpt/common/rxjs';
import typings from '!!raw-loader?esModule!@anglr/dynamic/typings/state';
import { languages } from 'monaco-editor';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/common/material";
import * as i2 from "@angular/common";
languages.typescript.typescriptDefaults.addExtraLib(typings, 'file:///node_modules/@types/state/index.d.ts');
/**
 * Relations node component for state
 */
export class StateNodeSAComponent extends RelationsNodeBase {
    //######################### protected properties - template bindings #########################
    /**
     * Gets added input function names
     */
    get inputFunctions() {
        var _a;
        if (!this.metadata) {
            return [];
        }
        (_a = this.metadata).relationsOptions ?? (_a.relationsOptions = {
            inputFunctions: {},
        });
        return Object.keys(this.metadata.relationsOptions.inputFunctions ?? {});
    }
    //######################### constructor #########################
    constructor(changeDetector, element, dialog) {
        super(changeDetector, element);
        this.dialog = dialog;
    }
    //######################### protected methods - template bindings #########################
    /**
     * Adds new input function
     */
    async addInputFunc() {
        var _a, _b;
        const inputFunc = {
            id: generateId(12),
            code: null,
        };
        const name = {
            name: `inputFunc${this.inputFunctions.length + 1}`,
            defaultValue: null,
            noDefaultValue: true,
            skipInit: false,
        };
        if (await this.configureEndpoint(name)) {
            if (!this.metadata?.relationsOptions || !this.metadata.nodeMetadata) {
                return;
            }
            (_a = this.metadata.relationsOptions).inputFunctions ?? (_a.inputFunctions = {});
            this.metadata.relationsOptions.inputFunctions[name.name] = inputFunc;
            (_b = this.metadata.nodeMetadata).options ?? (_b.options = {
                contents: {}
            });
            if (this.metadata.nodeMetadata.options.contents) {
                this.metadata.nodeMetadata.options.contents[name.name] = '';
            }
            this.history.getNewState();
        }
    }
    /**
     * Removes input function
     * @param name - Name of input function to be removed
     */
    removeInputFunc(name) {
        if (this.metadata?.relationsOptions?.inputFunctions && name in this.metadata.relationsOptions.inputFunctions) {
            delete this.metadata.relationsOptions.inputFunctions[name];
            if (this.metadata.nodeMetadata?.options?.contents) {
                delete this.metadata.nodeMetadata.options.contents[name];
            }
            this.history.getNewState();
        }
    }
    /**
     * Renames input func
     * @param name - Allows renaming of input func
     */
    async rename(name) {
        await this.configureEndpoint({
            name,
            defaultValue: null,
            noDefaultValue: true,
            skipInit: false,
        });
        this.changeDetector.detectChanges();
    }
    /**
     * Configures endpoint
     * @param endpoint - Endpoint to be configured
     */
    async configureEndpoint(endpoint) {
        const copy = JSON.parse(JSON.stringify(endpoint));
        const result = await lastValueFrom(this.dialog.open(ConfigureNodeEndpointSAComponent, {
            title: 'configure input function',
            width: '60vw',
            data: copy,
        }).afterClosed());
        //rename
        if (result) {
            if (this.metadata?.relationsOptions?.inputFunctions?.[endpoint.name] && (this.metadata.nodeMetadata?.options?.contents?.[endpoint.name] === '' || this.metadata.nodeMetadata?.options?.contents?.[endpoint.name])) {
                this.metadata.relationsOptions.inputFunctions[copy.name] = this.metadata.relationsOptions.inputFunctions[endpoint.name];
                delete this.metadata.relationsOptions.inputFunctions[endpoint.name];
                this.metadata.nodeMetadata.options.contents[copy.name] = this.metadata.nodeMetadata.options.contents[endpoint.name];
                delete this.metadata.nodeMetadata.options.contents[endpoint.name];
                this.history.getNewState();
            }
            else {
                endpoint.name = copy.name;
            }
        }
        return result ?? false;
    }
    /**
     * Shows code editor
     * @param name - Name of input func
     */
    async showCodeEditor(name) {
        var _a, _b, _c;
        const result = await lastValueFrom(this.dialog.open(CodeEditorDialogComponent, {
            title: 'Code editor',
            width: '75vw',
            height: '75vh',
            data: {
                content: this.metadata?.nodeMetadata?.options?.contents?.[name] ?? '',
                languageModel: TypescriptLanguageModel(`import {InputFunction} from 'state';

/**
 * Input function that handles incoming data and can change state
 */
const inputFunc: InputFunction<any, any> = function(_data)
{
};

export default inputFunc;

`),
            }
        }).afterClosed());
        if (!this.metadata?.relationsOptions || !result || !this.metadata.nodeMetadata) {
            return;
        }
        (_a = this.metadata.relationsOptions).inputFunctions ?? (_a.inputFunctions = {});
        (_b = this.metadata.nodeMetadata).options ?? (_b.options = {
            contents: {}
        });
        (_c = this.metadata.nodeMetadata.options).contents ?? (_c.contents = {});
        this.metadata.relationsOptions.inputFunctions[name].code = result.code;
        this.metadata.nodeMetadata.options.contents[name] = result.content;
        this.history.getNewState();
    }
}
StateNodeSAComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: StateNodeSAComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: i1.TitledDialogService }], target: i0.ɵɵFactoryTarget.Component });
StateNodeSAComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.0.4", type: StateNodeSAComponent, isStandalone: true, selector: "state-node", usesInheritance: true, ngImport: i0, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'state'|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=\"initState\" [parent]=\"this\"></relation-node-input>\n                <div>{{'init state'|firstUppercaseLocalize}}</div>\n            </div>\n\n            <div class=\"input\" *ngFor=\"let inputFunc of inputFunctions\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"inputFunc\" [parent]=\"this\"></relation-node-input>\n\n                <div class=\"dynamic-flex-row\">\n                    <div>{{inputFunc}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); rename(inputFunc)\">\n                        <span class=\"fas fa-pen\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); showCodeEditor(inputFunc)\">\n                        <span class=\"fas fa-code\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeInputFunc(inputFunc)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n            </div>\n\n            <div class=\"dynamic-flex-row\">\n                <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-left-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addInputFunc()\">\n                    <span class=\"fas fa-plus\"></span>\n                </button>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <div class=\"output\">\n                <div>{{'state'|firstUppercaseLocalize}}</div>\n                <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"state\" [parent]=\"this\"></relation-node-output>\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: StateNodeSAComponent, decorators: [{
            type: Component,
            args: [{ selector: 'state-node', standalone: true, imports: [
                        RelationsNodeHeaderSAComponent,
                        RelationNodeInputSAComponent,
                        RelationNodeOutputSAComponent,
                        CommonModule,
                        FirstUppercaseLocalizeSAPipe,
                    ], changeDetection: ChangeDetectionStrategy.OnPush, template: "<relations-node-header [parent]=\"this\" [destroySubject]=\"destroySubject\" [name]=\"'state'|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=\"initState\" [parent]=\"this\"></relation-node-input>\n                <div>{{'init state'|firstUppercaseLocalize}}</div>\n            </div>\n\n            <div class=\"input\" *ngFor=\"let inputFunc of inputFunctions\">\n                <relation-node-input [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" [name]=\"inputFunc\" [parent]=\"this\"></relation-node-input>\n\n                <div class=\"dynamic-flex-row\">\n                    <div>{{inputFunc}}</div>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); rename(inputFunc)\">\n                        <span class=\"fas fa-pen\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-horizontal-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); showCodeEditor(inputFunc)\">\n                        <span class=\"fas fa-code\"></span>\n                    </button>\n\n                    <button type=\"button\" class=\"dynamic-btn-icon\" (click)=\"$event.preventDefault(); $event.stopPropagation(); removeInputFunc(inputFunc)\">\n                        <span class=\"fas fa-times\"></span>\n                    </button>\n                </div>\n            </div>\n\n            <div class=\"dynamic-flex-row\">\n                <button type=\"button\" class=\"dynamic-btn-icon dynamic-margin-left-extra-small\" (click)=\"$event.preventDefault(); $event.stopPropagation(); addInputFunc()\">\n                    <span class=\"fas fa-plus\"></span>\n                </button>\n            </div>\n        </div>\n\n        <div class=\"outputs\">\n            <div class=\"output\">\n                <div>{{'state'|firstUppercaseLocalize}}</div>\n                <relation-node-output [parentCoordiantes]=\"nodePosition\" [zoomLevel]=\"zoomLevel\" name=\"state\" [parent]=\"this\"></relation-node-output>\n            </div>\n        </div>\n    </div>\n</div>" }]
        }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: i1.TitledDialogService }] });
//# sourceMappingURL=stateNode.component.js.map