import{r as e}from"./rolldown-runtime-Dw2cE7zH.js";import{O as t,k as n}from"./vendor-markdown-DngssFHR.js";import{u as r}from"./vendor-react-Cts2Wj-s.js";import{Q as i,X as a,Y as o,at as s,i as c,nt as l,q as u,rt as d,z as f}from"./vendor-mui-Ckcy4Lfd.js";import"./vendor-md-editor-DC6xr_29.js";import{H as p}from"./index-JB73_g7R.js";var m=e(n(),1),h=t();function g(){let[e]=r(),t=e.get(`client_id`)||``,n=e.get(`redirect_uri`)||``,g=e.get(`response_type`)||``,_=e.get(`code_challenge`)||``,v=e.get(`code_challenge_method`)||``,y=e.get(`state`)||``,b=(()=>{try{return new URL(n).hostname}catch{return n}})(),[x,S]=(0,m.useState)(`checking`),[C,w]=(0,m.useState)(``),[T,E]=(0,m.useState)(``),[D,O]=(0,m.useState)(``),[k,A]=(0,m.useState)(!1),[j,M]=(0,m.useState)(``),N=(0,m.useCallback)(async()=>{try{(await p()).authenticated?S(`consent`):S(`login`)}catch{S(`login`)}},[]);return(0,m.useEffect)(()=>{if(!t||!n||g!==`code`||!_||v!==`S256`){w(`Invalid OAuth parameters`),S(`error`);return}N()},[t,n,g,_,v,N]),x===`checking`?(0,h.jsx)(i,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,minHeight:`100vh`},children:(0,h.jsx)(s,{})}):x===`connecting`?(0,h.jsxs)(i,{sx:{display:`flex`,flexDirection:`column`,alignItems:`center`,justifyContent:`center`,minHeight:`100vh`,gap:2},children:[(0,h.jsx)(s,{}),(0,h.jsx)(l,{color:`text.secondary`,children:`Connecting...`})]}):x===`error`?(0,h.jsx)(i,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,minHeight:`100vh`},children:(0,h.jsx)(o,{sx:{maxWidth:400,width:`100%`,mx:2},children:(0,h.jsxs)(u,{children:[(0,h.jsx)(l,{variant:`h5`,gutterBottom:!0,align:`center`,children:`Authorization Error`}),(0,h.jsx)(d,{severity:`error`,children:C})]})})}):(0,h.jsx)(i,{sx:{display:`flex`,alignItems:`center`,justifyContent:`center`,minHeight:`100vh`},children:(0,h.jsx)(o,{sx:{maxWidth:400,width:`100%`,mx:2},children:(0,h.jsxs)(u,{children:[(0,h.jsx)(l,{variant:`h5`,gutterBottom:!0,align:`center`,children:`Graph Memory`}),x===`login`&&(0,h.jsxs)(h.Fragment,{children:[(0,h.jsxs)(l,{variant:`body2`,color:`text.secondary`,gutterBottom:!0,align:`center`,children:[`Sign in to connect `,(0,h.jsx)(`strong`,{children:b})]}),(0,h.jsxs)(`form`,{onSubmit:async e=>{if(e.preventDefault(),!(!T.trim()||!D)){A(!0),M(``);try{let e=await fetch(`/api/auth/login`,{method:`POST`,headers:{"Content-Type":`application/json`},credentials:`include`,body:JSON.stringify({email:T.trim(),password:D})});e.ok?S(`consent`):M((await e.json().catch(()=>({error:`Login failed`}))).error||`Login failed`)}catch{M(`Connection failed`)}finally{A(!1)}}},children:[(0,h.jsx)(c,{fullWidth:!0,label:`Email`,type:`email`,value:T,onChange:e=>E(e.target.value),margin:`normal`,autoFocus:!0}),(0,h.jsx)(c,{fullWidth:!0,label:`Password`,type:`password`,value:D,onChange:e=>O(e.target.value),margin:`normal`}),j&&(0,h.jsx)(d,{severity:`error`,sx:{mt:1},children:j}),(0,h.jsx)(a,{fullWidth:!0,variant:`contained`,type:`submit`,disabled:k||!T.trim()||!D,sx:{mt:2},children:k?`Signing in...`:`Sign In`})]})]}),x===`consent`&&(0,h.jsxs)(h.Fragment,{children:[(0,h.jsx)(f,{sx:{my:2}}),(0,h.jsxs)(l,{variant:`body1`,align:`center`,sx:{mb:2},children:[`Connect `,(0,h.jsx)(`strong`,{children:b}),` to your Graph Memory account?`]}),(0,h.jsxs)(l,{variant:`body2`,color:`text.secondary`,align:`center`,sx:{mb:3},children:[`This will allow `,b,` to access your graph memory data.`]}),(0,h.jsx)(a,{fullWidth:!0,variant:`contained`,onClick:async()=>{S(`connecting`),w(``);try{let e=await fetch(`/api/oauth/authorize`,{method:`POST`,headers:{"Content-Type":`application/json`},credentials:`include`,body:JSON.stringify({response_type:g,client_id:t,redirect_uri:n,code_challenge:_,code_challenge_method:v,state:y||void 0})});if(!e.ok){let t=await e.json().catch(()=>({error_description:`Authorization failed`}));if(t.error===`login_required`){S(`login`);return}w(t.error_description||t.error||`Authorization failed`),S(`error`);return}let r=await e.json();window.location.href=r.redirectUrl}catch{w(`Connection failed`),S(`error`)}},sx:{mb:1},children:`Connect`}),(0,h.jsx)(a,{fullWidth:!0,variant:`outlined`,onClick:()=>window.history.back(),children:`Cancel`})]})]})})})}export{g as default};