import{r as t,bx as R,bW as T,db as U,bX as D,x as A,dc as r,j as f,L as F}from"./index-C6OVXshM.js";import{L as H,C as M}from"./ConsentForm-Da6I9Kuc.js";const W=()=>{const g=R(),l=T(),[i,C]=t.useState(null),[O,E]=t.useState(),[n,u]=t.useState(!1),[L,d]=t.useState(!1),[c,m]=t.useState(),[h]=U(),{code:s,state:o}=t.useMemo(()=>({code:h.get("code"),state:h.get("state")}),[h]),P=D(a=>a.loading.effects.auth.oauthLogin);t.useEffect(()=>{!i&&(!s||!o)&&g(A.auth.login),(async()=>{var x,S,b,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((x=e.data)!=null&&x.email&&((S=e.data)==null?void 0:S.email.length)>0?(b=e.data)==null?void 0:b.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=t.useCallback(async()=>{const a=await l.auth.oauthLogin({data:{code:s,state:o,chosenEmail:c,consent:!0}});if(a!=null&&a.error&&(a==null?void 0:a.message)==="Network Error"){r.delete();return}a!=null&&a.error&&(C(a),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(M,{needConsent:n,remoteUser:O,chosenEmail:c,setChosenEmail:m,handleSubmit:y,onCancel:()=>g(A.auth.login)})})},q=t.memo(W);export{q as default};
