import{renderCliTaggedLine}from"#cli/ui/output.js";import{resolveTextToResponse}from"#channel/resolve-text.js";const ESCAPED_RUNTIME_INPUT_PROMPT=Symbol(`eve.cli.runtime-input.escaped`);function extractPendingRuntimeInputRequests(e){for(let t=e.length-1;t>=0;--t){let n=e[t];if(n?.type===`input.requested`)return n.data.requests}return[]}async function promptForRuntimeInputRequests(t){let n=[];t.print(renderCliTaggedLine(t.theme,{message:`pending input request${t.requests.length===1?``:`s`}: reply now, press Escape, or type "skip" to return to the normal prompt`,tag:`input`,tone:`info`}));for(let e of t.requests){let r=await promptForRuntimeInputRequest({ask:t.ask,print:t.print,request:e,theme:t.theme});if(r.kind!==`submitted`)return r;n.push(r.inputResponse)}return{inputResponses:n,kind:`submitted`}}function isApprovalRequest(e){return e.display===`confirmation`}async function promptForRuntimeInputRequest(e){return isApprovalRequest(e.request)?await promptForApprovalInputRequest({ask:e.ask,print:e.print,request:e.request,theme:e.theme}):await promptForQuestionInputRequest({ask:e.ask,print:e.print,request:e.request,theme:e.theme})}async function promptForApprovalInputRequest(t){for(t.print(renderCliTaggedLine(t.theme,{message:`approve tool "${t.request.action.toolName}"? (yes/no)`,tag:`approval`,tone:`warning`})),t.print(renderCliTaggedLine(t.theme,{message:JSON.stringify(t.request.action.input,null,2),tag:`tool`,tone:`muted`}));;){let r=await t.ask(`approval> `);if(r===void 0)return{kind:`aborted`};if(r===ESCAPED_RUNTIME_INPUT_PROMPT)return{kind:`deferred`};let i=r.trim().toLowerCase();if(i===`skip`||i===`/skip`)return{kind:`deferred`};if(i===`a`||i===`approve`||i===`y`||i===`yes`)return{inputResponse:{optionId:`approve`,requestId:t.request.requestId},kind:`submitted`};if(i===`d`||i===`deny`||i===`n`||i===`no`)return{inputResponse:{optionId:`deny`,requestId:t.request.requestId},kind:`submitted`};t.print(renderCliTaggedLine(t.theme,{message:`enter "yes" to approve, "no" to deny, press Escape, or type "skip" to return to the normal prompt`,tag:`input`,tone:`warning`}))}}async function promptForQuestionInputRequest(r){r.print(renderCliTaggedLine(r.theme,{message:r.request.prompt,tag:`question`,tone:`info`}));for(let[t,n]of(r.request.options??[]).entries())r.print(renderCliTaggedLine(r.theme,{message:`${t+1}. ${n.label}${n.description===void 0?``:` - ${n.description}`}`,tag:`option`,tone:`muted`}));let i=r.request.allowFreeform===!0||(r.request.options?.length??0)===0;for(;;){let a=await r.ask(`answer> `);if(a===void 0)return{kind:`aborted`};if(a===ESCAPED_RUNTIME_INPUT_PROMPT)return{kind:`deferred`};let o=a.trim();if(o===`skip`||o===`/skip`)return{kind:`deferred`};let s=resolveTextToResponse(o,r.request);if(s!==void 0)return{inputResponse:s,kind:`submitted`};r.print(renderCliTaggedLine(r.theme,{message:r.request.options===void 0||r.request.options.length===0?`enter a response, press Escape, or type "skip" to return to the normal prompt`:i?`enter an option number/id, freeform text, press Escape, or type "skip" to return to the normal prompt`:`enter an option number/id, press Escape, or type "skip" to return to the normal prompt`,tag:`input`,tone:`warning`}))}}export{ESCAPED_RUNTIME_INPUT_PROMPT,extractPendingRuntimeInputRequests,promptForRuntimeInputRequests};