// THIS FILE WAS AUTO-GENERATED
// Source: lib/templates/backends/typescriptGenerator/interruptReturn.mustache
// Any manual changes will be lost.
import { apply } from "typestache";
export const template = `// Resume path: check for a response by interruptId
const __response = getRuntimeContext().ctx.getInterruptResponse(__self.{{{interruptIdKey:string}}});
if (__response) {
  if (__response.type === "approve") {
    // approved, continue execution
  } else if (__response.type === "reject") {
    // rejected, halt
    {{#nodeContext}}
    runner.halt({ messages: __threads(), data: failure("interrupt rejected", { retryable: false }) });
    {{/nodeContext}}
    {{^nodeContext}}
    runner.halt(failure("interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.getResultCheckpoint() }));
    {{/nodeContext}}
    return;
  }
} else {
  // First run: call handlers, then propagate if unhandled
  const __handlerResult = await interruptWithHandlers({{{kind}}}, {{{message}}}, {{{data}}}, {{{origin}}}, __ctx, __stateStack());
  if (isRejected(__handlerResult)) {
    {{#nodeContext}}
    runner.halt({ messages: __threads(), data: failure(__handlerResult.value ?? "interrupt rejected", { retryable: false }) });
    {{/nodeContext}}
    {{^nodeContext}}
    runner.halt(failure(__handlerResult.value ?? "interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.checkpoints.get(__resultCheckpointId) }));
    {{/nodeContext}}
    return;
  }
  if (!isApproved(__handlerResult)) {
    // No handler — propagate interrupt array to TypeScript caller
    // Store interruptId on frame BEFORE checkpoint so it's captured in the snapshot
    __self.{{{interruptIdKey:string}}} = __handlerResult[0].interruptId;
    const __checkpointId = getRuntimeContext().ctx.checkpoints.create(__stateStack(), __ctx, { moduleId: {{{moduleId}}}, scopeName: {{{scopeName}}}, stepPath: {{{stepPath}}} });
    __handlerResult[0].checkpointId = __checkpointId;
    __handlerResult[0].checkpoint = getRuntimeContext().ctx.checkpoints.get(__checkpointId);
    {{#nodeContext}}
    runner.halt({ messages: __threads(), data: __handlerResult });
    {{/nodeContext}}
    {{^nodeContext}}
    runner.halt(__handlerResult);
    {{/nodeContext}}
    return;
  }
  // Approved — continue execution past interrupt
}
`;
const render = (args) => {
    return apply(template, args);
};
export default render;
