import{useCallback as r,useMemo as t}from"react";import{GetDepositConfig as e}from"@privy-io/routes";import{u as o}from"./internal-context-dfitw7Ot.mjs";import{create as a}from"zustand";import{u as n}from"./user-D5cqJNp9.mjs";import{u as s}from"./modal-context-DBwfUxD-.mjs";class i extends Error{constructor(){super("Back navigation"),this.name="BackNavigationError"}}const d=a((()=>null)),c=r=>{null!==d.getState()&&d.setState(r)},l=()=>d.setState(null);async function u(r,t){let o=await r.fetchPrivyRoute(e,{}),a={config:{status:"ready",data:{currencies:o.currencies,chains:o.chains}}};t?.aborted||c(a)}function p(){let t=d(),{closePrivyModal:e,privy:a}=o(),n=t?.params??null,s=t?.config??{status:"loading"},i=r((r=>{c({modalState:r})}),[]),l=r((async()=>{let r=t?.controller;if(n&&r&&!r.signal.aborted){c({config:{status:"loading"}});try{await u(a,r.signal)}catch(t){if(r.signal.aborted)return;throw c({config:{status:"error",error:t instanceof Error?t:Error("Failed to load deposit config")}}),t}}}),[n,a,t?.controller]),p=r((()=>{if(!t)return;let{modalState:r}=t;"complete"===r.step?t.onComplete():"failed"===r.step?t.onError(Error("DEPOSIT_FAILED")):"error"===r.step?t.onError(Error(r.code)):"refunded"===r.step?t.onError(Error("DEPOSIT_REFUNDED")):t.onError(Error("USER_EXITED")),e({shouldCallAuthOnSuccess:!1})}),[t,e]);return{modalState:t?.modalState??{step:"intro"},setModalState:i,config:s,retryConfig:l,params:n,close:p,onBack:t?.onBack}}function f(r){let{modalState:t,config:e,params:o,...a}=p();if(function(r,t){if(r.step!==t)throw Error("UNEXPECTED_STATE")}(t,r),!o||"ready"!==e.status)throw Error("UNEXPECTED_STATE");return{state:t,configData:e.data,params:o,...a}}const E=()=>{let{openModal:r,privy:e}=o(),{navigate:a}=s();return t((()=>({createDepositAddress:async t=>{let{resolve:o,reject:s,promise:p}=Promise.withResolvers();if(!n.getState().user)throw Error("User must be authenticated to create a deposit address");if(!t.destinationChain)throw Error("Invalid input: expected destinationChain");if(!t.destinationCurrency)throw Error("Invalid input: expected destinationCurrency");if(!t.destinationAddress)throw Error("Invalid input: expected destinationAddress");if(d.getState())return s(Error("Existing deposit address flow in progress")),await p;let f=new AbortController;d.setState({controller:f,onComplete:()=>{l(),o()},onError:r=>{l(),s(r)},onBack:t.showBackButton?()=>{f.abort(),l(),s(new i)}:void 0,params:{destinationChain:t.destinationChain,destinationCurrency:t.destinationCurrency,destinationAddress:t.destinationAddress,refundAddress:t.refundAddress,slippageBps:t.slippageBps},config:{status:"loading"},modalState:{step:"intro"}}),t.showBackButton?a("DepositAddressScreen"):r("DepositAddressScreen");try{await u(e,f.signal)}catch(t){if(f.signal.aborted)return await p;c({config:{status:"error",error:t instanceof Error?t:Error("Failed to load deposit config")},modalState:{step:"error",code:"ROUTE_UNAVAILABLE"}})}return await p}})),[r,a,e])};export{i as B,p as a,d as b,f as c,E as u};
//# sourceMappingURL=use-deposit-address-BwThybOQ.mjs.map
