import{ok,err}from"./_deps/shared/index.js";import{autoAcceptInvite}from"./auto-accept.js";import{MemoryTrustRegistry}from"./trust-registry.js";import{HttpsTransportAdapter}from"./transport.js";import{toBase64}from"./crypto-utils.js";export var LazyAgentErrorCode;!function(t){t.INIT_FAILED="LAZY_AGENT_INIT_FAILED",t.AUTO_ACCEPT_FAILED="LAZY_AGENT_AUTO_ACCEPT_FAILED",t.AGENT_CREATION_FAILED="LAZY_AGENT_AGENT_CREATION_FAILED"}(LazyAgentErrorCode||(LazyAgentErrorCode={}));export class LazyAgent{config;agent=null;initPromise=null;lastError=null;_did;_identity;constructor(t,e){this.config=t,this._identity=e,this._did=e.did}get did(){return this._did}isReady(){return null!==this.agent}getLastError(){return this.lastError}async ensureInitialized(){if(this.agent)return ok(this.agent);if(this.initPromise)return this.initPromise;this.initPromise=this.initialize();const t=await this.initPromise;return this.initPromise=null,t.ok?(this.agent=t.value,this.lastError=null):this.lastError={code:LazyAgentErrorCode.INIT_FAILED,message:`Agent initialization failed: ${t.error}`,cause:t.error},t}async initialize(){const t=this.config.inviteCode??this.getEnv("XBIND_INVITE_CODE");let e=this.config.registry,i=this.config.transport;const r=this.config.endpoint??this.getEnv("XBIND_ENDPOINT")??"";if(t){const n=toBase64(this._identity.rawPublicKey),s=this._identity.rawX25519PublicKey?toBase64(this._identity.rawX25519PublicKey):void 0,o=await autoAcceptInvite({inviteCode:t,enabled:!0,registry:this.config.autoAccept?.registry,inviteApiUrl:this.config.autoAccept?.inviteApiUrl},{name:this.config.name,did:this._identity.did,endpoint:r,publicKey:n,x25519PublicKey:s});o.ok?(!e&&o.value.registryAutoconfigured&&(e=this.config.autoAccept?.registry??new MemoryTrustRegistry),i||(i=new HttpsTransportAdapter({baseUrl:o.value.from.endpoint}))):this.lastError={code:LazyAgentErrorCode.AUTO_ACCEPT_FAILED,message:o.error.message,hint:o.error.hint,cause:o.error}}const n=e??new MemoryTrustRegistry,s=i??new HttpsTransportAdapter({baseUrl:r||"http://localhost"}),o=await n.register({did:this._identity.did,publicKey:this._identity.rawPublicKey,name:this.config.name,scopes:void 0,x25519PublicKey:this._identity.rawX25519PublicKey,mlKemPublicKey:this._identity.mlKemPublicKey,mlDsaPublicKey:this._identity.mlDsaPublicKey,xchange:this.config.xchange??!1});if(!o.ok){this.lastError={code:LazyAgentErrorCode.AGENT_CREATION_FAILED,message:`Agent registration failed: ${o.error}`,cause:o.error};const t="ALREADY_REGISTERED"===o.error?"REGISTRATION_FAILED:ALREADY_REGISTERED":"NETWORK_ERROR"===o.error?"REGISTRATION_FAILED:NETWORK_ERROR":"REGISTRATION_FAILED";return err(t)}const{MemoryNonceStore:a}=await import("./nonce-store.js"),{DefaultSecurityPolicy:c}=await import("./security-policy.js"),d=new a,u=Array.isArray(s)?s:[s],l=new(0,(await import("./agent.js")).Agent)(this._identity,this.config.name,n,u,d,3e4,new c,void 0);return this.agent=l,ok(l)}async send(t){const e=await this.ensureInitialized();return e.ok?e.value.send(t):e}async receive(t,e){const i=await this.ensureInitialized();return i.ok?i.value.receive(t,e):err(i.error)}async receiveSplitShare(t){const e=await this.ensureInitialized();return e.ok?e.value.receiveSplitShare(t):err(e.error)}getEnv(t){return"undefined"!=typeof process&&void 0!==process.env?process.env[t]??"":""}getAgent(){if(!this.agent)throw new Error("Agent not initialized. Call send/receive first or await ensureInitialized().");return this.agent}}export async function createLazyAgent(t){const{generateIdentity:e}=await import("./identity.js"),i=await e({postQuantumSig:t.postQuantumSig});if(!i.ok)throw new Error(`Failed to generate identity: ${i.error}`);return new LazyAgent(t,i.value)}