"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.SetupComponentDataService = void 0;
const core_1 = require("@angular-devkit/core");
const static_injector_1 = require("static-injector");
const change_component_1 = require("../component-template-inject/change-component");
const const_1 = require("./const");
const get_library_path_1 = require("./get-library-path");
const merge_using_component_path_1 = require("./merge-using-component-path");
const token_1 = require("./token");
const i0 = __importStar(require("static-injector"));
const i1 = __importStar(require("./output-template-metadata.service"));
const i2 = __importStar(require("../platform/platform"));
class SetupComponentDataService {
    constructor(dataGroup, entryPoint, addGlobalTemplateService, buildPlatform) {
        this.dataGroup = dataGroup;
        this.entryPoint = entryPoint;
        this.addGlobalTemplateService = addGlobalTemplateService;
        this.buildPlatform = buildPlatform;
    }
    run(data, originFileName, customStyleSheetProcessor) {
        const changedData = (0, change_component_1.changeComponent)(data);
        if (!changedData) {
            return data;
        }
        const useComponentPath = this.dataGroup.useComponentPath.get(originFileName);
        const componentClassName = changedData.componentName;
        const componentDirName = core_1.strings.dasherize(core_1.strings.camelize(componentClassName));
        const libraryPath = (0, get_library_path_1.getComponentOutputPath)(this.entryPoint, componentClassName);
        const styleUrlList = this.dataGroup.style.get(originFileName);
        const styleContentList = [];
        styleUrlList === null || styleUrlList === void 0 ? void 0 : styleUrlList.forEach((item) => {
            styleContentList.push(customStyleSheetProcessor.styleMap.get(item));
        });
        const selfTemplateImportStr = this.dataGroup.otherMetaCollectionGroup['$self']
            ? `<import src="${(0, core_1.resolve)((0, core_1.normalize)('/'), (0, core_1.join)((0, core_1.normalize)(const_1.LIBRARY_OUTPUT_ROOTDIR), this.entryPoint, 'self' + this.buildPlatform.fileExtname.contentTemplate))}"/>`
            : '';
        const insertComponentData = {
            id: core_1.strings.classify(this.entryPoint) +
                core_1.strings.classify(core_1.strings.camelize(componentDirName)),
            className: componentClassName,
            content: selfTemplateImportStr +
                this.dataGroup.outputContent.get(originFileName),
            libraryPath: libraryPath,
            useComponents: Object.assign(Object.assign({}, (0, merge_using_component_path_1.getUseComponents)(useComponentPath.libraryPath, useComponentPath.localPath, this.entryPoint)), this.addGlobalTemplateService.getSelfUseComponents()),
            moduleId: this.entryPoint,
        };
        if (styleContentList.length) {
            insertComponentData.style = styleContentList.join('\n');
        }
        const list = changedData.content.split(/\n|\r\n/g);
        list.splice(Math.max(list.length - 1, 0), 0, `let ${componentClassName}_${const_1.LIBRARY_COMPONENT_METADATA_SUFFIX}=${JSON.stringify(insertComponentData)}`);
        return list.join('\n');
    }
}
SetupComponentDataService.ɵfac = function SetupComponentDataService_Factory(t) { return new (t || SetupComponentDataService)(i0.ɵɵinject(token_1.RESOLVED_DATA_GROUP_TOKEN), i0.ɵɵinject(token_1.ENTRY_POINT_TOKEN), i0.ɵɵinject(i1.OutputTemplateMetadataService), i0.ɵɵinject(i2.BuildPlatform)); };
SetupComponentDataService.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: SetupComponentDataService, factory: SetupComponentDataService.ɵfac });
exports.SetupComponentDataService = SetupComponentDataService;
