import { __awaiter, __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]() {
        var _a;
        let x = 0;
        const initPromise = (_a = this.initPromise) !== null && _a !== void 0 ? _a : (this.initPromise = this.getInitPromise());
        const items = this.items;
        return {
            next() {
                return __awaiter(this, void 0, void 0, function* () {
                    yield 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
     */
    getChildren() {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.getComponent(this.layoutMetadata);
        });
    }
    /**
     * Gets children metadata for single layout component metadata
     * @param metadata - Metadata for layout component
     */
    getComponent(metadata) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const def = yield this.loader.loadItem(metadata);
            if (!def) {
                (_a = this.logger) === null || _a === void 0 ? void 0 : _a.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
     */
    getInitPromise() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.initialized) {
                this.initialized = true;
                yield this.getChildren();
            }
        });
    }
};
LayoutComponentsChildrenIterator = __decorate([
    __param(1, Inject(LAYOUT_COMPONENTS_LOADER)),
    __metadata("design:paramtypes", [Object, DynamicItemLoader, Object])
], LayoutComponentsChildrenIterator);
export { LayoutComponentsChildrenIterator };
//# sourceMappingURL=layoutComponentsChildren.iterator.js.map