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