var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var PdfInstruction_exports = {};
__export(PdfInstruction_exports, {
  PdfInstruction: () => PdfInstruction
});
module.exports = __toCommonJS(PdfInstruction_exports);
var import_node_vm = require("node:vm");
var import_module_workflow = require("@tachybase/module-workflow");
var import_core = require("@babel/core");
class PdfInstruction extends import_module_workflow.Instruction {
  async run(node, input, processor) {
    const { sourceArray, type, code = "", model } = node.config;
    let data = {};
    switch ((sourceArray || []).length) {
      case 0: {
        data = {};
        break;
      }
      case 1: {
        const keyName = sourceArray[0]["keyName"];
        const sourcePath = sourceArray[0]["sourcePath"];
        const rawData = processor.getParsedValue(sourcePath, node.id);
        if (keyName) {
          data = {
            [keyName]: rawData
          };
        } else {
          data = rawData;
        }
        break;
      }
      default: {
        data = sourceArray.reduce(
          (cookedData, { keyName, sourcePath }) => ({
            ...cookedData,
            [keyName]: processor.getParsedValue(sourcePath, node.id)
          }),
          {}
        );
      }
    }
    try {
      const { httpContext } = processor.options;
      const myresult = (0, import_core.transform)(code, {
        sourceType: "module",
        filename: "a.tsx",
        presets: [
          [
            require("@babel/preset-env"),
            {
              modules: "commonjs",
              targets: {
                node: "current"
              }
            }
          ],
          require("@babel/preset-react"),
          require("@babel/preset-typescript")
        ]
      }).code;
      const compiledCode = myresult;
      const script = new import_node_vm.Script(compiledCode);
      const defaultFunction = async (event, context) => {
      };
      const sandbox = {
        module: {},
        exports: { default: defaultFunction },
        require,
        console
      };
      (0, import_node_vm.createContext)(sandbox);
      script.runInContext(sandbox);
      const func = sandbox.exports.default;
      const result = await func(data, {
        httpContext
      });
      return {
        result,
        status: import_module_workflow.JOB_STATUS.RESOLVED
      };
    } catch (err) {
      return {
        result: err.toString(),
        status: import_module_workflow.JOB_STATUS.ERROR
      };
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PdfInstruction
});
