import{r as a,bo as D,bI as R,d0 as T,bJ as U,t as A,d1 as r,j as f,D as F}from"./index-_eM6l4Nf.js";import{L as H,C as I}from"./ConsentForm-Cj-iWc62.js";const J=()=>{const g=D(),l=R(),[i,C]=a.useState(null),[O,E]=a.useState(),[n,u]=a.useState(!1),[L,d]=a.useState(!1),[c,m]=a.useState(),[h]=T(),{code:s,state:o}=a.useMemo(()=>({code:h.get("code"),state:h.get("state")}),[h]),P=U(t=>t.loading.effects.auth.oauthLogin);a.useEffect(()=>{!i&&(!s||!o)&&g(A.auth.login),(async()=>{var S,b,x,v,N,j,k,w;const e=await l.auth.oauthLogin({data:{code:s,state:o}});if(e!=null&&e.error&&(e==null?void 0:e.message)==="Network Error"){r.delete();return}e!=null&&e.error?e.status===412&&e.data?(E(e.data),u(!0),m((S=e.data)!=null&&S.email&&((b=e.data)==null?void 0:b.email.length)>0?(x=e.data)==null?void 0:x.email[0]:void 0)):(C(e),u(!1),d(!1),r.delete()):(v=e.data)!=null&&v.email&&((N=e.data)==null?void 0:N.email.length)>1&&(E(e.data),d(!0),m((j=e.data)!=null&&j.email&&((k=e.data)==null?void 0:k.email.length)>0?(w=e.data)==null?void 0:w.email[0]:void 0))})()},[s,o]);const y=a.useCallback(async()=>{const t=await l.auth.oauthLogin({data:{code:s,state:o,chosenEmail:c,consent:!0}});if(t!=null&&t.error&&(t==null?void 0:t.message)==="Network Error"){r.delete();return}t!=null&&t.error&&(C(t),u(!1),d(!1),r.delete())},[c,s,l.auth,o]);return P||!n&&!L&&i===null?f.jsx(F,{}):f.jsx(H,{error:i,register:!1,login:!0,loginHelp:!0,children:(n||L)&&f.jsx(I,{needConsent:n,remoteUser:O,chosenEmail:c,setChosenEmail:m,handleSubmit:y,onCancel:()=>g(A.auth.login)})})},q=a.memo(J);export{q as default};
