import{i as e,n as t,t as n}from"./jsx-runtime-DG_AJzB_.js";import{n as r}from"./useRouterState-DpT8luFk.js";import{a as i,o as a}from"./api-base-url-DIb6qukm.js";import{t as o}from"./auth-client-CVsUOxFj.js";import{t as s}from"./queryOptions-DgMxBJDn.js";import{t as c}from"./query-timing-CO3WQkW1.js";import{_ as l,d as u,p as d,r as f}from"./schemas-BwLRjo3Z.js";import{t as p}from"./organization-query-keys-DLHrXHGP.js";import{A as m,W as h,j as g}from"./index-BQ5WpFpw.js";import{t as _}from"./auth-bootstrap-query-YDJYu6l2.js";var v=e(t(),1);function y(e){let t=e.organizations.at(0);if(t)return{kind:`organizationHome`,organizationSlug:g(t)};let n=b(e.pendingInvitations);return n?{kind:`invite`,invitationId:n.id}:e.signupMode===`invite-only`?{kind:`inviteOnlyPendingAccess`}:{kind:`onboardingCreateOrg`}}function b(e){return e.length===0?null:[...e].sort(x)[0]??null}function x(e,t){let n=Date.parse(t.createdAt)-Date.parse(e.createdAt);return n===0?e.id.localeCompare(t.id):n}var S=f(u({createdAt:l(),email:d(),expiresAt:l(),id:d(),inviterId:d(),organizationId:d(),organizationName:d(),role:d().nullable().optional(),status:d(),teamId:d().nullable().optional()}));async function C(){let e=await o.listUserInvitations();if(!e.data)throw Error(`Failed to fetch pending invitations`);return S.parse(e.data)}function w(e){return s({queryFn:C,queryKey:p.pendingInvitations(e),staleTime:c})}var T=n(),E=i(`/auth/callback`),D=`Your account is signed in, but you do not have access to an organization yet. Ask an admin to send or resend your invitation link.`;function O(){let e=a(),t=r(),n=(0,v.useRef)(!1),[i,o]=(0,v.useState)(0),[s,c]=(0,v.useState)(`Setting up your account...`),{redirect:l}=E.useSearch(),{auth:u,queryClient:d,refetchSession:f}=E.useRouteContext(),p=h(l);return(0,v.useEffect)(()=>{let r=!0;async function i(){try{let i=u;if(!i.session?.user){if(!n.current){console.warn(`[auth] callback missing session, retrying refetch`),n.current=!0,await f(),r&&o(e=>e+1);return}if(!i.session?.user){console.error(`[auth] callback failed: session is still missing`),r&&await e({to:`/signin`});return}}if(await t.invalidate({sync:!0}),r&&c(`Setting up your account...`),!r)return;if(p&&p.path!==`/auth/callback`){await e({to:p.path});return}let a=i.session?.user.id;if(!a){console.error(`[auth] callback failed: authenticated user is missing`),r&&await e({to:`/signin`});return}let s=await d.fetchQuery({...m(a),staleTime:0}).catch(e=>(console.error(`[auth] callback failed to load organizations`,{error:e}),null));if(!s){r&&await e({to:`/signin`});return}if(!r)return;let l=y({organizations:s,pendingInvitations:s.length===0?await d.fetchQuery({...w(a),staleTime:0}).catch(e=>(console.error(`[auth] callback failed to load pending invitations`,{error:e}),[])):[],signupMode:(await d.fetchQuery({..._(),staleTime:0}).catch(e=>(console.error(`[auth] callback failed to load bootstrap state`,{error:e}),null)))?.signupMode});if(l.kind===`organizationHome`){await e({params:{org_slug:l.organizationSlug},to:`/$org_slug/home`});return}if(l.kind===`invite`){await e({params:{invitationId:l.invitationId},to:`/invite/$invitationId`});return}if(l.kind===`inviteOnlyPendingAccess`){console.warn(`[auth] callback found no organizations or pending invitations in invite-only mode`),r&&c(D);return}console.warn(`[auth] callback found no organizations or pending invitations, onboarding redirect`),await e({to:`/onboarding/create-org`})}catch(t){console.error(`[auth] callback failed unexpectedly`,{error:t}),r&&await e({to:`/signin`})}}return i(),()=>{r=!1}},[u.session?.user.id,e,d,p,i,f,t]),(0,T.jsx)(`div`,{className:`flex min-h-screen items-center justify-center`,children:(0,T.jsx)(`div`,{className:`max-w-md text-center text-muted-foreground`,children:s})})}var k=O;export{k as component};