import { Inject, Injectable } from '@angular/core';
import { DynamicItemLoader } from '@anglr/dynamic';
import { LAYOUT_COMPONENTS_LOADER } from '@anglr/dynamic/layout';
import { LayoutComponentsIteratorService } from '@anglr/dynamic/layout-editor';
import { Subscription } from 'rxjs';
import { LayoutManager } from '../layoutManager/layoutManager.service';
import * as i0 from "@angular/core";
import * as i1 from "../layoutManager/layoutManager.service";
import * as i2 from "@anglr/dynamic/layout-editor";
import * as i3 from "@anglr/dynamic";
/**
 * Register for layout components that are part of relations
 */
export class LayoutComponentsRegister {
    //######################### public properties #########################
    /**
     * Gets available types that are used as layout components
     */
    get types() {
        this.initPromise ?? (this.initPromise = this.initializeTypes());
        return this.initPromise.then(() => {
            return Object.keys(this.definedTypes);
        });
    }
    //######################### constructor #########################
    constructor(layoutManager, loader, iteratorSvc) {
        this.layoutManager = layoutManager;
        this.loader = loader;
        this.iteratorSvc = iteratorSvc;
        //######################### protected fields #########################
        /**
         * Subscriptions created during initialization
         */
        this.initSubscriptions = new Subscription();
        /**
         * Object storing defined types
         */
        this.definedTypes = {};
        /**
         * Initialization promise
         */
        this.initPromise = null;
        this.initSubscriptions.add(this.layoutManager.layoutChange.subscribe(() => this.initPromise = null));
    }
    //######################### public methods - implementation of OnDestroy #########################
    /**
     * Called when component is destroyed
     */
    ngOnDestroy() {
        this.initSubscriptions.unsubscribe();
    }
    //######################### public methods #########################
    /**
     * Gets type by its name
     * @param name - Name of type that should be obtained
     */
    async getType(name) {
        await (this.initPromise ?? (this.initPromise = this.initializeTypes()));
        return this.definedTypes[name]?.type ?? null;
    }
    /**
     * Gets display name of type by its name
     * @param name - Name of type that display name should be obtained
     */
    async getDisplayName(name) {
        await (this.initPromise ?? (this.initPromise = this.initializeTypes()));
        return this.definedTypes[name]?.displayName ?? undefined;
    }
    /**
     * Gets scope of type by its name
     * @param name - Name of type that scope should be obtained
     */
    async getScope(name) {
        await (this.initPromise ?? (this.initPromise = this.initializeTypes()));
        return this.definedTypes[name]?.scope;
    }
    /**
     * Gets component name of type byt its name
     * @param name - Name of type that component name should be obtained
     */
    async getComponentName(name) {
        await (this.initPromise ?? (this.initPromise = this.initializeTypes()));
        return this.definedTypes[name]?.name ?? null;
    }
    /**
     * Gets component package of type byt its name
     * @param name - Name of type that component package should be obtained
     */
    async getComponentPackage(name) {
        await (this.initPromise ?? (this.initPromise = this.initializeTypes()));
        return this.definedTypes[name]?.package ?? null;
    }
    //######################### protected method #########################
    /**
     * Initialize layout types
     */
    async initializeTypes() {
        this.definedTypes = {};
        if (!this.layoutManager.layout) {
            return;
        }
        const layoutComponents = this.iteratorSvc.getIteratorFor(this.layoutManager.layout);
        for await (const component of layoutComponents) {
            const type = await this.loader.loadItem(component.metadata);
            if (!type) {
                continue;
            }
            this.definedTypes[component.metadata.id] =
                {
                    type: type.data,
                    displayName: component.metadata.displayName,
                    name: component.metadata.name,
                    scope: component.metadata.scope,
                    package: component.metadata.package,
                };
        }
    }
}
LayoutComponentsRegister.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutComponentsRegister, deps: [{ token: i1.LayoutManager }, { token: LAYOUT_COMPONENTS_LOADER }, { token: i2.LayoutComponentsIteratorService }], target: i0.ɵɵFactoryTarget.Injectable });
LayoutComponentsRegister.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutComponentsRegister });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutComponentsRegister, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1.LayoutManager }, { type: i3.DynamicItemLoader, decorators: [{
                    type: Inject,
                    args: [LAYOUT_COMPONENTS_LOADER]
                }] }, { type: i2.LayoutComponentsIteratorService }] });
//# sourceMappingURL=layoutComponentsRegister.service.js.map