#!/usr/bin/env node
import{a as i}from"./chunk-RLNOQILG.js";import{a as u,b as c}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";var y={name:"ag-snapshot",summary:"manage filesystem snapshots for state persistence and rollbacks",usage:"ag-snapshot [create|restore|list|delete] [SNAPSHOT_ID]",options:["    --help        display this help and exit"]};var j={name:"ag-snapshot",async execute(f,s){if(c(f))return u(y);let p=f.filter(t=>!t.startsWith("-")),h=p[0]||"list",n=p[1],o=s.fs.resolvePath(s.cwd,".ag-snapshots"),d=s;async function l(){await s.fs.exists(o)||await s.fs.mkdir(o,{recursive:!0})}switch(h){case"create":{let t=n||`snap_${Date.now()}`;try{await l();let e=s.fs.resolvePath(o,`${t}.json`),a=await s.fs.snapshot?.(),r=a;a instanceof Map&&(r=Array.from(a.entries()));let m={id:t,timestamp:Date.now(),cwd:s.cwd,env:Object.assign(Object.create(null),Object.fromEntries(d.state.env)),functions:Object.assign(Object.create(null),Object.fromEntries(Array.from(d.state.functions.entries()).map(([g,w])=>[g,w]))),fs:r};return await s.fs.writeFile(e,JSON.stringify(m,null,2)),{stdout:`Snapshot '${t}' created successfully.
`,stderr:"",exitCode:0}}catch(e){return{stdout:"",stderr:`ag-snapshot: failed to create: ${i(e.message)}
`,exitCode:1}}}case"restore":{if(!n)return{stdout:"",stderr:`ag-snapshot: missing snapshot ID
`,exitCode:1};let t=s.fs.resolvePath(o,`${n}.json`);if(!await s.fs.exists(t))return{stdout:"",stderr:`ag-snapshot: snapshot '${n}' not found
`,exitCode:1};try{let e=await s.fs.readFile(t,"utf8"),a=JSON.parse(e);if(d.state.env=new Map(Object.entries(a.env)),d.state.functions=new Map(Object.entries(a.functions)),a.fs&&s.fs.restore){let r=a.fs;Array.isArray(r)&&(r=new Map(r)),await s.fs.restore(r)}return{stdout:`Restored session and environment from snapshot '${n}'.
`,stderr:"",exitCode:0}}catch(e){return{stdout:"",stderr:`ag-snapshot: failed to restore: ${i(e.message)}
`,exitCode:1}}}case"list":try{if(!await s.fs.exists(o))return{stdout:`No snapshots found.
`,stderr:"",exitCode:0};let e=(await s.fs.readdir(o)).filter(a=>a.endsWith(".json")).map(a=>a.replace(".json","")).join(`
`);return{stdout:e?`${e}
`:`No snapshots found.
`,stderr:"",exitCode:0}}catch(t){return{stdout:"",stderr:`ag-snapshot: failed to list: ${i(t.message)}
`,exitCode:1}}case"delete":{if(!n)return{stdout:"",stderr:`ag-snapshot: missing snapshot ID
`,exitCode:1};let t=s.fs.resolvePath(o,`${n}.json`);try{return await s.fs.exists(t)?(await s.fs.rm(t),{stdout:`Deleted snapshot '${n}'.
`,stderr:"",exitCode:0}):{stdout:"",stderr:`ag-snapshot: snapshot '${n}' not found
`,exitCode:1}}catch(e){return{stdout:"",stderr:`ag-snapshot: failed to delete: ${i(e.message)}
`,exitCode:1}}}default:return{stdout:"",stderr:`ag-snapshot: unknown action: ${h}
`,exitCode:1}}}};export{j as agSnapshotCommand};
