"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const cyia_code_util_1 = require("cyia-code-util");
const library_1 = require("../../library");
const platform_1 = require("../../platform/platform");
const util_1 = require("../../util");
const const_1 = require("../const");
function default_1(data, map) {
    return __awaiter(this, void 0, void 0, function* () {
        const callback = this.async();
        const selector = (0, cyia_code_util_1.createCssSelectorForTs)(data);
        const injector = this._compilation[const_1.InjectorSymbol];
        const buildPlatform = injector.get(platform_1.BuildPlatform);
        const templateScopeOutside = this._compilation[const_1.TemplateScopeSymbol];
        const selfTemplateNode = selector.queryOne(`VariableDeclaration[name="$self_${library_1.GLOBAL_TEMPLATE_SUFFIX}"]`);
        if (selfTemplateNode) {
            const content = selfTemplateNode.initializer.getText();
            const config = (0, util_1.literalResolve)(content);
            this.emitFile(config.outputPath + buildPlatform.fileExtname.contentTemplate, (0, util_1.literalResolve)(`\`${config.template}\``, {
                directivePrefix: buildPlatform.templateTransform.getData().directivePrefix,
                eventListConvert: buildPlatform.templateTransform.eventListConvert,
                templateInterpolation: buildPlatform.templateTransform.templateInterpolation,
                fileExtname: buildPlatform.fileExtname,
            }));
        }
        const libraryTemplateNode = selector.queryOne(`VariableDeclaration[name="library_${library_1.GLOBAL_TEMPLATE_SUFFIX}"]`);
        if (libraryTemplateNode) {
            const content = libraryTemplateNode.initializer.getText();
            const config = (0, util_1.literalResolve)(content);
            for (const key in config) {
                if (Object.prototype.hasOwnProperty.call(config, key)) {
                    const element = config[key];
                    templateScopeOutside.setScopeExtraUseComponents(key, {
                        useComponents: element.useComponents,
                        templateList: [element.template],
                    });
                }
            }
        }
        callback(undefined, data, map);
    });
}
exports.default = default_1;
