import { __decorate, __metadata, __param } from "tslib";
import { Inject } from '@angular/core';
import { DynamicItemLoader } from '@anglr/dynamic';
import { LAYOUT_COMPONENTS_LOADER } from '@anglr/dynamic/layout';
import { getDescendantsGetter } from '../../decorators';
/**
 * Iterator for layout components children metadata
 */
let LayoutComponentsChildrenIterator = class LayoutComponentsChildrenIterator {
    //######################### public properties #########################
    /**
     * Async iterator for layout components children metadata
     */
    [Symbol.asyncIterator]() {
        let x = 0;
        const initPromise = this.initPromise ?? (this.initPromise = this.getInitPromise());
        const items = this.items;
        return {
            async next() {
                await initPromise;
                if (x < items.length) {
                    return {
                        value: items[x++],
                        done: false
                    };
                }
                return {
                    value: items[x - 1],
                    done: true
                };
            }
        };
    }
    //######################### constructor #########################
    constructor(layoutMetadata, loader, logger) {
        this.layoutMetadata = layoutMetadata;
        this.loader = loader;
        this.logger = logger;
        //######################### protected fields #########################
        /**
         * Array of layout component iterator children items
         */
        this.items = [];
        /**
         * Indication whether was iterator initialized
         */
        this.initialized = false;
        /**
         * Initialization promise
         */
        this.initPromise = null;
    }
    //######################### protected methods #########################
    /**
     * Gets children in layout components metadata hierarchy
     */
    async getChildren() {
        await this.getComponent(this.layoutMetadata);
    }
    /**
     * Gets children metadata for single layout component metadata
     * @param metadata - Metadata for layout component
     */
    async getComponent(metadata) {
        const def = await this.loader.loadItem(metadata);
        if (!def) {
            this.logger?.debug('LayoutComponentsChildrenIterator: failed to get dynamic component type for children iterator! {{@data}}', { data: { package: metadata.package, name: metadata.name } });
            return;
        }
        const getDescendants = getDescendantsGetter(def.data);
        //component does not have children
        if (!getDescendants) {
            return;
        }
        const childrenMeta = getDescendants(metadata.options);
        for (let x = 0; x < childrenMeta.length; x++) {
            this.items.push({
                index: x,
                metadata: childrenMeta[x]
            });
        }
    }
    /**
     * Gets initialization promise
     */
    async getInitPromise() {
        if (!this.initialized) {
            this.initialized = true;
            await this.getChildren();
        }
    }
};
LayoutComponentsChildrenIterator = __decorate([
    __param(1, Inject(LAYOUT_COMPONENTS_LOADER)),
    __metadata("design:paramtypes", [Object, DynamicItemLoader, Object])
], LayoutComponentsChildrenIterator);
export { LayoutComponentsChildrenIterator };
//# sourceMappingURL=layoutComponentsChildren.iterator.js.map