import{o as e}from"./rolldown-runtime.js";import{n as t,t as n}from"./jsx-runtime.js";import{Mt as r,j as i}from"./useActiveWallet-CEs8euQ3.js";import{k as a}from"./context-Cdw68BC2.js";import"./index-CKYEOQWW.js";import{t as o}from"./modal-context-DHfp-mFQ.js";import{i as s,n as c,r as l,t as u}from"./to-ui-error-84asheVg.js";var d=n(),f=e(t(),1);i();var p={component:()=>{let{user:e}=r(),t=a(),n=(0,f.useMemo)((()=>e?.mfaMethods.filter((e=>e!==`passkey`||!t.globalDisablePasskeys))??[]),[e?.mfaMethods,t.globalDisablePasskeys]),{data:i}=o(),[p,m]=(0,f.useState)(n[0]),[h,g]=(0,f.useState)(!1),[_,v]=(0,f.useState)(),[y,b]=(0,f.useState)();if((0,f.useEffect)((()=>{m(n[0])}),[n]),!i?.mfaVerify)throw Error(`Missing modal data for MFA verification screen.`);let{onFailure:x,onSuccess:S,generateOptions:C,verifyTotpCode:w,verifyPasskey:T,verifySmsCode:E,sendSmsCode:D}=i.mfaVerify,O=async e=>{if(e!==`passkey`)try{m(e),e===`sms`&&(m(e),await D()),e===`totp`&&m(e)}catch(e){console.error(e)}else try{m(e);let t=await C();if(!t)throw Error(`something went wrong`);v(t),await T(t),g(!0),b(void 0),S()}catch(e){b(l(e))}},k=async e=>{b(void 0);try{if(!e||!p)return;if(p===`passkey`){if(!_)throw Error(`Missing passkey challenge`);await T(_)}else p===`sms`?await E(e):p===`totp`&&await w(e);b(void 0),g(!0),S()}catch(e){throw l(e).error}},A=()=>{y||!h?x(y?.error??Error(`Canceled MFA verification.`)):S()},j=(0,f.useRef)(!1);return(0,f.useEffect)((()=>{!j.current&&p&&(j.current=!0,O(p).finally((()=>{j.current=!1})))}),[open]),e?p===`passkey`?(0,d.jsx)(u,{account:e.linkedAccounts.filter((e=>e.type===`passkey`&&e.enrolledInMfa)).sort(((e,t)=>t.firstVerifiedAt.valueOf()-e.firstVerifiedAt.valueOf()))[0],submitSuccess:h,hasBlockingError:y?.isBlocking??!1,error:y?.error,onClose:A,onBack:()=>{m(void 0),b(void 0)},handleSubmit:()=>k(_).catch(b)}):p===`sms`||p===`totp`?(0,d.jsx)(c,{selectedMethod:p,submitSuccess:h,hasBlockingError:y?.isBlocking??!1,handleSubmitCode:k,onClose:A,onBack:n.length>1?()=>m(void 0):void 0}):(0,d.jsx)(s,{mfaMethods:n,onSelect:O,handleClose:A}):null}};export{p as MfaAuthVerifyFlowScreen,p as default};