import { __awaiter, __decorate, __metadata } from "tslib";
import { Injectable } from '@angular/core';
import { ScopeRegister as RelationsScopeRegister } from '@anglr/dynamic/relations-editor';
import { DebounceCall, WithSync } from '@jscrpt/common';
import { Subscription } from 'rxjs';
import { LayoutComponentsRegister } from '../layoutComponentsRegister/layoutComponentsRegister.service';
import { LayoutManager } from '../layoutManager/layoutManager.service';
import * as i0 from "@angular/core";
import * as i1 from "../layoutManager/layoutManager.service";
import * as i2 from "../layoutComponentsRegister/layoutComponentsRegister.service";
/**
 * Service that is used for obtaining existing scopes from layout
 */
export class ScopeRegister extends RelationsScopeRegister {
    //######################### constructor #########################
    constructor(layoutManager, componentRegister) {
        super();
        this.layoutManager = layoutManager;
        this.componentRegister = componentRegister;
        //######################### protoected properties #########################
        /**
         * Subscriptions created during initialization
         */
        this.initSubscriptions = new Subscription();
        this.initSubscriptions.add(this.layoutManager.layoutChange.subscribe(() => this.getScopes()));
        this.getScopes();
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        this.initSubscriptions.unsubscribe();
    }
    //######################### protected methods #########################
    /**
     * Gets available scopes
     */
    getScopes() {
        return __awaiter(this, void 0, void 0, function* () {
            this.ɵScopes = [];
            const types = yield this.componentRegister.types;
            for (const type of types) {
                let scope = null;
                if ((scope = yield this.componentRegister.getScope(type))) {
                    if (this.ɵScopes.indexOf(scope) < 0) {
                        this.ɵScopes.push(scope);
                    }
                }
            }
            this.scopesChangeSubject.next();
        });
    }
}
ScopeRegister.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ScopeRegister, deps: [{ token: i1.LayoutManager }, { token: i2.LayoutComponentsRegister }], target: i0.ɵɵFactoryTarget.Injectable });
ScopeRegister.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ScopeRegister });
__decorate([
    DebounceCall(10),
    WithSync(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], ScopeRegister.prototype, "getScopes", null);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: ScopeRegister, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1.LayoutManager }, { type: i2.LayoutComponentsRegister }], propDecorators: { getScopes: [] } });
//# sourceMappingURL=scopeRegister.service.js.map