#!/usr/bin/env node
import*as i from'fs';import*as a from'path';import*as P from'os';import*as x from'crypto';import {exec}from'child_process';var U=a.join(P.homedir(),".claude","plugins"),C=a.join(P.homedir(),".paradigm","plugin-update-check.json"),A=6;function $(e,s={}){return new Promise((n,l)=>{exec(e,{timeout:s.timeout||3e3,cwd:s.cwd},(r,t)=>{if(r)return l(r);n(t.trim());});})}function k(e){try{return i.existsSync(e)?JSON.parse(i.readFileSync(e,"utf8")):null}catch{return null}}function b(){return k(C)}function H(e){let s=a.dirname(C);i.existsSync(s)||i.mkdirSync(s,{recursive:true}),i.writeFileSync(C,JSON.stringify(e,null,2));}function E(){let e=b();return e?Date.now()-new Date(e.lastCheck).getTime()<A*3600*1e3:false}function V(e){let s=x.createHash("sha256"),n=new Set([".md",".json",".sh",".yaml",".yml",".ts",".js"]),l=new Set([".git","node_modules",".cache"]);function r(t){let o;try{o=i.readdirSync(t,{withFileTypes:!0});}catch{return}o.sort((c,u)=>c.name.localeCompare(u.name));for(let c of o){if(l.has(c.name))continue;let u=a.join(t,c.name);if(c.isDirectory())r(u);else if(c.isFile()){let p=a.extname(c.name).toLowerCase();if(n.has(p))try{let f=i.readFileSync(u,"utf8"),y=a.relative(e,u);s.update(y+"\0"+f);}catch{}}}}return r(e),s.digest("hex").slice(0,12)}function N(){let e=[],s=a.join(U,"marketplaces"),n=a.join(U,"cache");if(!i.existsSync(s))return e;let l;try{l=i.readdirSync(s).filter(r=>{let t=a.join(s,r);return i.statSync(t).isDirectory()&&i.existsSync(a.join(t,".git"))});}catch{return e}for(let r of l){let t=a.join(s,r),o=a.join(t,"plugins");if(!i.existsSync(o))continue;let c;try{c=i.readdirSync(o).filter(u=>i.statSync(a.join(o,u)).isDirectory());}catch{continue}for(let u of c){let p=a.join(o,u,".claude-plugin","plugin.json"),f=k(p);if(!f)continue;let y=f.version||"unknown",d=a.join(n,r,u),m="unknown",D="unknown";if(i.existsSync(d))try{let S=i.readdirSync(d).filter(h=>i.statSync(a.join(d,h)).isDirectory()).sort((h,R)=>{let j=h.split(".").map(Number),v=R.split(".").map(Number);for(let g=0;g<3;g++)if((j[g]||0)!==(v[g]||0))return (j[g]||0)-(v[g]||0);return 0}).reverse();if(S.length>0){m=S[0];let h=k(a.join(d,S[0],".claude-plugin","plugin.json"));h?.sha&&(D=h.sha);}}catch{}let w=r;try{let h=i.readFileSync(a.join(t,".git","config"),"utf8").match(/github\.com[:/]([^/]+\/[^/\s.]+)/);h&&(w=h[1].replace(/\.git$/,""));}catch{}e.push({repo:w,plugin:u,installedVersion:m,installedSha:D,marketplacePath:t,localVersion:y,pluginSourceDir:a.join(o,u),pluginCacheDir:i.existsSync(d)&&m!=="unknown"?a.join(d,m):""});}}return e}async function I(e){let s=null,n=e.installedSha;try{n=await $("git rev-parse HEAD",{cwd:e.marketplacePath});}catch{}try{let p=(await $("git ls-remote origin HEAD",{cwd:e.marketplacePath,timeout:3e3})).match(/^([a-f0-9]+)/);p&&(s=p[1]);}catch{}let l=s!==null&&s!==n,r=e.localVersion!==e.installedVersion&&e.installedVersion!=="unknown",t=V(e.pluginSourceDir),o=e.pluginCacheDir?V(e.pluginCacheDir):"",c=o!==""&&t!==o;return {repo:e.repo,plugin:e.plugin,installedVersion:e.installedVersion,installedSha:n,localVersion:e.localVersion,remoteSha:s,marketplacePath:e.marketplacePath,hasRemoteUpdate:l,hasCacheStale:r||c,marketplaceContentHash:t,cachedContentHash:o,hasContentDrift:c}}function J(){let e=b();if(!e?.results)return null;let s=e.results.filter(t=>t.hasRemoteUpdate||t.hasCacheStale);if(s.length===0)return null;let n=[];n.push("--- PLUGIN UPDATES AVAILABLE ---"),n.push("Updates available for installed Claude Code plugins:");let l=[],r=[];for(let t of s)if(t.hasRemoteUpdate){let o=t.installedVersion!=="unknown"?`${t.installedVersion} \u2192 ${t.localVersion}`:"newer commits available";n.push(`  - ${t.plugin} (${t.repo}): ${o}`),l.push(`git -C ${t.marketplacePath} pull origin main`),r.push(`/plugin marketplace add ${t.repo}`);}else t.hasContentDrift?(n.push(`  - ${t.plugin} (${t.repo}): content changed (hash ${t.cachedContentHash} \u2192 ${t.marketplaceContentHash}, reinstall needed)`),r.push(`/plugin marketplace add ${t.repo}`)):t.hasCacheStale&&(n.push(`  - ${t.plugin} (${t.repo}): ${t.installedVersion} \u2192 ${t.localVersion} (reinstall needed)`),r.push(`/plugin marketplace add ${t.repo}`));if(n.push(""),n.push("Tell the user about these updates and suggest the following steps:"),l.length>0&&(n.push(""),n.push("Step 1 \u2014 Pull latest from remote:"),n.push(`  ${l.join(` && \\
  `)}`)),r.length>0){let t=l.length>0?2:1;n.push(""),n.push(`Step ${t} \u2014 Reinstall to update the cached version:`);for(let o of r)n.push(`  ${o}`);n.push(""),n.push(`Step ${t+1} \u2014 Restart the Claude Code session to load the new version.`);}return n.push("---"),n.join(`
`)}function O(){E()||(async()=>{try{let e=await L();H({lastCheck:new Date().toISOString(),results:e});}catch{}})();}async function L(){let e=N();if(e.length===0)return [];let s=await Promise.all(e.map(n=>I(n)));return H({lastCheck:new Date().toISOString(),results:s}),s}export{J as a,O as b,L as c};