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