import { __awaiter } from "tslib";
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
     */
    extractMetadata(metadata) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            const cacheId = `${metadata.package}-${metadata.name}`;
            if (this._cache[cacheId]) {
                return this._cache[cacheId];
            }
            const type = yield this._loader.loadItem(metadata);
            if (!type) {
                return null;
            }
            const metadataType = type.data;
            if (!metadataType.layoutEditorMetadata) {
                (_a = this._logger) === null || _a === void 0 ? void 0 : _a.warn('LayoutEditorMetadataExtractor: Missing metadata! ', { package: metadata.package, name: metadata.name });
                return null;
            }
            (_b = this._logger) === null || _b === void 0 ? void 0 : _b.debug('LayoutEditorMetadataExtractor: Reading metadata! ', { package: metadata.package, name: metadata.name });
            const metadataData = yield 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