var e=0;function t(t){return`${t}_callback_${Date.now()}_${e++}`}function n(e,n={}){return new Promise((r,i)=>{let a=t(`exec`);window[a]=(e,t,n)=>{r({errno:e,stdout:t,stderr:n}),o(a)};function o(e){delete window[e]}try{typeof ksu<`u`?ksu.exec(e,JSON.stringify(n),a):r({errno:1,stdout:``,stderr:`ksu is not defined`})}catch(e){i(e),o(a)}})}var r=class{constructor(){this.listeners={}}on(e,t){this.listeners[e]||(this.listeners[e]=[]),this.listeners[e].push(t)}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach(e=>e(...t))}};function i(e,n=[],i={}){let a={listeners:{},stdout:new r,stderr:new r,stdin:new r,on(e,t){this.listeners[e]||(this.listeners[e]=[]),this.listeners[e].push(t)},emit(e,...t){this.listeners[e]&&this.listeners[e].forEach(e=>e(...t))}},o=t(`spawn`);window[o]=a,a.on(`exit`,()=>delete window[o]);try{typeof ksu<`u`?ksu.spawn(e,JSON.stringify(n),JSON.stringify(i),o):setTimeout(()=>{a.stderr.emit(`data`,`ksu is not defined`),a.emit(`exit`,1)},0)}catch(e){a.emit(`error`,e),delete window[o]}return a}function a(e){typeof ksu<`u`?ksu.toast(e):console.log(e)}async function o(e){if(typeof globalThis.ksu?.listPackages==`function`)try{return JSON.parse(ksu.listPackages(e))}catch{}return new Promise((t,n)=>{let r={all:[],user:[`-3`],system:[`-s`]};if(!(e in r))return n(Error(`Unknown listPackages type: ${e}`));let a=[],o=``,s=i(`pm`,[`list`,`packages`,...r[e]]);s.stdout.on(`data`,e=>{e.trim()!==``&&a.push(e.trim().replace(/^package:/,``))}),s.stderr.on(`data`,e=>o+=e),s.on(`exit`,e=>{if(e!==0)return n(Error(`pm process exited with code ${e}: ${o.trim()}`));t(a)}),s.on(`error`,e=>{n(e)})})}function s(e){return new Promise((t,n)=>{if(!e)return t([]);if(typeof globalThis.ksu?.getPackagesInfo!=`function`)return n(Error(`ksu.getPackagesInfo is not available.`));let r=Array.isArray(e)?e:[e];if(r.length===0)return t([]);try{let n=ksu.getPackagesInfo(JSON.stringify(r)),i=JSON.parse(n);!Array.isArray(e)&&i.length===1?t(i[0]):t(i)}catch(e){n(Error(`Failed to get package info: ${e.message}`))}})}export{a,i,s as n,o as r,n as t};