import{a as x,u as y,d as j,j as e}from"./vendor-query-Brcc40-Y.js";import{d as b,u as N,L as a}from"./vendor-react-DP6BW_9B.js";import{A as E}from"./vendor-utils-ui5PHgZB.js";import{A as d}from"./AuthLayout-DounjrD9.js";import{f as w,i as k,B as $,j as h}from"./index-tN-ddyeb.js";import"./vendor-zustand-CVDiRiMG.js";import"./vendor-form-BQNAly2Z.js";const l="inline-flex h-12 w-full items-center justify-center rounded-xl bg-primary px-6 text-base font-medium text-white hover:bg-primary-hover transition-colors";function A(r){var n;if(r instanceof E){const i=(n=r.response)==null?void 0:n.data;if(Array.isArray(i==null?void 0:i.message))return i.message[0];if(typeof(i==null?void 0:i.message)=="string")return i.message}return r instanceof Error?r.message:"Something went wrong. Please try again."}function B(){const{token:r=""}=b(),n=N(),i=w(m=>m.user),[p,v]=x.useState(!1),u=y({queryKey:["invite-preview",r],queryFn:()=>h.previewInvite(r),enabled:!!r,retry:!1}),s=j({mutationFn:()=>h.acceptInvite(r),onSuccess:({groupId:m})=>{n(`/groups/${m}`,{replace:!0})}}),t=u.data,o=!!i&&!!t&&i.email.toLowerCase()===t.invitedEmail.toLowerCase();x.useEffect(()=>{(t==null?void 0:t.status)==="accepted"&&i&&n(`/groups/${t.groupId}`,{replace:!0})},[t,i,n]),x.useEffect(()=>{!t||p||s.isPending||s.isSuccess||t.isExpired||t.status!=="pending"||o&&(v(!0),s.mutate())},[t,o,p,s.isPending,s.isSuccess]);const c=encodeURIComponent(`/join/${r}`),f=t?`/register?email=${encodeURIComponent(t.invitedEmail)}&redirect=${c}`:`/register?redirect=${c}`,g=t?`/login?email=${encodeURIComponent(t.invitedEmail)}&redirect=${c}`:`/login?redirect=${c}`;return u.isLoading?e.jsx(d,{title:"Join group",subtitle:"Loading invite…",children:e.jsx("div",{className:"flex justify-center py-8",children:e.jsx("div",{className:"h-8 w-8 animate-spin rounded-full border-2 border-primary border-t-transparent"})})}):u.isError||!t?e.jsxs(d,{title:"Invite unavailable",subtitle:"This link is invalid or has expired",children:[e.jsx("p",{className:"text-sm text-ink-muted text-center mb-6",children:"Ask the group admin to send a new invite."}),e.jsx(a,{to:"/",className:l,children:"Go to Divyourday"})]}):t.isExpired||t.status==="expired"||t.status==="cancelled"?e.jsxs(d,{title:"Invite expired",subtitle:`${t.invitedByName} invited you to ${t.groupName}`,children:[e.jsx("p",{className:"text-sm text-ink-muted text-center mb-6",children:"This invite is no longer valid. Ask them to invite you again."}),e.jsx(a,{to:"/",className:l,children:"Go to Divyourday"})]}):e.jsxs(d,{title:`Join ${t.groupName}`,subtitle:`${t.invitedByName} invited you to split expenses together`,children:[e.jsxs("div",{className:"rounded-xl border border-bg-overlay bg-bg-elevated px-4 py-4 mb-6 space-y-1",children:[e.jsx("p",{className:"text-sm text-ink-muted",children:"Invited as"}),e.jsx("p",{className:"text-base font-medium text-ink",children:t.invitedEmail}),t.groupDescription&&e.jsx("p",{className:"text-sm text-ink-muted pt-2",children:t.groupDescription})]}),!i&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-sm text-ink-muted text-center",children:"Create a Divyourday account with this email to join the group."}),e.jsx(a,{to:f,className:l,children:"Create account & join"}),e.jsxs("p",{className:"text-center text-sm text-ink-muted",children:["Already have an account?"," ",e.jsx(a,{to:g,className:"text-primary font-medium hover:text-primary-hover",children:"Sign in"})]})]}),i&&!o&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{className:k("text-sm text-danger bg-danger-subtle rounded-xl px-3 py-2"),children:["You're signed in as ",i.email,". This invite was sent to ",t.invitedEmail,". Sign in with that email to join."]}),e.jsx(a,{to:g,className:l,children:"Sign in with invited email"})]}),i&&o&&e.jsxs("div",{className:"space-y-3",children:[(s.isPending||!s.isError&&!s.isSuccess)&&e.jsx("p",{className:"text-sm text-ink-muted text-center",children:"Joining group…"}),s.isError&&e.jsxs(e.Fragment,{children:[e.jsx("p",{className:"text-sm text-danger bg-danger-subtle rounded-xl px-3 py-2",children:A(s.error)}),e.jsx($,{fullWidth:!0,size:"lg",loading:s.isPending,onClick:()=>s.mutate(),children:"Try again"})]})]})]})}export{B as default};
