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,_t as a,ft as o,j as s}from"./useActiveWallet-CEs8euQ3.js";import{b as c,k as l}from"./context-Cdw68BC2.js";import{d as u,t as d,u as f}from"./index-CKYEOQWW.js";import{t as p}from"./modal-context-DHfp-mFQ.js";import{t as m}from"./ScreenLayout-Dy-3vlz4.js";import{t as h}from"./shouldProceedtoEmbeddedWalletCreationFlow-Zce_uO5W.js";import{t as g}from"./telegram-B-JqnkqZ.js";var _=n(),v=e(t(),1);s();var y=({success:e,errorMessage:t,onRetry:n})=>{let r=e?`Successfully connected with Telegram`:t?t.message:`Verifying connection to Telegram`;return(0,_.jsx)(m,{title:r,subtitle:e?`You're good to go!`:t?t.detail:`Just a few moments more`,icon:g,iconVariant:`loading`,iconLoadingStatus:{success:e,fail:!!t},secondaryCta:t?.retryable&&n?{label:`Retry`,onClick:n}:void 0,watermark:!0})},b={component:()=>{let{authenticated:e,logout:t,ready:n,user:s}=r(),{setModalData:m,navigate:g,resetNavigation:b,data:x}=p(),S=l(),{initLoginWithTelegram:C,loginWithTelegram:w,updateWallets:T,setReadyToTrue:E,closePrivyModal:D,createAnalyticsEvent:O,getAuthMeta:k}=i(),[A,j]=(0,v.useState)(!1),[M,N]=(0,v.useState)(void 0),P=d();async function F(){try{let t=await async function(){let t;if(!e){if(P.enabled&&P.status===`error`)throw new u(P.error,null,a.CAPTCHA_FAILURE);return P.enabled&&P.status!==`success`&&(P.execute(),t=await P.waitForResult()),t}}();await w({captchaToken:t}),j(!0),E(!0)}catch(e){if(e?.privyErrorCode===a.ALLOWLIST_REJECTED)return N(void 0),b(),void g(`AllowlistRejectionScreen`);if(e?.privyErrorCode===a.USER_LIMIT_REACHED)return console.error(new o(e).toString()),N(void 0),b(),void g(`UserLimitReachedScreen`);if(e?.privyErrorCode===a.USER_DOES_NOT_EXIST)return N(void 0),b(),void g(`AccountNotFoundScreen`);if(e?.privyErrorCode===a.ACCOUNT_TRANSFER_REQUIRED&&e.data?.data?.nonce)return N(void 0),b(),m({accountTransfer:{nonce:e.data?.data?.nonce,account:e.data?.data?.subject,telegramAuthResult:k()?.telegramAuthResult,telegramWebAppData:k()?.telegramWebAppData,displayName:e.data?.data?.account?.displayName,linkMethod:`telegram`,embeddedWalletAddress:e.data?.data?.otherUser?.embeddedWalletAddress}}),void g(`LinkConflictScreen`);let{retryable:t,detail:n}=f(e);N({retryable:t,detail:n,message:`Authentication failed`})}}return(0,v.useEffect)((()=>{F()}),[]),(0,v.useEffect)((()=>{if(!(n&&e&&A&&s))return;if(S?.legal.requireUsersAcceptTerms&&!s.hasAcceptedTerms){let e=setTimeout((()=>{g(`AffirmativeConsentScreen`)}),c);return()=>clearTimeout(e)}if(h(s,S.embeddedWallets)){let e=setTimeout((()=>{m({createWallet:{onSuccess:()=>{},onFailure:e=>{console.error(e),O({eventName:`embedded_wallet_creation_failure_logout`,payload:{error:e,provider:`telegram`,screen:`TelegramAuthScreen`}}),t()},callAuthOnSuccessOnClose:!0}}),g(`EmbeddedWalletOnAccountCreateScreen`)}),c);return()=>clearTimeout(e)}T();let r=setTimeout((()=>D({shouldCallAuthOnSuccess:!0,isSuccess:!0})),c);return()=>clearTimeout(r)}),[n,e,A,s]),(0,_.jsx)(y,{success:A,errorMessage:M,onRetry:M?.retryable?async()=>{try{N(void 0),x?.telegramAuthModalData?.seamlessAuth||await C(void 0,x?.login?.disableSignup),await F()}catch(e){let{retryable:t,detail:n}=f(e);N({retryable:t,detail:n,message:`Authentication failed`})}}:void 0})},isCaptchaRequired:!0,isShownBeforeReady:!0};export{b as TelegramAuthScreen,b as default,y as TelegramAuthScreenView};