#!/usr/bin/env node
import{a as g}from"./chunk-RLNOQILG.js";import{a as f}from"./chunk-GTNBSMZR.js";var x={name:"ag-explain",summary:"parse and explain a shell command",usage:"ag-explain [--json] <command_string>",options:["    --json        output raw AST in JSON format","    --help        display this help and exit"]},E={name:"ag-explain",async execute(n,$){if(n.includes("--help")||n.includes("-h"))return f(x);let c=!1,l=[];for(let r of n)r==="--json"?c=!0:l.push(r);let m=l.join(" ");if(!m)return{stdout:"",stderr:`ag-explain: missing command string
`,exitCode:2};try{let{parse:r}=await import("./parser-CVWY2HS3.js"),o=r(m);if(c)return{stdout:JSON.stringify(o,(e,a)=>e==="parent"?void 0:a,2)+`
`,stderr:"",exitCode:0};let p=`--- Explanation for: ${m} ---
`,i=(e,a="")=>{let s="";if(!e)return"";try{switch(e.type){case"Script":e.statements&&e.statements.forEach(t=>s+=i(t,a));break;case"Statement":e.pipelines&&e.pipelines.forEach(t=>s+=i(t,a));break;case"Pipeline":e.commands&&e.commands.length>1?(s+=`${a}Pipeline with ${e.commands.length} stages:
`,e.commands.forEach((t,u)=>{s+=`${a}  [Stage ${u+1}]:
`,s+=i(t,`${a}    `)})):e.commands&&e.commands.length===1&&(s+=i(e.commands[0],a));break;case"SimpleCommand":s+=`${a}Execute command: '${e.name?.text||""}'
`,e.args&&e.args.length>0&&(s+=`${a}  Arguments: ${e.args.map(t=>`'${t.text}'`).join(", ")}
`),e.assignments&&e.assignments.length>0&&(s+=`${a}  Environment variables: ${e.assignments.map(t=>`${t.name}=${t.value?.text||""}`).join(", ")}
`),e.redirects&&e.redirects.length>0&&(s+=`${a}  Redirections:
`,e.redirects.forEach(t=>{s+=`${a}    - fd ${t.fd||(t.type.startsWith(">")?1:0)} ${t.type} ${t.file?.text||"unknown"}
`}));break;default:s+=`${a}Node type: ${e.type}
`}}catch(t){s+=`${a}[Error explaining ${e.type}: ${t.message}]
`}return s};return p+=i(o),p+=`--------------------------------------
`,{stdout:p,stderr:"",exitCode:0}}catch(r){return{stdout:"",stderr:`ag-explain: parse error: ${g(r.message)}
`,exitCode:1}}}};export{E as a};
