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 JSONParse_instruction_exports = {};
__export(JSONParse_instruction_exports, {
  JSONParseInstruction: () => JSONParseInstruction
});
module.exports = __toCommonJS(JSONParse_instruction_exports);
var import_jsonata = __toESM(require("jsonata"));
var import_lodash = __toESM(require("lodash"));
var import__ = require("../..");
class JSONParseInstruction extends import__.Instruction {
  constructor() {
    super(...arguments);
    this.engine = (expression, scope) => (0, import_jsonata.default)(expression).evaluate(scope);
  }
  async run(node, input, processor) {
    const { source = "", expression = "", model } = node.config;
    const data = processor.getParsedValue(source, node.id);
    const query = this.engine;
    try {
      let result = data;
      if (expression) {
        result = query ? await query(expression, data) : data;
      }
      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;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  JSONParseInstruction
});
