"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.invokeJavaScript = exports.compileJavaScript = exports.compileAsyncJs = exports.compileSyncJs = exports.evalExpression = void 0;
const jsonpointer_1 = __importDefault(require("jsonpointer"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const timers_1 = require("timers");
const vm_1 = __importDefault(require("vm"));
const util = __importStar(require("../util/index.js"));
const data_js_1 = require("./data.js");
const globals = ['module', 'exports', 'require', 'global', 'process'];
function evalExpression(expression, scope, additionalArgs = {}) {
    switch (expression[0]) {
        case '/':
            if (expression.startsWith('//')) {
                return undefined;
            }
            (0, data_js_1.checkType)(scope, ['object', 'array']);
            return jsonpointer_1.default.get(scope, expression);
        case '=': {
            const _this = util.deepClone(scope);
            const entries = Object.entries(_this)
                .filter(ent => /^[a-z][a-z0-9_]*$/i.test(ent[0]))
                .concat(Object.entries(additionalArgs));
            const keys = entries.map(_ => _[0]);
            const values = entries.map(_ => _[1]);
            const js = compileSyncJs(`return ${expression.substring(1)}`, ...keys);
            const result = js.apply(_this, values);
            return result;
        }
        default:
            return String(expression);
    }
}
exports.evalExpression = evalExpression;
function compileSyncJs(expr, ...args) {
    return new Function(...args, `return (() => { ${expr} })(${args.join(',')})`);
}
exports.compileSyncJs = compileSyncJs;
function compileAsyncJs(expr, ...args) {
    return new Function(...args, `return (async (${globals.join(',')}) => { ${expr} })(${args.join(',')})`);
}
exports.compileAsyncJs = compileAsyncJs;
function compileJavaScript(code, timeout = 60000) {
    return new vm_1.default.Script(`$promise = (async (${globals.join(',')}) => { ${code} })()`, { timeout });
}
exports.compileJavaScript = compileJavaScript;
function invokeJavaScript(script, args) {
    const sandbox = {
        $promise: null,
        setTimeout: timers_1.setTimeout,
        clearTimeout: timers_1.clearTimeout,
        setInterval: timers_1.setInterval,
        clearInterval: timers_1.clearInterval,
        setImmediate: timers_1.setImmediate,
        clearImmediate: timers_1.clearImmediate,
        URL,
        URLSearchParams,
        Buffer,
        fetch: node_fetch_1.default,
        console,
        ...args,
    };
    script.runInNewContext(sandbox);
    return sandbox.$promise;
}
exports.invokeJavaScript = invokeJavaScript;
//# sourceMappingURL=expression.js.map