import{u as C,a as E,r,j as e,N as z,i as D,M as S,B as O}from"./index-CgMptIqm.js";import{A as j}from"./arrow-left-BK77bEV_.js";function I({onBack:f}){const{pendingVerification:g,verifyEmail:N,resendVerification:x,signOut:u}=C(),[t,v]=r.useState(""),[h,m]=r.useState(""),[p,b]=r.useState(!1),[i,s]=r.useState(!1);if(!g)return null;const o=async l=>{l.preventDefault(),m(""),b(!0);try{await N(t.trim())}catch(w){m(w.message??"Invalid code")}finally{b(!1)}},c=async()=>{s(!1);try{await x(),s(!0)}catch(l){m(l.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:o,className:"space-y-4",children:[e.jsx("input",{type:"text",inputMode:"numeric",placeholder:"Enter 6-digit code",value:t,onChange:l=>v(l.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:c,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??u,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(),[x,u]=r.useState("email"),[t,v]=r.useState(""),[h,m]=r.useState(""),[p,b]=r.useState(""),[i,s]=r.useState(""),[o,c]=r.useState(!1);if(f)return e.jsx(z,{to:"/chat",replace:!0});const l=async a=>{a.preventDefault(),s(""),c(!0);try{const n=await fetch("/functions/auth/request",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t})}),d=await n.json();if(!n.ok)throw new Error(d.error??"Failed to request sign-in");u(d.method==="password"?"password":d.method==="magic-link"?"check-inbox":"enter-otp")}catch(n){s(n.message??"Something went wrong")}finally{c(!1)}},w=async a=>{a.preventDefault(),s(""),c(!0);try{await g(t,h)}catch(n){s(n.message??"Sign in failed")}finally{c(!1)}},k=async a=>{a.preventDefault(),s(""),c(!0);try{const n=await fetch("/functions/auth/verify-otp",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:t,code:p})}),d=await n.json();if(!n.ok)throw new Error(d.error??"Invalid code");const y=O.auth;y._setToken(d.token),y._user=d.user,y._pendingVerification=null,y._notify()}catch(n){s(n.message??"Verification failed")}finally{c(!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"})]}),x==="email"&&e.jsxs("form",{onSubmit:l,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||o,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:o?"...":"Continue"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i}),!D()&&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"})]}),x==="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:o,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:o?"...":"Sign In"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i}),e.jsxs("button",{type:"button",onClick:()=>{u("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"]})]}),x==="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}),". Enter it below, or just tap the sign-in link in that email."]}),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||o,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:o?"...":"Sign In"}),i&&e.jsx("p",{className:"text-danger text-micro text-center",children:i})]}),e.jsxs("button",{onClick:()=>{u("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"]})]}),x==="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:()=>{u("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,I as VerificationOverlay};