import{a as x}from"./chunk-HWKDQ44K.js";import{a as b}from"./chunk-3THT3N7L.js";var I={name:"ag-edit",async execute($,o){if($.length<2)return{stdout:"",stderr:`Usage: ag-edit <file> <action> [options]
`,exitCode:1};let c=x("ag-edit",$,{line:{short:"n",long:"line",type:"number"},to:{short:"t",long:"to",type:"number"},text:{short:"x",long:"text",type:"string"},dryRun:{long:"dry-run",type:"boolean"}});if(!c.ok)return c.error;let{flags:e,positional:h}=c.result,l=h[0],d=h[1];if(!l||!d)return{stdout:"",stderr:`usage: ag-edit <action> <file> [-n line] [-t to] [-x text] [--dry-run]
`,exitCode:1};let u=o.fs.resolvePath(o.cwd,d);if(!await o.fs.exists(u))return{stdout:"",stderr:`ag-edit: ${d}: No such file or directory
`,exitCode:1};let i=(await o.fs.readFile(u,"utf8")).split(/\r?\n/),f=i.length,n=(e.text||"").replace(/\\n/g,`
`).split(`
`),t=e.line!==void 0?e.line-1:void 0,r=e.to!==void 0?e.to-1:t;if(["insert-before","insert-after","replace","delete"].includes(l)){if(t===void 0||Number.isNaN(t))return a("missing or invalid --line");if(t<0||l!=="insert-after"&&t>=f)return a(`line ${e.line} out of range (1-${f})`)}let s="";switch(l){case"insert-before":i.splice(t,0,...n),s=`Inserted ${n.length} line(s) before line ${e.line}`;break;case"insert-after":i.splice(t+1,0,...n),s=`Inserted ${n.length} line(s) after line ${e.line}`;break;case"replace":if(r===void 0||Number.isNaN(r)||r<t)return a("invalid range for replace");if(r>=f)return a(`range end ${e.to} out of range`);i.splice(t,r-t+1,...n),s=`Replaced lines ${e.line}-${e.to||e.line} with ${n.length} line(s)`;break;case"delete":if(r===void 0||Number.isNaN(r)||r<t)return a("invalid range for delete");if(r>=f)return a(`range end ${e.to} out of range`);i.splice(t,r-t+1),s=`Deleted lines ${e.line}-${e.to||e.line}`;break;case"append":i.push(...n),s=`Appended ${n.length} line(s)`;break;case"prepend":i.unshift(...n),s=`Prepended ${n.length} line(s)`;break;default:return{stdout:"",stderr:`ag-edit: unknown action: ${l}
`,exitCode:1}}let g=i.join(`
`);if(e.dryRun)return{stdout:`[DRY RUN] ${s} in ${d}
Proposed content length: ${g.length} bytes
`,stderr:"",exitCode:0};try{return await o.fs.writeFile(u,g),o.bash?.lsp&&o.bash.lsp.notifyDidChange(u,g),{stdout:`Successfully updated ${d}: ${s}
`,stderr:"",exitCode:0}}catch(p){return{stdout:"",stderr:`ag-edit: failed to write ${d}: ${b(p.message)}
`,exitCode:1}}function a(p){return{stdout:"",stderr:`ag-edit: ${p}
`,exitCode:1}}}};export{I as a};
