import{r as t,bo as D,bH as H,c$ as R,bI as T,q as A,d0 as r,j as f,D as U}from"./index-46ma1THH.js";import{L as $,C as q}from"./ConsentForm-Y93Ip5-s.js";const F=()=>{const g=D(),i=H(),[l,C]=t.useState(null),[O,E]=t.useState(),[n,u]=t.useState(!1),[L,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,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),c(!1),r.delete()):(v=e.data)!=null&&v.email&&((N=e.data)==null?void 0:N.email.length)>1&&(E(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&&!L&&l===null?f.jsx(U,{}):f.jsx($,{error:l,register:!1,login:!0,loginHelp:!0,children:(n||L)&&f.jsx(q,{needConsent:n,remoteUser:O,chosenEmail:d,setChosenEmail:m,handleSubmit:y,onCancel:()=>g(A.auth.login)})})},z=t.memo(F);export{z as default};
