"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionHandler = exports.FunctionDefinition = void 0;
const error_1 = require("../../error");
const magic_function_1 = require("../magic.function");
const modules_1 = require("../modules");
const utils_1 = require("../utils");
/**
 * 动态异步方法
 * @param script
 * @param argsName
 * @return {*}
 * @constructor
 */
const FunctionDefinition = (script, ...argsName) => {
    return (0, utils_1.ScriptFunction)(script, [
        'ctx',
        'importModule',
        'importFunction',
        'BizError',
        'BizErrorEnum',
        'db',
        'cache',
        'logger',
        ...argsName,
    ]);
};
exports.FunctionDefinition = FunctionDefinition;
/**
 * 函数处理器
 * @param ctx 上下文
 * @param functionDefinition 执行脚本
 * @param args 参数值
 */
const FunctionHandler = async (ctx, functionDefinition, args) => {
    const requestContext = ctx.requestContext;
    const db = await requestContext.getAsync(modules_1.SQLModule);
    const cache = await requestContext.getAsync(modules_1.CacheModule);
    const magicFunction = await requestContext.getAsync(magic_function_1.MagicFunction);
    const logger = ctx.getLogger('magic');
    const importModule = async (moduleName) => {
        return await requestContext.getAsync(moduleName);
    };
    try {
        //执行script
        return await (0, utils_1.executeScriptFunction)(functionDefinition, ctx, importModule, magicFunction.getFunction.bind(magicFunction), error_1.BizError, error_1.BizErrorEnum, db, cache, logger, ...args);
    }
    finally {
        await db.release();
    }
};
exports.FunctionHandler = FunctionHandler;
