import { __decorate, __metadata } from "tslib";
import { Injector } from '@angular/core';
import { defineAssignedProp, defineSkipInitProp, RelationsChangeDetector, RelationsComponentManager, RelationsProcessor } from '@anglr/dynamic/relations';
import { RelationsEditorMetadata } from '@anglr/dynamic/relations-editor';
import { Subject } from 'rxjs';
import { ComponentOutputsRelationsMetadataLoader } from './componentOutputs.metadata';
/**
 * Relations used for connecting relations outputs with external relations
 */
let ComponentOutputsRelations = class ComponentOutputsRelations {
    //######################### 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 outputs relations
         */
        this.id = '';
    }
    //######################### public methods - implementation of RelationsComponent #########################
    /**
     * @inheritdoc
     */
    ngOnChanges(changes) {
        for (const key of Object.keys(changes)) {
            this.customComponent[key] = changes[key].currentValue;
        }
    }
    /**
     * @inheritdoc
     */
    invalidateVisuals() {
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * @inheritdoc
     */
    ngOnDestroy() {
        if (this.id) {
            this.relationsProcessor?.destroyComponent(this.id);
            this.componentManager?.unregisterComponent(this.id);
        }
    }
    //######################### public methods #########################
    /**
     * Initialize outputs for component
     * @param outputRelations - Output relations metadata
     * @param id - Id of output relations
     */
    async initOutputs(outputRelations, id, customComponent) {
        this.id = id;
        this.customComponent = customComponent;
        if (!this.componentManager || !this.relationsProcessor) {
            return;
        }
        for (const output of outputRelations.relationsOptions?.outputs ?? []) {
            Object.defineProperty(this.customComponent, output.name, {
                get: function () {
                    return this[`ɵ${output.name}`];
                },
                set: function (value) {
                    this[`ɵ${output.name}`] = value;
                    defineAssignedProp(this, output.name);
                    const injector = this.ɵɵinjector;
                    if (!injector) {
                        return;
                    }
                    const componentManager = injector.get(RelationsComponentManager);
                    const changeDetector = injector.get(RelationsChangeDetector);
                    const id = componentManager.getId(this);
                    if (!id) {
                        return;
                    }
                    changeDetector.markForCheck({
                        componentId: id,
                        outputName: output.name,
                    });
                }
            });
            Object.defineProperty(this.customComponent, `${output.name}Change`, {
                value: new Subject()
            });
            //sets initial value
            if (output.defaultValue !== undefined) {
                this[output.name] = output.defaultValue;
            }
            if (output.skipInit) {
                defineSkipInitProp(this.customComponent, output.name);
            }
        }
        this.componentManager.registerComponent(this.id, this);
        await this.relationsProcessor.initialized;
        this.relationsProcessor.updateRelations(this.id);
    }
};
ComponentOutputsRelations = __decorate([
    RelationsEditorMetadata(ComponentOutputsRelationsMetadataLoader),
    __metadata("design:paramtypes", [Injector])
], ComponentOutputsRelations);
export { ComponentOutputsRelations };
//# sourceMappingURL=componentOutputs.relations.js.map