import { Inject, Injectable, Optional } from '@angular/core';
import { DynamicItemLoader } from '@anglr/dynamic';
import { LAYOUT_COMPONENTS_LOADER } from '@anglr/dynamic/layout';
import { LOGGER } from '@anglr/common';
import * as i0 from "@angular/core";
import * as i1 from "@anglr/dynamic";
/**
 * Class used for extracting layout editor metadata
 */
export class LayoutEditorMetadataExtractor {
    //######################### constructor #########################
    constructor(_loader, _logger) {
        this._loader = _loader;
        this._logger = _logger;
        //######################### protected fields #########################
        /**
         * Cache for layout editor metadata
         */
        this._cache = {};
    }
    //######################### public methods #########################
    /**
     * Extracts layout editor metadata from layout metadata
     * @param metadata - Metadata describing type that contains metadata
     */
    async extractMetadata(metadata) {
        const cacheId = `${metadata.package}-${metadata.name}`;
        if (this._cache[cacheId]) {
            return this._cache[cacheId];
        }
        const type = await this._loader.loadItem(metadata);
        if (!type) {
            return null;
        }
        const metadataType = type.data;
        if (!metadataType.layoutEditorMetadata) {
            this._logger?.warn('LayoutEditorMetadataExtractor: Missing metadata! ', { package: metadata.package, name: metadata.name });
            return null;
        }
        this._logger?.debug('LayoutEditorMetadataExtractor: Reading metadata! ', { package: metadata.package, name: metadata.name });
        const metadataData = await metadataType.layoutEditorMetadata;
        Object.freeze(metadataData);
        this._cache[cacheId] = metadataData;
        return metadataData;
    }
}
LayoutEditorMetadataExtractor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutEditorMetadataExtractor, deps: [{ token: LAYOUT_COMPONENTS_LOADER }, { token: LOGGER, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
LayoutEditorMetadataExtractor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutEditorMetadataExtractor });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.0.4", ngImport: i0, type: LayoutEditorMetadataExtractor, decorators: [{
            type: Injectable
        }], ctorParameters: () => [{ type: i1.DynamicItemLoader, decorators: [{
                    type: Inject,
                    args: [LAYOUT_COMPONENTS_LOADER]
                }] }, { type: undefined, decorators: [{
                    type: Inject,
                    args: [LOGGER]
                }, {
                    type: Optional
                }] }] });
//# sourceMappingURL=layoutEditorMetadataExtractor.service.js.map