import{t as e}from"./xling-DyktHkvX.js";import{n as t,t as n}from"./types-DI1Q0wjk.js";import{spawn as r}from"node:child_process";var i=class{#e;#t;constructor(t){this.#t=t,this.#e=new e}async load(){return this.#e.getShortcuts(`user`)}async list(){let e=await this.load();return Object.entries(e).map(([e,t])=>{let n,r;if(t.command)n=`command`,r=t.command;else if(t.shell){n=`shell`;let e=this.#i(t.shell);r=e.length>50?e.substring(0,47)+`...`:e}else t.pipeline?(n=`pipeline`,r=this.#s(t.pipeline).map(e=>e.command).join(` | `)):(n=`command`,r=`unknown`);return{name:e,type:n,command:r,args:t.args||[],description:t.description}})}async run(e,n){let r=await this.load(),i=r[e];if(!i)throw new t(e,Object.keys(r));this.#d(e,i),i.command?await this.#n(i,n):i.shell?await this.#r(i.shell):i.pipeline&&await this.#o(i.pipeline)}async#n(e,t){let n=[...e.args||[],...t];await this.#t.runCommand(e.command,n)}async#r(e){return new Promise((t,n)=>{let i=this.#a(),a=r(this.#i(e),{shell:i,stdio:`inherit`});a.on(`error`,e=>{n(Error(`Failed to execute shell command: ${e.message}`))}),a.on(`exit`,e=>{e===0?t():n(Error(`Shell command exited with code ${e}`))})})}#i(e){return typeof e==`string`?e:this.#l(e)}#a(){return process.platform===`win32`?`pwsh`:!0}async#o(e){return new Promise((t,n)=>{let i=this.#s(e),a=[];for(let e=0;e<i.length;e++){let t=i[e],o=e===0,s=e===i.length-1,c=t.command,l=t.args||[],u=c===`pwsh`||c===`powershell`||c===`cmd`||c.includes(`\\`)||c.includes(`/`),d=process.platform===`win32`&&!u,f=r(c,l,{stdio:[o?`inherit`:`pipe`,s?`inherit`:`pipe`,`inherit`],shell:d,cwd:process.cwd(),env:process.env});!o&&a[e-1]&&a[e-1].stdout?.pipe(f.stdin),a.push(f),f.on(`error`,e=>{n(Error(`Failed to execute pipeline step "${t.command}": ${e.message}`))})}a[a.length-1].on(`exit`,e=>{e===0?t():n(Error(`Pipeline exited with code ${e}`))})})}#s(e){return(Array.isArray(e)?e:this.#c(e)).map(e=>({command:this.#l(e.command),args:e.args?this.#l(e.args):void 0}))}#c(e){let t=process.platform;return t===`win32`&&e.win32?e.win32:t===`darwin`&&e.darwin?e.darwin:t===`linux`&&e.linux?e.linux:e.default}#l(e){if(!this.#u(e))return e;let t=process.platform;return t===`win32`&&e.win32!==void 0?e.win32:t===`darwin`&&e.darwin!==void 0?e.darwin:t===`linux`&&e.linux!==void 0?e.linux:e.default}#u(e){return typeof e==`object`&&!!e&&!Array.isArray(e)&&`default`in e}#d(e,t){if(t.command===`sx`)throw new n(e);if(t.pipeline){let r=this.#s(t.pipeline);for(let t of r)if(t.command===`xling`&&t.args?.includes(`sx`))throw new n(e)}if(t.shell&&this.#i(t.shell).includes(`xling sx`))throw new n(e)}};export{i as t};