function f(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 p(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 l(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 d extends Error{constructor(t,r,a,n){super(`${r}: ${a}`),this.status=t,this.code=r,this.detail=a,this.retryAfterMs=n,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 h(e,t){try{return await e.json()}catch{throw new Error(`${t} returned invalid JSON`)}}function c(e,t){const r=e[t];return typeof r=="string"?r:void 0}function y(e){const t=e.retryAfterMs;return typeof t=="number"&&Number.isFinite(t)?t:void 0}async function m(e,t,r,a){f(e);const n={"Content-Type":"application/octet-stream"};r&&(n["X-API-Key"]=r),a&&(n["X-Client-ID"]=a);const s=await fetch(`${e}/prove-and-submit`,{method:"POST",headers:n,body:t}),o=await h(s,"proveAndSubmit");if(!s.ok){const i=c(o,"error"),u=c(o,"message");throw new d(s.status,i??"UNKNOWN",u??i??`HTTP ${s.status}`,y(o))}return p(o)}async function w(e,t,r,a){f(e);const n={"Content-Type":"application/octet-stream"};r&&(n["X-API-Key"]=r),a&&(n["X-Client-ID"]=a);const s=await fetch(`${e}/balance-and-submit`,{method:"POST",headers:n,body:t}),o=await h(s,"balanceAndSubmit");if(!s.ok){const i=c(o,"error"),u=c(o,"message");throw new d(s.status,i??"UNKNOWN",u??i??`HTTP ${s.status}`,y(o))}return p(o)}async function b(e,t,r,a){f(e);const n={"Content-Type":"application/octet-stream"};r&&(n["X-API-Key"]=r),a&&(n["X-Client-ID"]=a);const s=await fetch(`${e}/prove-and-balance`,{method:"POST",headers:n,body:t}),o=await h(s,"proveAndBalance");if(!s.ok){const i=c(o,"error"),u=c(o,"message");throw new d(s.status,i??"UNKNOWN",u??i??`HTTP ${s.status}`,y(o))}return l(o)}async function g(e,t,r,a){f(e);const n={"Content-Type":"application/octet-stream"};r&&(n["X-API-Key"]=r),a&&(n["X-Client-ID"]=a);const s=await fetch(`${e}/balance-only`,{method:"POST",headers:n,body:t}),o=await h(s,"balanceOnly");if(!s.ok){const i=c(o,"error"),u=c(o,"message");throw new d(s.status,i??"UNKNOWN",u??i??`HTTP ${s.status}`,y(o))}return l(o)}async function T(e,t,r,a){f(e);const n={"Content-Type":"application/json"};r&&(n["X-API-Key"]=r),a&&(n["X-Client-ID"]=a);const s=await fetch(`${e}/confirm-submitted`,{method:"POST",headers:n,body:JSON.stringify({txHash:t})});if(!s.ok)throw new Error(`confirmSubmitted failed: ${s.status} ${s.statusText}`)}async function v(e){try{f(e);const t=await fetch(`${e}/ready`);return(await h(t,"checkBalanceServiceReady"))?.balanceReady===!0}catch{return!1}}export{d as BalanceServiceError,w as balanceAndSubmit,g as balanceOnly,v as checkBalanceServiceReady,T as confirmSubmitted,b as proveAndBalance,m as proveAndSubmit};
