import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,s as n}from"./envUtils-BWeoiL4Y.js";import{_ as r,fj as i,g as a,h as o,pj as s}from"./loadAgentsDir-C_wFhytF.js";import{basename as c,join as l}from"path";import{appendFileSync as u,mkdirSync as d,readFileSync as f,readdirSync as p,writeFileSync as m}from"fs";import{randomUUID as h}from"crypto";function g(){let e=a(`templates`,process.cwd()),n=l(t(),`templates`);try{return p(n),[...e,n]}catch{return e}}function _(){let e=[],t=new Set;for(let n of g()){let r;try{r=p(n)}catch{continue}for(let i of r){if(!i.endsWith(`.md`))continue;let r=c(i,`.md`);if(t.has(r))continue;t.add(r);let a=l(n,i);try{let{frontmatter:t,content:n}=s(f(a,`utf-8`),a),i=(typeof t.description==`string`?t.description:``)||o(n,`No description`);e.push({name:r,description:i,filePath:a,frontmatter:t,content:n})}catch{}}}return e}function v(e){return _().find(t=>t.name===e)??null}var y=e((()=>{i(),n(),r()}));function b(){return l(t(),`jobs`)}function x(e){return l(b(),e)}function S(e,t,n,r,i){let a=x(e);d(a,{recursive:!0});let o=new Date().toISOString(),s={jobId:e,templateName:t,createdAt:o,updatedAt:o,status:`created`,args:i};return m(l(a,`state.json`),JSON.stringify(s,null,2),`utf-8`),m(l(a,`template.md`),n,`utf-8`),m(l(a,`input.txt`),r,`utf-8`),a}function C(e){try{let t=f(l(x(e),`state.json`),`utf-8`),n=JSON.parse(t);if(typeof n!=`object`||!n)return null;let r=n;return typeof r.jobId!=`string`||typeof r.status!=`string`?null:r}catch{return null}}function w(e,t){let n=x(e),r=C(e);if(!r)return!1;let i=l(n,`replies.jsonl`),a=JSON.stringify({text:t,timestamp:new Date().toISOString()});try{u(i,a+`
`,`utf-8`)}catch{m(i,a+`
`,`utf-8`)}let o={...r,updatedAt:new Date().toISOString()};return m(l(n,`state.json`),JSON.stringify(o,null,2),`utf-8`),!0}var T=e((()=>{n()}));async function E(e){let t=e[0];switch(t){case`list`:k();break;case`new`:A(e.slice(1));break;case`reply`:j(e.slice(1));break;case`status`:O(e.slice(1));break;default:console.error(`Unknown template command: ${t}`),D(),process.exitCode=1}}function D(){console.log(`
Template Job Commands:

  claude job list                    List available templates
  claude job new <template> [args]   Create a new job from a template
  claude job reply <job-id> <text>   Reply to an existing job
  claude job status <job-id>         Show job status
`)}function O(e){let t=e[0];if(!t){console.error(`Usage: claude job status <job-id>`),process.exitCode=1;return}let n=C(t);if(!n){console.error(`Job not found: ${t}`),process.exitCode=1;return}console.log(`Job: ${n.jobId}`),console.log(`  Template: ${n.templateName}`),console.log(`  Status: ${n.status}`),console.log(`  Created: ${n.createdAt}`),console.log(`  Updated: ${n.updatedAt}`),console.log(`  Args: ${n.args.join(` `)||`(none)`}`)}function k(){let e=_();if(e.length===0){console.log(`No templates found.`),console.log(`Place .md files in .claude/templates/ or ~/.claude/templates/`);return}console.log(`${e.length} template${e.length>1?`s`:``} found:\n`);for(let t of e)console.log(`  ${t.name}`),console.log(`    ${t.description}`),console.log(`    Path: ${t.filePath}`),console.log()}function A(e){let t=e[0];if(!t){console.error(`Usage: claude job new <template> [args...]`),process.exitCode=1;return}let n=v(t);if(!n){console.error(`Template not found: ${t}`),console.log(`
Available templates:`);for(let e of _())console.log(`  ${e.name}`);process.exitCode=1;return}let r=h().slice(0,8),i=e.slice(1).join(` `),a=S(r,t,`---\n${Object.entries(n.frontmatter).map(([e,t])=>`${e}: ${t}`).join(`
`)}\n---\n${n.content}`,i,e.slice(1));console.log(`Job created: ${r}`),console.log(`  Template: ${t}`),console.log(`  Directory: ${a}`),i&&console.log(`  Input: ${i}`)}function j(e){let t=e[0],n=e.slice(1).join(` `);if(!t||!n){console.error(`Usage: claude job reply <job-id> <text>`),process.exitCode=1;return}if(!C(t)){console.error(`Job not found: ${t}`),process.exitCode=1;return}w(t,n)?(console.log(`Reply added to job ${t}`),console.log(`  Directory: ${x(t)}`)):(console.error(`Failed to append reply to job ${t}`),process.exitCode=1)}e((()=>{y(),T()}))();export{E as templatesMain};