var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var JSParse_instruction_exports = {};
__export(JSParse_instruction_exports, {
  JSParseInstruction: () => JSParseInstruction
});
module.exports = __toCommonJS(JSParse_instruction_exports);
var import_crypto = __toESM(require("crypto"));
var import_lodash = __toESM(require("lodash"));
var import_qrcode = __toESM(require("qrcode"));
var import__ = require("../..");
class JSParseInstruction extends import__.Instruction {
  async run(node, input, processor) {
    const { source = "", JSCode = "", model } = node.config;
    const data = processor.getParsedValue(source, node.id);
    try {
      const ctx = {
        data,
        body: {}
      };
      await evalSimulate(JSCode, {
        ctx,
        lib: {
          JSON,
          qrcode: import_qrcode.default,
          crypto: import_crypto.default,
          log: console.log
        }
      });
      let result = ctx.body;
      if (typeof result === "object" && result && (model == null ? void 0 : model.length)) {
        if (Array.isArray(result)) {
          result = result.map((item) => mapModel(item, model));
        } else {
          result = mapModel(result, model);
        }
      }
      return {
        result,
        status: import__.JOB_STATUS.RESOLVED
      };
    } catch (err) {
      return {
        result: err.toString(),
        status: import__.JOB_STATUS.ERROR
      };
    }
  }
}
function mapModel(data, model) {
  if (typeof data !== "object" || data === null) {
    throw new Error("Invalid data: data should be a non-null object");
  }
  const result = model.reduce((acc, { path, alias }) => {
    const key = alias ?? path.replace(/\./g, "_");
    const value = import_lodash.default.get(data, path);
    acc[key] = value;
    return acc;
  }, {});
  return result;
}
async function evalSimulate(jsCode, { ctx, lib }) {
  const AsyncFunction = async function() {
  }.constructor;
  return await new AsyncFunction("$root", `with($root) { ${jsCode}; }`)({
    ctx,
    // 允许用户覆盖，这个时候可以使用 _ctx
    __ctx: ctx,
    lib
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  JSParseInstruction
});
