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