import { __awaiter } from "tslib";
import { DynamicItemExtensionBase } from '@anglr/dynamic';
import { RelationsComponentManager, RelationsProcessor } from '../../services';
/**
 * Extension that allows registration of component for relations
 */
export class RelationsRegistrationExtension extends DynamicItemExtensionBase {
    constructor() {
        //######################### protected properties #########################
        super(...arguments);
        /**
         * Instance of relations processor
         */
        this.relationsProcessor = null;
        /**
         * Instance of component manager
         */
        this.componentManager = null;
    }
    //######################### public methods - overrides #########################
    /**
     * @inheritdoc
     */
    onInit() {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.injector || !this.metadata || !this.instance) {
                return;
            }
            (_a = this.relationsProcessor) !== null && _a !== void 0 ? _a : (this.relationsProcessor = this.injector.get(RelationsProcessor, null));
            (_b = this.componentManager) !== null && _b !== void 0 ? _b : (this.componentManager = this.injector.get(RelationsComponentManager, null));
            if (!this.relationsProcessor || !this.componentManager) {
                return;
            }
            this.componentManager.registerComponent(this.metadata.id, this.instance);
            yield this.relationsProcessor.initialized;
            this.relationsProcessor.updateRelations(this.metadata.id);
        });
    }
    /**
     * @inheritdoc
     */
    onDestroy() {
        if (!this.relationsProcessor || !this.componentManager || !this.metadata) {
            return;
        }
        this.relationsProcessor.destroyComponent(this.metadata.id);
        this.componentManager.unregisterComponent(this.metadata.id);
    }
}
//# sourceMappingURL=relationsRegistration.extension.js.map