#!/usr/bin/env node
import'./chunk-5TAVYPOV.js';var l="https://api.nevr.land",h=class extends Error{constructor(t,e,a){super(t),this.status=e,this.code=a,this.name="RegistryError";}},d=class{baseUrl;authState=null;authStore;constructor(t){this.baseUrl=(t?.baseUrl??process.env.NEVR_REGISTRY_URL??l).replace(/\/$/,""),this.authStore=t?.authStore??null;}async loadAuth(){if(!this.authStore)return  false;try{return this.authState=await this.authStore.load(),this.authState&&this.authState.expiresAt<Date.now()&&await this.refreshAuth(),!!this.authState}catch{return  false}}async saveAuth(){this.authStore&&this.authState&&await this.authStore.save(this.authState);}async login(t,e){let a=await this.post("/api/v1/auth/login",{email:t,password:e},false);return this.authState={accessToken:a.access_token,refreshToken:a.refresh_token,expiresAt:Date.now()+a.expires_in*1e3,email:a.user.email},await this.saveAuth(),{email:a.user.email}}async logout(){this.authState=null,this.authStore&&await this.authStore.clear();}async whoami(){if(!this.authState&&!await this.loadAuth())return null;try{return await this.get("/api/v1/auth/me")}catch{return null}}async refreshAuth(){if(this.authState?.refreshToken)try{let t=await this.post("/api/v1/auth/refresh",{refresh_token:this.authState.refreshToken},!1);this.authState.accessToken=t.access_token,this.authState.refreshToken=t.refresh_token,this.authState.expiresAt=Date.now()+t.expires_in*1e3,await this.saveAuth();}catch{this.authState=null;}}get isAuthenticated(){return !!this.authState?.accessToken}get currentEmail(){return this.authState?.email??null}async search(t,e){let a=new URLSearchParams({q:t});return e?.tags&&a.set("tags",e.tags),e?.domain&&a.set("domain",e.domain),e?.page&&a.set("page",String(e.page)),this.get(`/api/v1/agents?${a}`)}async getAgent(t,e){let a=t.replace("@","");return this.get(`/api/v1/agents/${a}/${e}`)}async getVersions(t,e){let a=t.replace("@","");return this.get(`/api/v1/agents/${a}/${e}/versions`)}async registerAgent(t){await this.ensureAuth();let e=t.name,a=t.scope,s=e;if(!a&&e?.includes("/")){let r=e.indexOf("/");a=e.substring(0,r),s=e.substring(r+1);}return this.post("/api/v1/agents",{...t,scope:a,name:s})}async publishVersion(t,e,a,s){await this.ensureAuth();let r=t.replace("@",""),o=`${this.baseUrl}/api/v1/agents/${r}/${e}/versions`,n=new FormData;n.append("package",new Blob([new Uint8Array(a)]),`${e}.nevr.tar.gz`),n.append("manifest",JSON.stringify(s));let i=await fetch(o,{method:"POST",headers:{Authorization:`Bearer ${this.authState.accessToken}`},body:n});if(!i.ok){let u=await i.text();throw new h(`Publish failed: ${u}`,i.status)}return i.json()}async downloadPackage(t,e,a){let s=t.replace("@",""),r=a??"latest",o=`${this.baseUrl}/api/v1/agents/${s}/${e}/${r}/download`,n=await fetch(o,{redirect:"follow"});if(!n.ok)throw new h("Download failed",n.status);let i=Buffer.from(await n.arrayBuffer()),{createHash:u}=await import('crypto'),c=u("sha256").update(i).digest("hex");return {buffer:i,hash:c}}async ensureAuth(){if(!this.authState&&!await this.loadAuth())throw new h("Not authenticated. Run: nevr login",401)}authHeaders(){let t={"Content-Type":"application/json"};return this.authState?.accessToken&&(t.Authorization=`Bearer ${this.authState.accessToken}`),t}async get(t){let e=await fetch(`${this.baseUrl}${t}`,{headers:this.authHeaders()});if(!e.ok){let a=await e.text().catch(()=>"Unknown error");throw new h(a,e.status)}return e.json()}async post(t,e,a=true){a&&await this.ensureAuth();let s=await fetch(`${this.baseUrl}${t}`,{method:"POST",headers:this.authHeaders(),body:JSON.stringify(e)});if(!s.ok){let r=await s.text().catch(()=>"Unknown error");throw new h(r,s.status)}return s.json()}};
export{d as RegistryClient,h as RegistryError};