import{u as C,a as E,r,j as e,N as z,M as S,i as D,B as I}from"./index-DvBxzriB.js";import{A as j}from"./arrow-left-DrHDfZP0.js";function V({onBack:f}){const{pendingVerification:g,verifyEmail:N,resendVerification:u,signOut:o}=C(),[t,v]=r.useState(""),[h,m]=r.useState(""),[p,b]=r.useState(!1),[i,s]=r.useState(!1);if(!g)return null;const c=async d=>{d.preventDefault(),m(""),b(!0);try{await N(t.trim())}catch(w){m(w.message??"Invalid code")}finally{b(!1)}},l=async()=>{s(!1);try{await u(),s(!0)}catch(d){m(d.message??"Failed to resend")}};return e.jsx("div",{className:"fixed inset-0 z-50 bg-black/60 backdrop-blur-sm flex items-center justify-center px-4",children:e.jsxs("div",{className:"w-full max-w-sm space-y-6 bg-surface rounded-xl p-6 border border-border",children:[e.jsxs("div",{className:"text-center space-y-3",children:[e.jsx("div",{className:"flex justify-center",children:e.jsx("div",{className:"w-14 h-14 rounded-full bg-accent/15 flex items-center justify-center",children:e.jsx(S,{size:24,className:"text-accent"})})}),e.jsx("h1",{className:"text-lead font-semibold",children:"Check your email"}),e.jsxs("p",{className:"text-micro text-muted",children:["We sent a 6-digit code to ",e.jsx("span",{className:"text-text",children:g.email})]})]}),e.jsxs("form",{onSubmit:c,className:"space-y-4",children:[e.jsx("input",{type:"text",inputMode:"numeric",placeholder:"Enter 6-digit code",value:t,onChange:d=>v(d.target.value.replace(/\D/g,"").slice(0,6)),maxLength:6,autoFocus:!0,className:"w-full px-3 py-3 bg-bg border border-border rounded-lg text-text text-center text-body tracking-[0.3em] font-mono focus:outline-none focus:border-accent"}),e.jsx("button",{type:"submit",disabled:t.length!==6||p,className:"w-full py-2 bg-accent hover:bg-accent-hover text-bg font-medium rounded-lg text-micro transition-colors disabled:opacity-50",children:p?"Verifying...":"Verify Email"}),h&&e.jsx("p",{className:"text-danger text-micro text-center",children:h})]}),e.jsxs("div",{className:"text-center space-y-2",children:[e.jsx("button",{onClick:l,className:"text-micro text-muted hover:text-accent bg-transparent border-none p-0",children:i?"Code sent!":"Didn't get the code? Resend"}),e.jsx("br",{}),e.jsxs("button",{onClick:f??o,className:"text-micro text-muted hover:text-text bg-transparent border-none p-0 inline-flex items-center gap-1",children:[e.jsx(j,{size:12})," ",f?"Go back":"Use a different email"]})]})]})})}function F(){const{user:f,signIn:g}=C(),N=E(),[u,o]=r.useState("email"),[t,v]=r.useState(""),[h,m]=r.useState(""),[p,b]=r.useState(""),[i,s]=r.useState(""),[c,l]=r.useState(!1);if(f)return e.jsx(z,{to:"/chat",replace:!0});const d=async a=>{a.preventDefault(),s(""),l(!0);try{const n=await fetch("/functions/auth/request",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t,...D()?{mode:"otp"}:{}})}),x=await n.json();if(!n.ok)throw new Error(x.error??"Failed to request sign-in");x.method==="password"?o("password"):x.method==="otp"?o("enter-otp"):o("check-inbox")}catch(n){s(n.message??"Something went wrong")}finally{l(!1)}},w=async a=>{a.preventDefault(),s(""),l(!0);try{await g(t,h)}catch(n){s(n.message??"Sign in failed")}finally{l(!1)}},k=async a=>{a.preventDefault(),s(""),l(!0);try{const n=await fetch("/functions/auth/verify-otp",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t,code:p})}),x=await n.json();if(!n.ok)throw new Error(x.error??"Invalid code");const y=I.auth;y._setToken(x.token),y._user=x.user,y._pendingVerification=null,y._notify()}catch(n){s(n.message??"Verification failed")}finally{l(!1)}};return e.jsx("div",{className:"relative min-h-screen flex items-center justify-center px-4",children:e.jsxs("div",{className:"relative z-10 text-center space-y-8 w-full max-w-sm",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsx("h1",{className:"text-hero font-bold text-accent",children:"Para"}),e.jsx("p",{className:"text-micro text-muted",children:"Sign in to your Para"})]}),u==="email"&&e.jsxs("form",{onSubmit:d,className:"space-y-4",children:[e.jsx("input",{type:"email",inputMode:"email",placeholder:"your email",value:t,onChange:a=>v(a.target.value),required:!0,autoFocus:!0,autoComplete:"email",className:"w-full px-3 py-3 bg-bg border border-border rounded-lg text-text text-body focus:outline-none focus:border-accent"}),e.jsx("button",{type:"submit",disabled:!t||c,className:"w-full py-3 bg-accent hover:bg-accent-hover text-bg font-semibold rounded-lg text-body transition-colors disabled:opacity-40",children:c?"...":"Continue"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i}),e.jsx("button",{type:"button",onClick:()=>N("/"),className:"text-micro text-muted hover:text-text bg-transparent border-none p-0",children:"Don't have a handle yet? Claim one"})]}),u==="password"&&e.jsxs("form",{onSubmit:w,className:"space-y-4",children:[e.jsxs("p",{className:"text-micro text-muted",children:["Signing in as ",e.jsx("span",{className:"text-text",children:t})]}),e.jsx("input",{type:"password",placeholder:"Password",value:h,onChange:a=>m(a.target.value),required:!0,autoFocus:!0,minLength:6,autoComplete:"current-password",className:"w-full px-3 py-3 bg-bg border border-border rounded-lg text-text text-body focus:outline-none focus:border-accent"}),e.jsx("button",{type:"submit",disabled:c,className:"w-full py-3 bg-accent hover:bg-accent-hover text-bg font-semibold rounded-lg text-body transition-colors disabled:opacity-40",children:c?"...":"Sign In"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i}),e.jsxs("button",{type:"button",onClick:()=>{o("email"),m(""),s("")},className:"text-micro text-muted hover:text-text bg-transparent border-none p-0 inline-flex items-center gap-1",children:[e.jsx(j,{size:12})," Use a different email"]})]}),u==="enter-otp"&&e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"w-14 h-14 mx-auto rounded-full bg-accent/15 flex items-center justify-center",children:e.jsx(S,{size:24,className:"text-accent"})}),e.jsx("h2",{className:"text-body font-semibold text-text",children:"Check your email"}),e.jsxs("p",{className:"text-micro text-muted",children:["We sent a 6-digit code to ",e.jsx("span",{className:"text-text",children:t})]}),e.jsxs("form",{onSubmit:k,className:"space-y-4",children:[e.jsx("input",{type:"text",inputMode:"numeric",placeholder:"Enter 6-digit code",value:p,onChange:a=>b(a.target.value.replace(/\D/g,"").slice(0,6)),maxLength:6,autoFocus:!0,className:"w-full px-3 py-3 bg-bg border border-border rounded-lg text-text text-center text-body tracking-[0.3em] font-mono focus:outline-none focus:border-accent"}),e.jsx("button",{type:"submit",disabled:p.length!==6||c,className:"w-full py-3 bg-accent hover:bg-accent-hover text-bg font-semibold rounded-lg text-body transition-colors disabled:opacity-40",children:c?"...":"Sign In"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i})]}),e.jsxs("button",{onClick:()=>{o("email"),b(""),s("")},className:"text-micro text-muted hover:text-text bg-transparent border-none p-0 inline-flex items-center gap-1",children:[e.jsx(j,{size:12})," Use a different email"]})]}),u==="check-inbox"&&e.jsxs("div",{className:"space-y-4",children:[e.jsx("div",{className:"w-14 h-14 mx-auto rounded-full bg-accent/15 flex items-center justify-center",children:e.jsx(S,{size:24,className:"text-accent"})}),e.jsx("h2",{className:"text-body font-semibold text-text",children:"Check your inbox"}),e.jsxs("p",{className:"text-micro text-muted",children:["If ",e.jsx("span",{className:"text-text",children:t})," is registered, we sent a sign-in link. It expires in 15 minutes."]}),e.jsxs("button",{onClick:()=>{o("email"),s("")},className:"text-micro text-muted hover:text-text bg-transparent border-none p-0 inline-flex items-center gap-1",children:[e.jsx(j,{size:12})," Use a different email"]})]})]})})}export{F as ChatAuthPage,V as VerificationOverlay};