import{r as t,bw as D,aD as H,cH as R,aL as T,s as A,cI as r,j as f,L as U}from"./index-dP1W1_bd.js";import{L as F,C as I}from"./ConsentForm-CooCqjcY.js";const M=()=>{const g=D(),i=H(),[l,C]=t.useState(null),[O,L]=t.useState(),[n,u]=t.useState(!1),[E,c]=t.useState(!1),[d,m]=t.useState(),[h]=R(),{code:s,state:o}=t.useMemo(()=>({code:h.get("code"),state:h.get("state")}),[h]),P=T(a=>a.loading.effects.auth.oauthLogin);t.useEffect(()=>{!l&&(!s||!o)&&g(A.auth.login),(async()=>{var S,x,v,N,b,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?(L(e.data),u(!0),m((S=e.data)!=null&&S.email&&((x=e.data)==null?void 0:x.email.length)>0?(v=e.data)==null?void 0:v.email[0]:void 0)):(C(e),u(!1),c(!1),r.delete()):(N=e.data)!=null&&N.email&&((b=e.data)==null?void 0:b.email.length)>1&&(L(e.data),c(!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:d,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),c(!1),r.delete())},[d,s,i.auth,o]);return P||!n&&!E&&l===null?f.jsx(U,{}):f.jsx(F,{error:l,register:!1,login:!0,loginHelp:!0,children:(n||E)&&f.jsx(I,{needConsent:n,remoteUser:O,chosenEmail:d,setChosenEmail:m,handleSubmit:y,onCancel:()=>g(A.auth.login)})})},z=t.memo(M);export{z as default};
