import{r as c,j as e}from"./react-vendor-MPLM8hiW.js";import{u as y,a as C,h as A,C as x,c as g,d as j,e as w}from"./index-saDuzM6I.js";import{M as N}from"./MfaEnrollmentWizard-B6OHylc2.js";import{M as S}from"./MfaVerifyStage-D51QpfYY.js";import{L as v}from"./icons-vendor-H2PpUqT3.js";import{z as T}from"./vendor-CwCtuXvS.js";import"./tanstack-vendor-Dk_RmDT8.js";import"./radix-vendor-aVdTdbL0.js";import"./clipboard-Boy2u0Jj.js";function H(){const n=T(),i=y(s=>s.setAuth),[u,d]=c.useState(null),[t,k]=c.useState({kind:"loading"}),f=c.useRef(!1),{t:a}=C();return c.useEffect(()=>{if(f.current)return;f.current=!0;const s=window.location.hash.startsWith("#")?window.location.hash.slice(1):window.location.hash,r=new URLSearchParams(s),h=r.get("token"),p=r.get("challenge"),m=r.get("purpose"),o=r.get("redirect");if(p&&(m==="login"||m==="enroll")){k({kind:m==="enroll"?"mfa-enroll":"mfa-verify",challengeToken:p,redirectAfter:o}),window.history.replaceState(null,"",window.location.pathname);return}if(!h){d(a("auth.ssoNoToken"));return}(async()=>{try{i(h,{id:0,email:""});const l=await A.me();i(h,l.data.user),window.history.replaceState(null,"",window.location.pathname),n(o&&o.startsWith("/")?o:"/",{replace:!0})}catch(l){d(l instanceof Error?l.message:a("auth.ssoFailed"))}})()},[n,i,a]),t.kind==="mfa-verify"?e.jsx(S,{challengeToken:t.challengeToken,onCancel:()=>n("/login",{replace:!0}),onSuccess:(s,r)=>{i(s,r),n(t.redirectAfter&&t.redirectAfter.startsWith("/")?t.redirectAfter:"/",{replace:!0})}}):t.kind==="mfa-enroll"?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsxs(x,{className:"w-full max-w-2xl",children:[e.jsx(g,{children:e.jsx(j,{children:a("mfa.enrollmentTitle")})}),e.jsx(w,{children:e.jsx(N,{challengeToken:t.challengeToken,onCompleted:(s,r)=>{if(!r){d(a("auth.ssoFailed"));return}i(s,r),n(t.redirectAfter&&t.redirectAfter.startsWith("/")?t.redirectAfter:"/",{replace:!0})},onCancel:()=>n("/login",{replace:!0})})})]})}):e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsxs(x,{className:"w-full max-w-md",children:[e.jsx(g,{children:e.jsx(j,{children:a("auth.ssoFinishing")})}),e.jsx(w,{children:u?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"p-3 rounded-lg bg-error-light text-error text-sm",role:"alert",children:u}),e.jsx("button",{type:"button",className:"text-sm text-primary underline",onClick:()=>n("/login",{replace:!0}),children:a("auth.backToLogin")})]}):e.jsxs("div",{className:"flex items-center gap-3 text-text-secondary",children:[e.jsx(v,{className:"animate-spin h-5 w-5"}),e.jsx("span",{children:a("common.loading")})]})})]})})}export{H as SSOCallbackPage};
