import{ok,err}from"./_deps/shared/index.js";import{fromBase64,toBase64}from"./crypto-utils.js";import{Agent}from"./agent.js";import{ServiceDiscovery}from"./discovery.js";import{HttpsTransportAdapter}from"./transport.js";import{MemoryTrustRegistry}from"./trust-registry.js";import{importPublicKey}from"./identity.js";export var ConnectErrorCode;!function(e){e.DISCOVERY_FAILED="CONNECT_DISCOVERY_FAILED",e.AGENT_CREATION_FAILED="CONNECT_AGENT_CREATION_FAILED",e.TRUST_VERIFICATION_FAILED="CONNECT_TRUST_VERIFICATION_FAILED",e.INVALID_SERVICE_INFO="CONNECT_INVALID_SERVICE_INFO"}(ConnectErrorCode||(ConnectErrorCode={}));export async function connect(e,r={}){const t=r.discovery||new ServiceDiscovery,i=await t.discover(e);if(!i.ok)return err({code:ConnectErrorCode.DISCOVERY_FAILED,message:`Failed to discover service: ${i.error.message}`,hint:i.error.hint,cause:i.error});const o=i.value;if(!o.did||!o.endpoint||!o.publicKey)return err({code:ConnectErrorCode.INVALID_SERVICE_INFO,message:"Service info missing required fields (did, endpoint, publicKey)",hint:"Service must provide complete metadata"});const n=r.registry||new MemoryTrustRegistry;try{const e=fromBase64(o.publicKey),r=await importPublicKey(e);if(!r.ok)return err({code:ConnectErrorCode.TRUST_VERIFICATION_FAILED,message:"Invalid service public key",cause:r.error});await n.add({did:o.did,publicKey:r.value,x25519PublicKey:o.x25519PublicKey?await importPublicKey(fromBase64(o.x25519PublicKey)).then(e=>e.ok?e.value:void 0):void 0,mlKemPublicKey:o.mlKemPublicKey?fromBase64(o.mlKemPublicKey):void 0})}catch(e){return err({code:ConnectErrorCode.TRUST_VERIFICATION_FAILED,message:"Failed to verify service public key",cause:e})}const a=r.transport||new HttpsTransportAdapter({baseUrl:o.endpoint}),c={name:r.name||`client-${Date.now()}`,registry:n,transport:a,xchange:r.xchange,postQuantumSig:r.postQuantumSig},s=await Agent.create(c);return s.ok?ok({agent:s.value,service:o,did:o.did,endpoint:o.endpoint}):err({code:ConnectErrorCode.AGENT_CREATION_FAILED,message:"Failed to create agent",cause:s.error})}export async function acceptInvite(e,r={}){const{InviteService:t}=await import("./invite.js"),i=new t,o=await i.get(e);if(!o.ok)return err({code:ConnectErrorCode.DISCOVERY_FAILED,message:`Failed to get invite: ${o.error.message}`,cause:o.error});const n=o.value,a={name:n.from.name,did:n.from.did,endpoint:n.from.endpoint,publicKey:n.from.publicKey,x25519PublicKey:n.from.x25519PublicKey,mlKemPublicKey:n.from.mlKemPublicKey},c=r.registry||new MemoryTrustRegistry;try{const e=fromBase64(a.publicKey),r=await importPublicKey(e);if(!r.ok)return err({code:ConnectErrorCode.TRUST_VERIFICATION_FAILED,message:"Invalid service public key",cause:r.error});await c.add({did:a.did,publicKey:r.value,x25519PublicKey:a.x25519PublicKey?await importPublicKey(fromBase64(a.x25519PublicKey)).then(e=>e.ok?e.value:void 0):void 0,mlKemPublicKey:a.mlKemPublicKey?fromBase64(a.mlKemPublicKey):void 0})}catch(e){return err({code:ConnectErrorCode.TRUST_VERIFICATION_FAILED,message:"Failed to verify service public key",cause:e})}const s=r.transport||new HttpsTransportAdapter({baseUrl:a.endpoint}),d={name:r.name||`client-${Date.now()}`,registry:c,transport:s,xchange:r.xchange,postQuantumSig:r.postQuantumSig},u=await Agent.create(d);if(!u.ok)return err({code:ConnectErrorCode.AGENT_CREATION_FAILED,message:"Failed to create agent",cause:u.error});return(await i.accept({inviteUrl:e,acceptor:{name:d.name,did:u.value.did,endpoint:r.endpoint||"",publicKey:toBase64(u.value.identity.rawPublicKey),x25519PublicKey:u.value.identity.rawX25519PublicKey?toBase64(u.value.identity.rawX25519PublicKey):void 0}})).ok,ok({agent:u.value,service:a,did:a.did,endpoint:a.endpoint})}