import{dc as e,yc as t}from"./paths-Ci1iwL5Y.js";import{E as n,O as r,l as i,p as a}from"./debug-otxXY3j-.js";import{n as o,t as s}from"./which-BnEnzWhs.js";import{r as c,t as l}from"./execFileNoThrow-azCkYhmF.js";import{n as u,t as d}from"./githubRepoPathMapping-DGiSNykv.js";import{n as f}from"./banner-D9Cq292T.js";import{a as p,i as m,n as h}from"./registerProtocol-BkNncGMI.js";import{homedir as g}from"os";import{basename as _}from"path";import{spawn as v}from"child_process";n(),m(),h(),t(),i(),c(),s();var y=[{name:`iTerm2`,bundleId:`com.googlecode.iterm2`,app:`iTerm`},{name:`Ghostty`,bundleId:`com.mitchellh.ghostty`,app:`Ghostty`},{name:`Kitty`,bundleId:`net.kovidgoyal.kitty`,app:`kitty`},{name:`Alacritty`,bundleId:`org.alacritty`,app:`Alacritty`},{name:`WezTerm`,bundleId:`com.github.wez.wezterm`,app:`WezTerm`},{name:`Terminal.app`,bundleId:`com.apple.Terminal`,app:`Terminal`}],b=[`ghostty`,`kitty`,`alacritty`,`wezterm`,`gnome-terminal`,`konsole`,`xfce4-terminal`,`mate-terminal`,`tilix`,`xterm`];async function x(){let t=e().deepLinkTerminal;if(t){let e=y.find(e=>e.app===t);if(e)return{name:e.name,command:e.app}}let n=process.env.TERM_PROGRAM;if(n){let e=n.replace(/\.app$/i,``).toLowerCase(),t=y.find(t=>t.app.toLowerCase()===e||t.name.toLowerCase()===e);if(t)return{name:t.name,command:t.app}}for(let e of y){let{code:t,stdout:n}=await l(`mdfind`,[`kMDItemCFBundleIdentifier == "${e.bundleId}"`],{timeout:5e3,useCwd:!1});if(t===0&&n.trim().length>0)return{name:e.name,command:e.app}}for(let e of y){let{code:t}=await l(`ls`,[`/Applications/${e.app}.app`],{timeout:1e3,useCwd:!1});if(t===0)return{name:e.name,command:e.app}}return{name:`Terminal.app`,command:`Terminal`}}async function S(){let e=process.env.TERMINAL;if(e){let t=await o(e);if(t)return{name:_(e),command:t}}let t=await o(`x-terminal-emulator`);if(t)return{name:`x-terminal-emulator`,command:t};for(let e of b){let t=await o(e);if(t)return{name:e,command:t}}return null}async function C(){let e=await o(`wt.exe`);if(e)return{name:`Windows Terminal`,command:e};let t=await o(`pwsh.exe`);if(t)return{name:`PowerShell`,command:t};let n=await o(`powershell.exe`);return n?{name:`PowerShell`,command:n}:{name:`Command Prompt`,command:`cmd.exe`}}async function w(){switch(process.platform){case`darwin`:return x();case`linux`:return S();case`win32`:return C();default:return null}}async function T(e,t){let n=await w();if(!n)return a(`No terminal emulator detected`,{level:`error`}),!1;a(`Launching in terminal: ${n.name} (${n.command})`);let r=[`--deep-link-origin`];switch(t.repo&&(r.push(`--deep-link-repo`,t.repo),t.lastFetchMs!==void 0&&r.push(`--deep-link-last-fetch`,String(t.lastFetchMs))),t.query&&r.push(`--prefill`,t.query),process.platform){case`darwin`:return E(n,e,r,t.cwd);case`linux`:return D(n,e,r,t.cwd);case`win32`:return O(n,e,r,t.cwd);default:return!1}}async function E(e,t,n,r){switch(e.command){case`iTerm`:{let{code:e}=await l(`osascript`,[`-e`,`tell application "iTerm"
  if running then
    create window with default profile
  else
    activate
  end if
  tell current session of current window
    write text ${M(A(t,n,r))}
  end tell
end tell`],{useCwd:!1});if(e===0)return!0;break}case`Terminal`:{let{code:e}=await l(`osascript`,[`-e`,`tell application "Terminal"
  do script ${M(A(t,n,r))}
  activate
end tell`],{useCwd:!1});return e===0}case`Ghostty`:{let i=[`-na`,e.command,`--args`,`--window-save-state=never`];r&&i.push(`--working-directory=${r}`),i.push(`-e`,t,...n);let{code:a}=await l(`open`,i,{useCwd:!1});if(a===0)return!0;break}case`Alacritty`:{let i=[`-na`,e.command,`--args`];r&&i.push(`--working-directory`,r),i.push(`-e`,t,...n);let{code:a}=await l(`open`,i,{useCwd:!1});if(a===0)return!0;break}case`kitty`:{let i=[`-na`,e.command,`--args`];r&&i.push(`--directory`,r),i.push(t,...n);let{code:a}=await l(`open`,i,{useCwd:!1});if(a===0)return!0;break}case`WezTerm`:{let i=[`-na`,e.command,`--args`,`start`];r&&i.push(`--cwd`,r),i.push(`--`,t,...n);let{code:a}=await l(`open`,i,{useCwd:!1});if(a===0)return!0;break}}return a(`Failed to launch ${e.name}, falling back to Terminal.app`),E({name:`Terminal.app`,command:`Terminal`},t,n,r)}async function D(e,t,n,r){let i,a;switch(e.name){case`gnome-terminal`:i=r?[`--working-directory=${r}`,`--`]:[`--`],i.push(t,...n);break;case`konsole`:i=r?[`--workdir`,r,`-e`]:[`-e`],i.push(t,...n);break;case`kitty`:i=r?[`--directory`,r]:[],i.push(t,...n);break;case`wezterm`:i=r?[`start`,`--cwd`,r,`--`]:[`start`,`--`],i.push(t,...n);break;case`alacritty`:i=r?[`--working-directory`,r,`-e`]:[`-e`],i.push(t,...n);break;case`ghostty`:i=r?[`--working-directory=${r}`,`-e`]:[`-e`],i.push(t,...n);break;case`xfce4-terminal`:case`mate-terminal`:i=r?[`--working-directory=${r}`,`-x`]:[`-x`],i.push(t,...n);break;case`tilix`:i=r?[`--working-directory=${r}`,`-e`]:[`-e`],i.push(t,...n);break;default:i=[`-e`,t,...n],a=r;break}return k(e.command,i,{cwd:a})}async function O(e,t,n,r){let i=[];switch(e.name){case`Windows Terminal`:r&&i.push(`-d`,r),i.push(`--`,t,...n);break;case`PowerShell`:{let e=r?`Set-Location ${N(r)}; `:``;i.push(`-NoExit`,`-Command`,`${e}& ${N(t)} ${n.map(N).join(` `)}`);break}default:{let e=r?`cd /d ${P(r)} && `:``;i.push(`/k`,`${e}${P(t)} ${n.map(e=>P(e)).join(` `)}`);break}}return k(e.command,i,{windowsVerbatimArguments:e.name===`Command Prompt`})}function k(e,t,n={}){return new Promise(r=>{let i=v(e,t,{detached:!0,stdio:`ignore`,cwd:n.cwd,windowsVerbatimArguments:n.windowsVerbatimArguments});i.once(`error`,t=>{a(`Failed to spawn ${e}: ${t.message}`,{level:`error`}),r(!1)}),i.once(`spawn`,()=>{i.unref(),r(!0)})})}function A(e,t,n){return`${n?`cd ${j(n)} && `:``}${[e,...t].map(j).join(` `)}`}function j(e){return`'${e.replace(/'/g,`'\\''`)}'`}function M(e){return`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`)}"`}function N(e){return`'${e.replace(/'/g,`''`)}'`}function P(e){return`"${e.replace(/"/g,``).replace(/%/g,`%%`).replace(/(\\+)$/,`$1$1`)}"`}i();async function F(e){a(`Handling deep link URI: ${e}`);let t;try{t=p(e)}catch(e){let t=e instanceof Error?e.message:String(e);return console.error(`Deep link error: ${t}`),1}a(`Parsed deep link action: ${r(t)}`);let{cwd:n,resolvedRepo:i}=await L(t),o=i?await f(n):void 0;return await T(process.execPath,{query:t.query,cwd:n,repo:i,lastFetchMs:o?.getTime()})?0:(console.error(`Failed to open a terminal. Make sure a supported terminal emulator is installed.`),1)}async function I(){if(process.env.__CFBundleIdentifier!==`com.anthropic.claude-code-url-handler`)return null;try{let{waitForUrlEvent:e}=await import(`./src-C-Ho8kYe.js`),t=await e(5e3);return t?await F(t):null}catch{return null}}async function L(e){if(e.cwd)return{cwd:e.cwd};if(e.repo){let t=await d(u(e.repo));if(t[0])return a(`Resolved repo ${e.repo} → ${t[0]}`),{cwd:t[0],resolvedRepo:e.repo};a(`No local clone found for repo ${e.repo}, falling back to home`)}return{cwd:g()}}export{F as handleDeepLinkUri,I as handleUrlSchemeLaunch};