import{o as e}from"./rolldown-runtime.js";import{n as t,t as n}from"./jsx-runtime.js";import{Vt as r,_t as i,bt as a,j as o,v as s,xt as c}from"./useActiveWallet-CEs8euQ3.js";import{b as l,k as u}from"./context-Cdw68BC2.js";import"./index-CKYEOQWW.js";import{x as d}from"./toViemAccount-BywVRwQG.js";import{t as f}from"./modal-context-DHfp-mFQ.js";import{t as p}from"./ScreenLayout-Dy-3vlz4.js";import{t as m}from"./shouldProceedtoEmbeddedWalletCreationFlow-Zce_uO5W.js";var h=n(),g=e(t(),1);o();var _=({providerApp:e,success:t,error:n,onClose:r})=>{let{title:i,subtitle:a}=(0,g.useMemo)((()=>t?{title:`Successfully connected with ${e.name}`,subtitle:`You're good to go!`}:n?{title:`Authentication failed`,subtitle:n.message}:{title:`Connecting to ${e.name}`,subtitle:`Please check the pop-up from ${e.name} to continue`}),[t,n,e.name]);return(0,h.jsx)(p,{title:i,subtitle:a,icon:e.logoUrl,iconVariant:`loading`,iconLoadingStatus:{success:t,fail:!!n},onBack:r,watermark:!0})},v={component:()=>{let e=u(),{data:t,navigate:n,setModalData:o,onUserCloseViaDialogOrKeybindRef:p}=f(),{crossAppAuthFlow:v,updateWallets:y,closePrivyModal:b,createAnalyticsEvent:x}=r(),{logout:S}=s(),[C,w]=(0,g.useState)({}),T=t?.crossAppAuth,E=new c(`There was an issue connecting your ${T?.name} account. Please try again.`),D=new d((async e=>{if(T?.popup)try{let t=await v({appId:e,popup:T.popup,action:T.action,disableSignup:T.disableSignup});w({data:t})}catch(e){if(e instanceof c)w({error:e});else if(e instanceof a){if(e.privyErrorCode===i.ACCOUNT_TRANSFER_REQUIRED&&e.data?.data?.nonce)return o({accountTransfer:{nonce:e.data?.data?.nonce,account:e.data?.data?.subject,displayName:e.data?.data?.account?.displayName,linkMethod:`privy:${T.appId}`,embeddedWalletAddress:e.data?.data?.otherUser?.embeddedWalletAddress,oAuthUserInfo:e.data?.data?.otherUser?.oAuthUserInfo}}),void n(`LinkConflictScreen`);T.popup&&T.popup.close(),w({error:E})}else w({error:E})}else w({error:E})})),O=()=>{C.data&&(y(),T?.onSuccess(C.data),b({shouldCallAuthOnSuccess:!0,isSuccess:!0})),T?.onError(C.error??new c(`User canceled flow`)),b({shouldCallAuthOnSuccess:!1,isSuccess:!1})};return p.current=O,(0,g.useEffect)((()=>{T?.appId?.length&&D.execute(T.appId)}),[T?.appId]),(0,g.useEffect)((()=>{if(!C.data)return;let t=C.data;if(e.legal.requireUsersAcceptTerms&&!t.hasAcceptedTerms){let e=setTimeout((()=>{n(`AffirmativeConsentScreen`)}),l);return()=>clearTimeout(e)}if(m(t,e.embeddedWallets)){let e=setTimeout((()=>{o({createWallet:{onSuccess:()=>{},onFailure:e=>{console.error(e),x({eventName:`embedded_wallet_creation_failure_logout`,payload:{error:e,provider:`privy:${T?.appId}`,screen:`CrossAppAuthScreen`}}),S()},callAuthOnSuccessOnClose:!0}}),n(`EmbeddedWalletOnAccountCreateScreen`)}),l);return()=>clearTimeout(e)}let r=setTimeout(O,l);return()=>clearTimeout(r)}),[C.data]),T?.appId?(0,h.jsx)(_,{providerApp:{id:T?.appId,logoUrl:T?.logoUrl,name:T?.name},success:!!C.data,error:C.error,onClose:O}):(console.warn(`Missing data for Screen`),null)},isShownBeforeReady:!0};export{v as CrossAppAuthScreen,v as default,_ as CrossAppAuthScreenView};