"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lowcode_code_generator_1 = require("@alilc/lowcode-code-generator");
const const_1 = require("./const");
const lowcode_code_generator_2 = tslib_1.__importDefault(require("@alilc/lowcode-code-generator"));
const transformJsExpression_1 = require("./core/jsx/handlers/transformJsExpression");
const transformThis2Context_1 = require("./core/jsx/handlers/transformThis2Context");
const utils_1 = require("../../../utils");
const { jsExpression: { generateExpression }, compositeType: { generateCompositeType }, scope: { Scope } } = lowcode_code_generator_2.default.utils;
const { createReactNodeGenerator } = utils_1.nodeToVue;
const pluginFactory = (config) => {
    const cfg = Object.assign({ fileType: 'vue', 
        // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
        nodeTypeMapping: {} }, config);
    const { nodeTypeMapping } = cfg;
    const plugin = (pre) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const next = Object.assign({}, pre);
        const { tolerateEvalErrors = true, evalErrorsHandler = '' } = next.contextData;
        // 这里会将内部的一些子上下文的访问(this.xxx)转换为 __$$context.xxx 的形式
        // 与 Rax 所不同的是，这里不会将最顶层的 this 转换掉
        const customHandlers = {
            expression(input, scope, config) {
                var _a;
                return (0, transformJsExpression_1.transformJsExpr)(generateExpression(input, scope), scope, {
                    dontWrapEval: !((_a = config === null || config === void 0 ? void 0 : config.tolerateEvalErrors) !== null && _a !== void 0 ? _a : tolerateEvalErrors),
                    dontTransformThis2ContextAtRootScope: true,
                });
            },
            function(input, scope) {
                return (0, transformThis2Context_1.transformThis2Context)(generateCompositeType({
                    type: 'JSFunction',
                    value: input.value || 'function () {}',
                }, Scope.createRootScope()), scope, { ignoreRootScope: true });
            },
        };
        const generatorPlugins = {
            handlers: customHandlers,
            tagMapping: (v) => nodeTypeMapping[v] || v,
            tolerateEvalErrors,
        };
        const generator = createReactNodeGenerator(generatorPlugins);
        const ir = next.ir;
        const scope = Scope.createRootScope();
        const jsxContent = generator(ir, scope);
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.ScriptStart,
            content: `<script setup>`,
            linkAfter: [],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.ScriptContent,
            content: `console.log("我是脚本")`,
            linkAfter: [const_1.VUE_CHUNK_NAME.ScriptStart],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.ScriptEnd,
            content: `</script>`,
            linkAfter: [const_1.VUE_CHUNK_NAME.ScriptContent],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.TemplateStart,
            content: `<template>`,
            linkAfter: [const_1.VUE_CHUNK_NAME.ScriptEnd],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.TemplateContent,
            content: jsxContent,
            linkAfter: [const_1.VUE_CHUNK_NAME.TemplateStart],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.TemplateEnd,
            content: `</template>`,
            linkAfter: [const_1.VUE_CHUNK_NAME.TemplateContent],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.StyleStart,
            content: `<style scoped>`,
            linkAfter: [const_1.VUE_CHUNK_NAME.TemplateEnd],
        });
        next.chunks.push({
            type: lowcode_code_generator_1.ChunkType.STRING,
            fileType: cfg.fileType,
            name: const_1.VUE_CHUNK_NAME.StyleEnd,
            content: `</style>`,
            linkAfter: [const_1.VUE_CHUNK_NAME.ScriptContent],
        });
        return next;
    });
    return plugin;
};
exports.default = pluginFactory;
//# sourceMappingURL=vue.js.map