export function evaluatePath(data: unknown, path: string): unknown {
  if (!path || !path.startsWith("$out")) return data;
  const expr = path.slice(4); // strip "$out"
  const tokens: string[] = [];
  const regex = /\.([a-zA-Z_][a-zA-Z0-9_]*)|\[(\d+)\]/g;
  let match: RegExpExecArray | null;
  while ((match = regex.exec(expr)) !== null) {
    tokens.push(match[1] ?? match[2]);
  }
  let current: unknown = data;
  for (const token of tokens) {
    if (current == null) return null;
    const idx = parseInt(token, 10);
    if (!isNaN(idx) && Array.isArray(current)) {
      current = current[idx];
    } else {
      current = (current as Record<string, unknown>)[token];
    }
  }
  return current;
}
