import"../../errors-pXKbCKbL.js";import"../../runner-DYk4q6mP.js";import"../../utils-DMbpVz3Q.js";import"../../pr-DC2iMrPz.js";import"../../view-BZmgL-cB.js";import"../../worktree-DTI5xaFs.js";import"../../create-DiqmyIaU.js";import{t as e}from"../../dispatcher-Ci7OG66P.js";import{Command as t,Flags as n}from"@oclif/core";import{spawn as r}from"node:child_process";var i=class i extends t{static summary=`Switch to a git worktree (opens subshell by default)`;static description=`
    Find a matching worktree, then start a subshell rooted there.
    Defaults to the main branch if no branch/directory is provided.
    Use --path-only for scripting (outputs the path for cd $(...)).
    Use --select to pick a worktree from an interactive list.
  `;static examples=[{description:`Switch to main worktree`,command:`cd $(<%= config.bin %> <%= command.id %>)`},{description:`Switch to main worktree without subshell (path only)`,command:`<%= config.bin %> <%= command.id %> --path-only`},{description:`Switch to specific worktree by branch name`,command:`cd $(<%= config.bin %> <%= command.id %> -b feature/login)`},{description:`Switch by directory name`,command:`cd $(<%= config.bin %> <%= command.id %> -b xling-feature)`},{description:`Use interactive selector to pick a worktree`,command:`cd $(<%= config.bin %> <%= command.id %> --select)`}];static flags={branch:n.string({char:`b`,description:`Branch or worktree directory name (defaults to main)`}),"path-only":n.boolean({description:`Only print the worktree path (useful for cd $(...))`,default:!1}),select:n.boolean({description:`Use an interactive selector to choose the worktree`,default:!1})};async run(){let{flags:t}=await this.parse(i),n=new e,r={action:`switch`,branch:t.branch,interactive:t.select};try{let e=await n.execute({command:`worktree`,cwd:process.cwd(),data:r}),i=e.details?.path,a=typeof i==`string`?i:e.message;if(t[`path-only`]){this.log(a);return}this.log(`Switching to worktree at ${a}. Exit the shell to return.`),await this.#t(a)}catch(e){this.error(e.message,{exit:1})}}#e(){return process.platform===`win32`?{command:process.env.COMSPEC||`cmd.exe`,args:[]}:{command:process.env.SHELL||`bash`,args:[]}}async#t(e){let{command:t,args:n}=this.#e();await new Promise((i,a)=>{let o=r(t,n,{cwd:e,stdio:`inherit`});o.on(`error`,e=>{a(Error(`Failed to launch shell "${t}": ${e.message}`))}),o.on(`exit`,e=>{e===0?i():a(Error(`Shell exited with code ${e}`))})})}};export{i as default};