import{o as e}from"./errors-pXKbCKbL.js";import{r as t}from"./runner-DYk4q6mP.js";import n from"node:path";import{existsSync as r}from"node:fs";import i from"node:readline";function a(e){return n.basename(e)}function o(e,t){let r=a(e),i=t.replace(/\//g,`-`);return n.join(e,`..`,`${r}-${i}`)}function s(e,t){t.path&&e.push({path:t.path,branch:t.branch,head:t.head})}function c(e){let t=[],n=e.trim().split(`
`),r={};for(let e of n)e.startsWith(`worktree `)?(s(t,r),r={path:e.substring(9)}):e.startsWith(`HEAD `)?r.head=e.substring(5):e.startsWith(`branch `)?r.branch=e.substring(7).replace(`refs/heads/`,``):e===``&&(s(t,r),r={});return s(t,r),t}async function l(n){let r=await t(`git`,[`worktree`,`list`,`--porcelain`],{cwd:n,throwOnError:!1});if(!r.success)throw new e(`git worktree list`,r.stderr);return c(r.stdout)}async function u(n){let r=await t(`git`,[`branch`,`--format=%(refname:short)`,`--sort=-committerdate`],{cwd:n,throwOnError:!1});if(!r.success)throw new e(`git branch --format=%(refname:short)`,r.stderr);return r.stdout.split(`
`).map(e=>e.trim()).filter(Boolean)}async function d(e,n){return(await t(`git`,[`rev-parse`,`--verify`,`--quiet`,n],{cwd:e,throwOnError:!1,silent:!0})).success}async function f(e,t,n=0){if(t.length===0)throw Error(`No options available for selection.`);let r=process.stdin,a=process.stdout;if(!r.isTTY||!a.isTTY)throw Error(`Interactive selection requires a TTY. Remove --select to continue.`);i.emitKeypressEvents(r),r.resume();let o=!!r.isRaw;r.setRawMode&&r.setRawMode(!0);let s=Math.min(Math.max(n,0),t.length-1),c=0,l=()=>{a.moveCursor(0,-c),a.clearScreenDown();let n=[e,`Use ↑/↓ then Enter to confirm. Press q or Ctrl+C to cancel.`,...t.map((e,t)=>{let n=`${t===s?`➜`:` `} ${e.label}`;return t===s?`\x1B[36m${n}\x1B[0m`:n})];a.write(n.join(`
`)+`
`),c=n.length};return await new Promise((e,n)=>{let i=(t,i)=>{r.off(`keypress`,c),r.setRawMode&&r.setRawMode(o),r.pause(),a.write(`\x1B[?25h
`),t?n(t):e(i)},c=(e,n)=>{if(n){if(n.name===`up`){s=(s-1+t.length)%t.length,l();return}if(n.name===`down`){s=(s+1)%t.length,l();return}if(n.name===`return`){i(void 0,t[s].value);return}(n.name===`c`&&n.ctrl||n.name===`q`)&&i(Error(`Selection cancelled.`))}};r.on(`keypress`,c),a.write(`\x1B[?25l`),l()})}function p(e,t){let r=t.find(t=>t.branch===e);if(r)return r.path;let i=t.find(t=>{let r=n.basename(t.path);return r===e||r.endsWith(`-${e}`)});if(i)return i.path;let a=t.find(t=>t.path.includes(e));return a?a.path:null}async function m(n,i){let a=i||process.cwd(),{action:s,path:c,branch:m,base:h,force:g,detach:_,interactive:v,create:y}=n;switch(s){case`list`:{let e=await l(a);return{success:!0,message:`Available worktrees:`,details:{output:e.length===0?`  (no worktrees found)`:e.map((e,t)=>`  [${t+1}] ${e.path}\n      Branch: ${e.branch||`detached`}`).join(`

`),worktrees:e}}}case`switch`:{let e=await l(a);if(e.length===0)throw Error(`No worktrees found`);if(v){let t=m&&p(m,e)||null,n=t===null?0:Math.max(0,e.findIndex(e=>e.path===t)),r=await f(`Select worktree to switch to:`,e.map(e=>({label:`${e.branch||`detached`} — ${e.path}`,value:e})),n);return{success:!0,message:r.path,details:{path:r.path,branch:r.branch}}}let t=m||`main`,n=p(t,e);if(!n)throw Error(`Could not find worktree matching "${t}". Use --list to see available worktrees.`);return{success:!0,message:n,details:{path:n,branch:t}}}case`add`:{let n=m||`main`,i=h||`main`,s=y!==!1,p=await l(a),b=await d(a,n),x=await d(a,i);if(v){let e=await u(a);if(e.length===0)throw Error(`No local branches found. Create one or fetch remote branches before adding a worktree.`);let t=e.map(e=>({label:p.some(t=>t.branch===e)?`${e} (already has a worktree)`:e,value:e}));n=await f(`Select branch to create a worktree for:`,t,Math.max(0,t.findIndex(e=>e.value===n)))}let S=p.find(e=>e.branch===n);if(S&&!_&&b)throw Error(`Branch "${n}" is already used by worktree at "${S.path}".\nTry --detach to create a detached worktree, or use -b <new-branch> (with --base main) to create a new branch.`);let C=c||o(a,n);if(r(C)&&!g)throw Error(`Path "${C}" already exists. Use --force to override or specify a different path with --path`);let w=[`worktree`,`add`];g&&w.push(`--force`),_&&w.push(`--detach`),!b&&s&&w.push(`-b`,n),w.push(C),!b&&s?w.push(x?i:`HEAD`):w.push(n);let T=await t(`git`,w,{cwd:a,throwOnError:!1});if(!T.success)throw new e(`git worktree add ${C} ${n}`,T.stderr);return{success:!0,message:`Created worktree at ${C} for branch ${n}`,details:{path:C,branch:n}}}case`remove`:{let n=await l(a),i;if(v){if(n.length===0)throw Error(`No worktrees found`);let e=m&&p(m,n)||c||null,t=e===null?0:Math.max(0,n.findIndex(t=>t.path===e));i=(await f(`Select worktree to remove:`,n.map(e=>({label:`${e.branch||`detached`} — ${e.path}`,value:e})),t)).path}else if(m){let e=p(m,n);if(!e)throw Error(`Could not find worktree matching "${m}". Use --list to see available worktrees.`);i=e}else if(c){if(!r(c))throw Error(`Path "${c}" does not exist. Use --branch (-b) to remove by name, or --list to see available worktrees.`);i=c}else throw Error(`--branch (-b) or --path (-p) is required for remove action.`);let o=[`worktree`,`remove`];g&&o.push(`--force`),o.push(i);let s=await t(`git`,o,{cwd:a,throwOnError:!1});if(!s.success)throw new e(`git worktree remove ${i}`,s.stderr);return{success:!0,message:`Removed worktree at ${i}`,details:{path:i}}}case`prune`:{let n=await t(`git`,[`worktree`,`prune`],{cwd:a,throwOnError:!1});if(!n.success)throw new e(`git worktree prune`,n.stderr);return{success:!0,message:`Pruned stale worktrees`,details:{output:n.stdout}}}default:throw Error(`Unknown worktree action: ${s}`)}}export{m as t};