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