"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutputTemplateMetadataService = void 0;
const core_1 = require("@angular-devkit/core");
const static_injector_1 = require("static-injector");
const const_1 = require("./const");
const merge_using_component_path_1 = require("./merge-using-component-path");
const token_1 = require("./token");
const i0 = __importStar(require("static-injector"));
class OutputTemplateMetadataService {
    constructor(entryFile, dataGroup, entryPoint) {
        this.entryFile = entryFile;
        this.dataGroup = dataGroup;
        this.entryPoint = entryPoint;
    }
    run(fileName, data, sourceFile) {
        const list = data.split(/\n|\r\n/g);
        list.splice(list.length - 1, 0, `${this.getSelfTemplate()};${this.getLibraryTemplate()}`);
        return list.join('\n');
    }
    getSelfTemplate() {
        const selfMetaCollection = this.dataGroup.otherMetaCollectionGroup['$self'];
        if (!selfMetaCollection) {
            return '';
        }
        this.selfMetaCollection = selfMetaCollection;
        const templateStr = selfMetaCollection.templateList
            .map((item) => item.content)
            .join('');
        const extraTemplateData = {
            template: templateStr,
            outputPath: (0, core_1.resolve)((0, core_1.normalize)('/'), (0, core_1.join)((0, core_1.normalize)(const_1.LIBRARY_OUTPUT_ROOTDIR), this.entryPoint, 'self')),
        };
        delete this.dataGroup.otherMetaCollectionGroup['$self'];
        return `let $self_${const_1.GLOBAL_TEMPLATE_SUFFIX}=${JSON.stringify(extraTemplateData)}`;
    }
    getLibraryTemplate() {
        if (!Object.keys(this.dataGroup.otherMetaCollectionGroup).length) {
            return '';
        }
        const obj = {};
        for (const key in this.dataGroup.otherMetaCollectionGroup) {
            if (Object.prototype.hasOwnProperty.call(this.dataGroup.otherMetaCollectionGroup, key)) {
                const element = this.dataGroup.otherMetaCollectionGroup[key];
                const templateStr = element.templateList
                    .map((item) => item.content)
                    .join('');
                const useComponents = (0, merge_using_component_path_1.getUseComponents)(Array.from(element.libraryPath), Array.from(element.localPath), this.entryPoint);
                const extraTemplateData = {
                    template: templateStr,
                    useComponents: useComponents,
                };
                obj[key] = extraTemplateData;
            }
        }
        return `let library_${const_1.GLOBAL_TEMPLATE_SUFFIX}=${JSON.stringify(obj)}`;
    }
    getSelfUseComponents() {
        if (!this.selfUseComponents) {
            const selfMetaCollection = this.selfMetaCollection ||
                this.dataGroup.otherMetaCollectionGroup['$self'];
            if (!selfMetaCollection) {
                return {};
            }
            const useComponents = (0, merge_using_component_path_1.getUseComponents)(Array.from(selfMetaCollection.libraryPath), Array.from(selfMetaCollection.localPath), this.entryPoint);
            this.selfUseComponents = useComponents;
        }
        return this.selfUseComponents;
    }
}
OutputTemplateMetadataService.ɵfac = function OutputTemplateMetadataService_Factory(t) { return new (t || OutputTemplateMetadataService)(i0.ɵɵinject(token_1.ENTRY_FILE_TOKEN), i0.ɵɵinject(token_1.RESOLVED_DATA_GROUP_TOKEN), i0.ɵɵinject(token_1.ENTRY_POINT_TOKEN)); };
OutputTemplateMetadataService.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: OutputTemplateMetadataService, factory: OutputTemplateMetadataService.ɵfac });
exports.OutputTemplateMetadataService = OutputTemplateMetadataService;
