"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformJsExpr = void 0;
const tslib_1 = require("tslib");
const lowcode_code_generator_1 = tslib_1.__importDefault(require("@alilc/lowcode-code-generator"));
const isLiteralAtomicExpr_1 = require("../util/isLiteralAtomicExpr");
const isSimpleStraightLiteral_1 = require("../util/isSimpleStraightLiteral");
const transformThis2Context_1 = require("./transformThis2Context");
const { parseExpression } = lowcode_code_generator_1.default.utils.expressionParser;
function transformJsExpr(expr, scope, { dontWrapEval = false, dontTransformThis2ContextAtRootScope = false } = {}) {
    if (!expr) {
        return 'undefined';
    }
    if ((0, isLiteralAtomicExpr_1.isLiteralAtomicExpr)(expr)) {
        return expr;
    }
    const exprAst = parseExpression(expr);
    // 对于下面这些比较安全的字面值，可以直接返回对应的表达式，而非包一层
    if ((0, isSimpleStraightLiteral_1.isSimpleStraightLiteral)(exprAst)) {
        return expr;
    }
    if (dontWrapEval) {
        return (0, transformThis2Context_1.transformThis2Context)(exprAst, scope, {
            ignoreRootScope: dontTransformThis2ContextAtRootScope,
        });
    }
    switch (exprAst.type) {
        // 对于直接写个函数的，则不用再包下，因为这样不会抛出异常的
        case 'ArrowFunctionExpression':
        case 'FunctionExpression':
            return (0, transformThis2Context_1.transformThis2Context)(exprAst, scope, {
                ignoreRootScope: dontTransformThis2ContextAtRootScope,
            });
        default:
            break;
    }
    // 其他的都需要包一层
    return `__$$eval(() => (${(0, transformThis2Context_1.transformThis2Context)(exprAst, scope, {
        ignoreRootScope: dontTransformThis2ContextAtRootScope,
    })}))`;
}
exports.transformJsExpr = transformJsExpr;
//# sourceMappingURL=transformJsExpression.js.map