import{execSync as $}from"node:child_process";import Z from"node:fs";import x from"node:os";import F from"node:path";var Y=process.env.PANTRY_REGISTRY_URL||"https://registry.pantry.dev",D={"Logi Options+":"logitech.com/options-plus",logioptionsplus:"logitech.com/options-plus","Logi Options Plus":"logitech.com/options-plus",logioptionsplus_installer:"logitech.com/options-plus"},y=new Set(["Safari","Numbers","Pages","Keynote","GarageBand","iMovie","Color Picker","Simulator","Simulator (Watch)","Preview","TextEdit","Automator","Font Book","Migration Assistant","Photo Booth","System Preferences","System Settings","Disk Utility","Terminal","Activity Monitor","Console","Grapher","Script Editor","Time Machine","Photos","Mail","Calendar","Contacts","Reminders","Notes","Maps","Messages","FaceTime","Books","News","Stocks","Weather","Home","Podcasts","Music","TV","Voice Memos","Freeform","Shortcuts","Chess","Dictionary","Stickies","Image Capture","Xcode"]),N=new Set(["Xcode","Numbers","Pages","Keynote","GarageBand","iMovie","Things3","Grammarly for Safari","AdBlock","HP Smart","Audible","Mini Motorways","Snake.io+","WhatsApp Messenger","Honey","Wappalyzer"]);function A(H,J){if(!H||!J||H===J)return!1;if(J==="?")return!!H;let L=(B)=>B.replace(/[,+].*/g,"").replace(/-.*$/,"").split(".").map((K)=>Number.parseInt(K,10)||0),j=L(H),q=L(J);for(let B=0;B<Math.max(j.length,q.length);B++){let K=j[B]||0,Q=q[B]||0;if(K>Q)return!0;if(K<Q)return!1}return!1}function T(H){return H.toLowerCase().replace(/[^a-z0-9]/g,"")}function S(H="/Applications"){if(process.platform!=="darwin")return[];let J=[];if(!Z.existsSync(H))return J;let L=Z.readdirSync(H).filter((j)=>j.endsWith(".app"));for(let j of L){let q=j.replace(/\.app$/,""),B=`${H}/${j}`,K="?",Q;try{let W=`${B}/Contents/Info.plist`;if(Z.existsSync(W)){let X=$(`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${W}" 2>/dev/null`,{timeout:5000,encoding:"utf-8"}).trim();if(X)K=X;try{Q=$(`/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${W}" 2>/dev/null`,{timeout:5000,encoding:"utf-8"}).trim()||void 0}catch{}}}catch{}J.push({name:q,version:K,path:B,bundleId:Q})}return J}async function P(){try{let H=await fetch(`${Y}/desktop-apps`);if(!H.ok)return[];let J=await H.json();return(J.apps??J??[]).filter((j)=>j&&j.domain).map((j)=>({domain:j.domain,label:j.label??j.name??j.domain,version:j.version??null,platforms:j.platforms}))}catch{return[]}}function R(H,J){let L=D[H];if(L){let q=J.find((B)=>B.domain===L);if(q)return q}let j=T(H);return J.find((q)=>T(q.label)===j||T(q.domain.replace(/\.(com|app|io|net|dev|org|ai|so|rest|fr)$/,""))===j)??null}async function u(H){let J=H||S(),L=await P(),j=[];for(let q of J){let B="unknown",K=null,Q=!1,W=null;if(y.has(q.name))B="system";else{let X=R(q.name,L);if(X)B="pantry",W=X.domain,K=X.version,Q=!!K&&A(K,q.version);else if(N.has(q.name))B="mas"}j.push({name:q.name,currentVersion:q.version,latestVersion:K,updateAvailable:Q,source:B,domain:W})}return j.sort((q,B)=>{if(q.updateAvailable&&!B.updateAvailable)return-1;if(!q.updateAvailable&&B.updateAvailable)return 1;return q.name.localeCompare(B.name)}),j}async function m(H){if(process.platform!=="darwin")return{success:!1,error:"Desktop app updates are macOS-only"};try{let J=await P(),L=R(H,J);if(!L)return{success:!1,error:`"${H}" is not a pantry-published desktop app`};let j=L.domain,q=await fetch(`${Y}/binaries/${encodeURI(j)}/metadata.json`);if(!q.ok)return{success:!1,error:`no registry metadata for ${j}`};let B=await q.json(),K=B.latestVersion,Q=x.arch()==="arm64"?"darwin-arm64":"darwin-x86-64",W=B.versions?.[K]?.platforms??{},X=W[Q]??W["darwin-arm64"]??W["darwin-x86-64"];if(!X?.tarball)return{success:!1,error:`no ${Q} build published for ${j}@${K}`};let k=`${Y}/${String(X.tarball).replace(/^\/?/,"")}`,z=Z.mkdtempSync(F.join(x.tmpdir(),"pantry-app-"));try{let w=F.join(z,"pkg.tar.gz"),C=await fetch(k);if(!C.ok)return{success:!1,error:`failed to download ${k} (${C.status})`};Z.writeFileSync(w,Buffer.from(await C.arrayBuffer()));let U=F.join(z,"x");Z.mkdirSync(U,{recursive:!0}),$(`tar -xzf "${w}" -C "${U}"`,{timeout:120000});let _=(V,E)=>{for(let O of Z.readdirSync(V,{withFileTypes:!0})){if(!O.isDirectory())continue;let g=F.join(V,O.name);if(O.name.endsWith(".app"))return g;if(E>0){let I=_(g,E-1);if(I)return I}}return null},G=_(U,3);if(!G)return{success:!1,error:`tarball for ${j} contained no .app bundle`};let M=`/Applications/${F.basename(G)}`;return $(`rm -rf "${M}"`,{timeout:30000}),$(`ditto "${G}" "${M}"`,{timeout:120000}),$(`xattr -dr com.apple.quarantine "${M}" 2>/dev/null || true`,{timeout:30000}),{success:!0,version:K}}finally{try{Z.rmSync(z,{recursive:!0,force:!0})}catch{}}}catch(J){return{success:!1,error:(J?.message||"Update failed").toString().trim().split(`
`).pop()}}}
export{y as Ma,N as Na,A as Oa,S as Pa,P as Qa,u as Ra,m as Sa};
