import { __awaiter, __decorate, __metadata } from "tslib";
import { Injector } from '@angular/core';
import { defineAssignedProp, defineSkipInitProp, RelationsComponentManager, RelationsProcessor } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { Subject } from 'rxjs';
import { ComponentInputsRelationsMetadataLoader } from './componentInputs.metadata';
/**
 * Relations used for connecting relations inputs with external relations
 */
let ComponentInputsRelations = class ComponentInputsRelations {
    //######################### constructor #########################
    constructor(injector) {
        this.injector = injector;
        //######################### protected properties #########################
        /**
         * Relations processor instance
         */
        this.relationsProcessor = this.injector.get(RelationsProcessor, null);
        /**
         * Relations component manager
         */
        this.componentManager = this.injector.get(RelationsComponentManager, null);
        /**
         * Id of component inputs relations
         */
        this.id = '';
    }
    //######################### public methods - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    ngOnChanges(changes) {
        var _a, _b;
        for (const key of Object.keys(changes)) {
            const $this = this;
            $this[key] = changes[key].currentValue;
            (_b = (_a = $this[`${key}Change`]) === null || _a === void 0 ? void 0 : _a.next) === null || _b === void 0 ? void 0 : _b.call(_a);
        }
    }
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * @inheritdoc
     */
    ngOnDestroy() {
        var _a, _b;
        if (this.id) {
            (_a = this.relationsProcessor) === null || _a === void 0 ? void 0 : _a.destroyComponent(this.id);
            (_b = this.componentManager) === null || _b === void 0 ? void 0 : _b.unregisterComponent(this.id);
        }
    }
    //######################### public methods #########################
    /**
     * Initialize inputs for component
     * @param inputRelations - Input relations metadata
     * @param id - Id of input relations
     */
    initInputs(inputRelations, id) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            this.id = id;
            if (!this.componentManager || !this.relationsProcessor) {
                return;
            }
            for (const input of (_b = (_a = inputRelations.relationsOptions) === null || _a === void 0 ? void 0 : _a.inputs) !== null && _b !== void 0 ? _b : []) {
                Object.defineProperty(this, input.name, {
                    get: function () {
                        return this[`ɵ${input.name}`];
                    },
                    set: function (value) {
                        this[`ɵ${input.name}`] = value;
                        defineAssignedProp(this, input.name);
                    }
                });
                Object.defineProperty(this, `${input.name}Change`, {
                    value: new Subject()
                });
                //sets initial value
                if (input.defaultValue !== undefined) {
                    this[input.name] = input.defaultValue;
                }
                if (input.skipInit) {
                    defineSkipInitProp(this, input.name);
                }
            }
            this.componentManager.registerComponent(this.id, this);
            yield this.relationsProcessor.initialized;
            this.relationsProcessor.updateRelations(this.id);
        });
    }
};
ComponentInputsRelations = __decorate([
    RelationsEditorMetadata(ComponentInputsRelationsMetadataLoader),
    __metadata("design:paramtypes", [Injector])
], ComponentInputsRelations);
export { ComponentInputsRelations };
//# sourceMappingURL=componentInputs.relations.js.map