import{o as e}from"./rolldown-runtime.js";import{n as t,t as n}from"./jsx-runtime.js";import{Mt as r,Vt as i,j as a}from"./useActiveWallet-CEs8euQ3.js";import"./context-Cdw68BC2.js";import{_ as o,m as s}from"./index-CKYEOQWW.js";import{t as c}from"./modal-context-DHfp-mFQ.js";import{t as l}from"./circle-x.js";import{t as u}from"./ScreenLayout-Dy-3vlz4.js";var d=n(),f=e(t(),1);a();var p=({connectionFailed:e,onClose:t})=>(0,d.jsx)(u,e?{title:`Something went wrong`,subtitle:`We're on it. Please try again later.`,icon:l,iconVariant:`error`,primaryCta:{label:`Close`,onClick:t},watermark:!0}:{title:`Connecting to your wallet`,subtitle:`Please wait...`,iconVariant:`loading`,showClose:!0,onClose:t,watermark:!1}),m={component:()=>{let{authenticated:e,user:t}=r(),{client:n,closePrivyModal:a,createAnalyticsEvent:l,walletProxy:u}=i(),{navigate:m,data:h,setModalData:g,onUserCloseViaDialogOrKeybindRef:_}=c(),v=(0,f.useMemo)((()=>Date.now()),[]),[y,b]=(0,f.useState)(!1),{onCompleteNavigateTo:x,onFailure:S,shouldForceMFA:C,entropyId:w,entropyIdVerifier:T,recoveryMethod:E,connectingWalletAddress:D,isUnifiedWallet:O=!1}=h?.connectWallet??{},k=e=>{y||(b(!0),S(typeof e==`string`?Error(e):e))};(0,f.useEffect)((()=>{let t;return e?u?((async()=>{let e=await n.getAccessToken();if(!e)return k(`User must be authenticated and have a Privy wallet before it can be connected`);try{if(!O){if(!w||!T)return k(`For on-device first-class chain wallets, entropyId and entropyIdVerifier are required`);await u.connect({accessToken:e,entropyId:w,entropyIdVerifier:T})}C&&await u.verifyMfa({accessToken:e});let n=(Date.now()-v)/1e3;x===`EmbeddedWalletKeyExportScreen`&&n<1?t=setTimeout((()=>{m(x,!1)}),1e3*(1-n)):m(x,!1)}catch(e){if(o(e)&&E===`privy`){let e=await n.getAccessToken();if(!e)return k(`User must be authenticated and have a Privy wallet before it can be recovered`);try{l({eventName:`embedded_wallet_pinless_recovery_started`,payload:{walletAddress:D}}),(await u?.recover({accessToken:e,entropyId:w,entropyIdVerifier:T}))?.entropyId||k(Error(`Unable to recover wallet`)),x?m(x):a({shouldCallAuthOnSuccess:!1}),l({eventName:`embedded_wallet_recovery_completed`,payload:{walletAddress:D}}),m(x)}catch{k(`An error has occurred, please try again.`)}}else o(e)&&E!==`privy`&&E!==`privy-v2`?(g({...h,recoverWallet:{entropyId:w,entropyIdVerifier:T,onCompleteNavigateTo:x,onFailure:S},recoveryOAuthStatus:{provider:E,action:`recover`,isInAccountCreateFlow:!1,shouldCreateEth:!1,shouldCreateSol:!1}}),m(s(E))):k(e)}})(),()=>clearTimeout(t)):void 0:k(`User must be authenticated and have a Privy wallet before it can be connected`)}),[e,t,u]);let A=()=>{k(`User exited before wallet could be connected`),a({shouldCallAuthOnSuccess:!1})};return _.current=A,(0,d.jsx)(p,{connectionFailed:y,onClose:A})}};export{m as EmbeddedWalletConnectingScreen,m as default,p as EmbeddedWalletConnectingView};