import{WizardCancelledError}from"./step.js";function isCancellation(t){return t instanceof WizardCancelledError}var RetryableSetupError=class extends Error{constructor(e,t){super(e,t),this.name=`RetryableSetupError`}};function isRetryable(e){return e instanceof RetryableSetupError}async function runInteractive(e,t,n,r){let i=t,view=()=>r?.snapshot?r.snapshot(i):i,a={};try{for(let t of e)if(t.shouldRun?.(view())!==!1)for(;;){let e=await t.gather({state:view(),initial:a[t.id]});a[t.id]=e;try{let r=await t.perform({state:view(),input:e,sink:n});i=t.apply(i,r);break}catch(e){if(!isRetryable(e))throw e;n.write(e instanceof Error?e.message:String(e))}}}catch(e){if(isCancellation(e))return{kind:`cancelled`};throw e}return{kind:`done`,state:i}}async function runHeadless(e,t,n,r){let i=t,view=()=>r?.snapshot?r.snapshot(i):i;for(let t of e){if(t.shouldRun?.(view())===!1)continue;let e=await t.gather({state:view()}),r=await t.perform({state:view(),input:e,sink:n});i=t.apply(i,r)}return i}export{RetryableSetupError,runHeadless,runInteractive};