var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { WorkflowScriptTimeoutInMS } from "../../../../Server/EnvironmentConfig";
import VMUtil from "../../../Utils/VM/VMAPI";
import ComponentCode from "../ComponentCode";
import BadDataException from "../../../../Types/Exception/BadDataException";
import ComponentID from "../../../../Types/Workflow/ComponentID";
import JavaScriptComponents from "../../../../Types/Workflow/Components/JavaScript";
import CaptureSpan from "../../../Utils/Telemetry/CaptureSpan";
export default class JavaScriptCode extends ComponentCode {
    constructor() {
        super();
        const JavaScriptComponent = JavaScriptComponents.find((i) => {
            return i.id === ComponentID.JavaScriptCode;
        });
        if (!JavaScriptComponent) {
            throw new BadDataException("Custom JavaScript Component not found.");
        }
        this.setMetadata(JavaScriptComponent);
    }
    async run(args, options) {
        const successPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "success";
        });
        if (!successPort) {
            throw options.onError(new BadDataException("Success port not found"));
        }
        const errorPort = this.getMetadata().outPorts.find((p) => {
            return p.id === "error";
        });
        if (!errorPort) {
            throw options.onError(new BadDataException("Error port not found"));
        }
        try {
            /*
             * Set timeout
             * Inject args
             * Inject dependencies
             */
            let scriptArgs = args["arguments"] || {};
            if (typeof scriptArgs === "string") {
                scriptArgs = JSON.parse(scriptArgs);
            }
            const code = args["code"] || "";
            const returnResult = await VMUtil.runCodeInSandbox({
                code,
                options: {
                    args: scriptArgs,
                    timeout: WorkflowScriptTimeoutInMS,
                },
            });
            const logMessages = returnResult.logMessages;
            // add to option.log
            logMessages.forEach((msg) => {
                options.log(msg);
            });
            const returnVal = returnResult.returnValue;
            return {
                returnValues: {
                    returnValue: returnVal,
                },
                executePort: successPort,
            };
        }
        catch (err) {
            options.log("Error running script");
            options.log(err.message ? err.message : JSON.stringify(err, null, 2));
            return {
                returnValues: {},
                executePort: errorPort,
            };
        }
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], JavaScriptCode.prototype, "run", null);
//# sourceMappingURL=JavaScript.js.map