function i(e){try{const t=new URL(e);if(!(t.hostname==="localhost"||t.hostname==="127.0.0.1"||t.hostname==="::1")&&t.protocol!=="https:")throw new Error(`Insecure balance service URL: ${t.protocol}// — HTTPS required for non-localhost`)}catch(t){throw t instanceof Error&&t.message.startsWith("Insecure")?t:new Error(`Invalid balance service URL: ${e}`)}}function f(e){if(!e||typeof e!="object")throw new Error("Invalid server response");const t=e;if(typeof t.txId!="string"||typeof t.txHash!="string")throw new Error("Invalid server response: missing txId or txHash");return{txId:t.txId,txHash:t.txHash}}function h(e){if(!e||typeof e!="object")throw new Error("Invalid server response");const t=e;if(typeof t.txHash!="string"||typeof t.txBytes!="string")throw new Error("Invalid server response: missing txHash or txBytes");return{txHash:t.txHash,txBytes:t.txBytes,expiresAt:typeof t.expiresAt=="string"?t.expiresAt:""}}class c extends Error{constructor(t,n,a,o){super(`${n}: ${a}`),this.status=t,this.code=n,this.detail=a,this.retryAfterMs=o,this.name="BalanceServiceError"}get isRetryable(){return this.status===503}get isWalletSyncing(){return this.code==="WALLET_SYNCING"}get isInsufficientDust(){return this.code==="INSUFFICIENT_DUST"}}async function u(e,t,n,a){i(e);const o={"Content-Type":"application/octet-stream"};n&&(o["X-API-Key"]=n),a&&(o["X-Client-ID"]=a);const s=await fetch(`${e}/prove-and-submit`,{method:"POST",headers:o,body:t}),r=await s.json();if(!s.ok)throw new c(s.status,r?.error||"UNKNOWN",r?.message||r?.error||`HTTP ${s.status}`,r?.retryAfterMs);return f(r)}async function y(e,t,n,a){i(e);const o={"Content-Type":"application/octet-stream"};n&&(o["X-API-Key"]=n),a&&(o["X-Client-ID"]=a);const s=await fetch(`${e}/balance-and-submit`,{method:"POST",headers:o,body:t}),r=await s.json();if(!s.ok)throw new c(s.status,r?.error||"UNKNOWN",r?.message||r?.error||`HTTP ${s.status}`,r?.retryAfterMs);return f(r)}async function d(e,t,n,a){i(e);const o={"Content-Type":"application/octet-stream"};n&&(o["X-API-Key"]=n),a&&(o["X-Client-ID"]=a);const s=await fetch(`${e}/prove-and-balance`,{method:"POST",headers:o,body:t}),r=await s.json();if(!s.ok)throw new c(s.status,r?.error||"UNKNOWN",r?.message||r?.error||`HTTP ${s.status}`,r?.retryAfterMs);return h(r)}async function w(e,t,n,a){i(e);const o={"Content-Type":"application/octet-stream"};n&&(o["X-API-Key"]=n),a&&(o["X-Client-ID"]=a);const s=await fetch(`${e}/balance-only`,{method:"POST",headers:o,body:t}),r=await s.json();if(!s.ok)throw new c(s.status,r?.error||"UNKNOWN",r?.message||r?.error||`HTTP ${s.status}`,r?.retryAfterMs);return h(r)}async function p(e,t,n,a){i(e);const o={"Content-Type":"application/json"};n&&(o["X-API-Key"]=n),a&&(o["X-Client-ID"]=a);const s=await fetch(`${e}/confirm-submitted`,{method:"POST",headers:o,body:JSON.stringify({txHash:t})});if(!s.ok)throw new Error(`confirmSubmitted failed: ${s.status} ${s.statusText}`)}async function l(e){try{return i(e),(await(await fetch(`${e}/ready`)).json())?.balanceReady===!0}catch{return!1}}export{c as BalanceServiceError,y as balanceAndSubmit,w as balanceOnly,l as checkBalanceServiceReady,p as confirmSubmitted,d as proveAndBalance,u as proveAndSubmit};
