import {
  isRuntimeRef
} from "./chunk-4ETFMWQR.mjs";

// src/core/lib/flowCompiler/resolveRefs.ts
function resolveRuntimeRefs(nb, getNodeOutput, triggerContext) {
  return resolveValue(nb, getNodeOutput, triggerContext);
}
function resolveValue(value, getNodeOutput, triggerContext) {
  if (isRuntimeRef(value)) {
    return resolveRef(value.$ref, getNodeOutput, triggerContext);
  }
  if (Array.isArray(value)) {
    return value.map((item) => resolveValue(item, getNodeOutput, triggerContext));
  }
  if (typeof value === "object" && value !== null) {
    const result = {};
    for (const [key, val] of Object.entries(value)) {
      result[key] = resolveValue(val, getNodeOutput, triggerContext);
    }
    return result;
  }
  return value;
}
function resolveRef(ref, getNodeOutput, triggerContext) {
  if (ref.startsWith("trigger.payload.")) {
    if (!triggerContext) {
      throw new Error(`Trigger ref "${ref}" used outside of a listener invocation context. trigger.payload.* refs are only valid on block.event-triggered blocks.`);
    }
    const fieldPath2 = ref.slice("trigger.payload.".length);
    return getNestedValue(triggerContext.payload, fieldPath2);
  }
  if (triggerContext && Object.prototype.hasOwnProperty.call(triggerContext.refSnapshots, ref)) {
    return triggerContext.refSnapshots[ref];
  }
  const outputIndex = ref.indexOf(".output.");
  if (outputIndex === -1) {
    throw new Error(`Invalid runtime reference "${ref}". Expected format: "nodeId.output.fieldPath" or "trigger.payload.fieldPath"`);
  }
  const nodeId = ref.slice(0, outputIndex);
  const fieldPath = ref.slice(outputIndex + ".output.".length);
  const output = getNodeOutput(nodeId);
  if (!output) {
    return void 0;
  }
  return getNestedValue(output, fieldPath);
}
function getNestedValue(obj, path) {
  const parts = path.split(".");
  let current = obj;
  for (const part of parts) {
    if (current == null || typeof current !== "object") {
      return void 0;
    }
    current = current[part];
  }
  return current;
}

export {
  resolveRuntimeRefs
};
//# sourceMappingURL=chunk-VE3WI2J2.mjs.map