// THIS FILE WAS AUTO-GENERATED
// Source: lib/templates/cli/evaluate.mustache
// Any manual changes will be lost.
import { apply } from "typestache";
export const template = `import { {{{nodeName:string}}}, hasInterrupts, approve, reject, respondToInterrupts } from "./{{{filename:string}}}";
import { writeFileSync } from "fs";

export async function runEvaluation() {
  {{#hasArgs}}
  let result = await {{{nodeName:string}}}({{{args?:string}}});
  {{/hasArgs}}
  {{^hasArgs}}
  let result = await {{{nodeName:string}}}();
  {{/hasArgs}}

  {{#hasInterruptHandlers}}
  const interruptHandlers = {{{interruptHandlersJSON?:string}}};
  let handlerIndex = 0;

  while (hasInterrupts(result.data)) {
    const interrupts = result.data;
    const responses = [];

    for (const interruptItem of interrupts) {
      if (handlerIndex >= interruptHandlers.length) {
        throw new Error("Unexpected interrupt #" + (handlerIndex + 1) + ": \\"" + interruptItem.message + "\\". No handler provided.");
      }

      const handler = interruptHandlers[handlerIndex];

      // Validate expected message if provided
      if (handler.expectedMessage !== undefined && interruptItem.message !== handler.expectedMessage) {
        throw new Error(
          "Interrupt #" + (handlerIndex + 1) + " message mismatch.\\n" +
          "  Expected: \\"" + handler.expectedMessage + "\\"\\n" +
          "  Actual: \\"" + interruptItem.message + "\\""
        );
      }

      // Build response based on action
      if (handler.action === "approve") {
        responses.push(approve(handler.value));
      } else if (handler.action === "reject") {
        responses.push(reject(handler.value));
      } else if (handler.action === "resolve") {
        responses.push(approve(handler.resolvedValue));
      } else {
        throw new Error("Unknown interrupt action: " + handler.action);
      }

      handlerIndex++;
    }

    result = await respondToInterrupts(interrupts, responses);
  }

  // Check if we provided more handlers than interrupts that occurred
  if (handlerIndex < interruptHandlers.length) {
    throw new Error(
      "Expected " + interruptHandlers.length + " interrupts but only " + handlerIndex + " occurred."
    );
  }
  {{/hasInterruptHandlers}}

  writeFileSync("{{{resultsFilename:string}}}", JSON.stringify(result, null, 2));
  return result;
}

runEvaluation();
`;
const render = (args) => {
    return apply(template, args);
};
export default render;
