import { __decorate, __metadata } from "tslib";
import { Injector, Input } from '@angular/core';
import { CodeExecutor, DynamicOutput, PureRelationsComponent } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { nameof } from '@jscrpt/common';
import { StateRelationsMetadataLoader } from './state.metadata';
/**
 * State relations component
 */
let StateRelations = class StateRelations {
    //######################### public properties - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    get relationsOptions() {
        return this.ɵRelationsOptions;
    }
    set relationsOptions(value) {
        this.ɵRelationsOptions = value;
        this.initialize();
    }
    //######################### constructor #########################
    constructor(injector) {
        this.injector = injector;
        /**
         * Code executor used for execution o
         */
        this.codeExecutor = this.injector.get(CodeExecutor);
    }
    //######################### public methods - implementation of OnChanges #########################
    /**
     * Called when input value changes
     */
    ngOnChanges(changes) {
        if (nameof('initState') in changes) {
            this.state = this.initState;
        }
    }
    //######################### public properties - dynamic outputs #########################
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    //######################### protected methods #########################
    /**
     * Initialize rest relations
     */
    initialize() {
        if (this.relationsOptions) {
            if (this.relationsOptions.inputFunctions) {
                for (const name in this.relationsOptions.inputFunctions) {
                    const inputFuncData = this.relationsOptions.inputFunctions[name];
                    if (inputFuncData.code) {
                        Object.defineProperty(this, name, {
                            configurable: true,
                            enumerable: true,
                            set: async (value) => {
                                if (!inputFuncData.code) {
                                    return;
                                }
                                const inputFunc = await this.codeExecutor.loadData(inputFuncData.id, inputFuncData.code);
                                if (!inputFunc) {
                                    return;
                                }
                                try {
                                    inputFunc.bind(this)(value);
                                }
                                catch (e) {
                                    console.error(e);
                                }
                            }
                        });
                    }
                }
            }
        }
    }
};
__decorate([
    Input(),
    __metadata("design:type", Object)
], StateRelations.prototype, "initState", void 0);
__decorate([
    DynamicOutput(),
    __metadata("design:type", Object)
], StateRelations.prototype, "state", void 0);
StateRelations = __decorate([
    PureRelationsComponent(),
    RelationsEditorMetadata(StateRelationsMetadataLoader),
    __metadata("design:paramtypes", [Injector])
], StateRelations);
export { StateRelations };
//# sourceMappingURL=state.relations.js.map