import{j as e}from"./query-vendor-CZu0EHPe.js";import{u as z,r as a,L as B}from"./react-vendor-0bvDbF03.js";import{a as W,u as Y,e as K,s as u,S as V,B as b,a5 as k}from"./index-B2Fv47c7.js";import{I as j}from"./input-BqhnYLUV.js";import{A as G}from"./AuthScreenLayout-B1MuJB1Z.js";import{u as H}from"./i18n-vendor-Bg2yf_Zw.js";import{M as J}from"./mail-4BxaQwUf.js";import{A as Q}from"./arrow-left-49fmQP3j.js";import{E as C}from"./eye-off-CQ4sRBEV.js";import{E as R}from"./eye-EUY_y4Vf.js";import"./supabase-vendor-CNV44oIa.js";import"./helmet-vendor-NlTFkN97.js";const ce=()=>{const{t:s}=H("auth"),q=z(),{toast:o}=W(),{user:x}=Y(),{getLocalizedPath:d}=K(),[i,c]=a.useState("request"),[N,F]=a.useState(""),[w,A]=a.useState(""),[S,T]=a.useState(""),[g,U]=a.useState(!1),[y,I]=a.useState(!1),[p,n]=a.useState(!1),[E,_]=a.useState(!1),[h,v]=a.useState(null),[f,P]=a.useState(null);a.useEffect(()=>{new URLSearchParams(window.location.hash.substring(1)).get("type")==="recovery"&&c("set")},[]),a.useEffect(()=>{const{data:{subscription:r}}=u.auth.onAuthStateChange(t=>{t==="PASSWORD_RECOVERY"&&c("set")});return()=>r.unsubscribe()},[]),a.useEffect(()=>{x&&!E&&i==="request"&&(_(!0),(async()=>{n(!0);try{const{error:t}=await u.auth.resetPasswordForEmail(x.email,{redirectTo:`${window.location.origin}${d("/reset-password")}`});if(t){const l=t.message?.toLowerCase().includes("rate limit");o({title:"Error",description:l?s("resetPassword.rateLimited","Please wait a moment before requesting another reset email."):t.message,variant:"destructive"});return}c("sent")}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}})())},[x,E,i,d,s,o]);const $=async r=>{r.preventDefault(),n(!0);try{const{error:t}=await u.auth.resetPasswordForEmail(N,{redirectTo:`${window.location.origin}${d("/reset-password")}`});if(t){const l=t.message?.toLowerCase().includes("rate limit");o({title:"Error",description:l?s("resetPassword.rateLimited","Please wait a moment before requesting another reset email."):t.message,variant:"destructive"});return}c("sent")}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}},D=async r=>{if(r.preventDefault(),v(null),P(null),w.length<6){v(s("resetPassword.passwordTooShort"));return}if(w!==S){P(s("resetPassword.passwordsMismatch"));return}n(!0);try{const{data:t,error:l}=await u.auth.updateUser({password:w});if(l){o({title:"Error",description:l.message,variant:"destructive"});return}o({title:"Password updated!",description:"Your password has been successfully reset."});const m=t?.user;if(m?.email){const M=m.user_metadata?.display_name||m.user_metadata?.name||m.user_metadata?.full_name||"User";u.functions.invoke("send-password-reset-success",{body:{email:m.email,name:M}}).catch(O=>console.error("Failed to send reset success email:",O))}setTimeout(()=>q(d("/stories")),1500)}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}},L={request:{title:s("resetPassword.title","Forgot your password?"),subtitle:s("resetPassword.subtitle","Enter your email and we'll send you a reset link.")},sent:{title:s("resetPassword.checkEmail","Check your email"),subtitle:s("resetPassword.emailSent","We've sent a password reset link to your email. Click the link to set a new password.")},set:{title:s("resetPassword.newPasswordTitle","Set a new password"),subtitle:s("resetPassword.newPasswordSubtitle","Enter your new password below.")}}[i];return e.jsxs(G,{art:i==="sent"?"verify":"reset",title:L.title,subtitle:L.subtitle,children:[e.jsx(V,{title:"Reset Password — StorySparkle",description:"Reset your StorySparkle password securely.",canonical:"/reset-password",noIndex:!0}),i==="request"&&e.jsxs(e.Fragment,{children:[e.jsxs("form",{onSubmit:$,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"reset-email",className:"text-sm font-medium",children:s("login.email","Email")}),e.jsxs("div",{className:"relative",children:[e.jsx(J,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4"}),e.jsx(j,{id:"reset-email",type:"email",placeholder:"you@example.com",className:"pl-10",value:N,onChange:r=>F(r.target.value),required:!0})]})]}),e.jsx(b,{type:"submit",variant:"primary",size:"pill",className:"w-full",disabled:p,children:p?e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"h-4 w-4 border-2 border-white border-t-transparent rounded-full animate-spin"}),s("resetPassword.sending","Sending...")]}):s("resetPassword.sendLink","Send Reset Link")})]}),e.jsx("p",{className:"mt-6 text-center text-sm text-muted-foreground",children:e.jsxs(B,{to:d("/login"),className:"text-nature-coral font-medium hover:underline inline-flex items-center gap-1",children:[e.jsx(Q,{className:"h-3 w-3"}),s("resetPassword.backToLogin","Back to login")]})})]}),i==="sent"&&e.jsxs("div",{className:"text-center space-y-4",children:[e.jsx("p",{className:"text-muted-foreground text-xs",children:s("resetPassword.checkSpam","Didn't receive it? Check your spam folder.")}),e.jsx(b,{variant:"outline",onClick:()=>c("request"),className:"mt-4",children:s("resetPassword.tryAgain","Try a different email")})]}),i==="set"&&e.jsx(e.Fragment,{children:e.jsxs("form",{onSubmit:D,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"new-password",className:"text-sm font-medium",children:s("resetPassword.newPassword","New Password")}),e.jsxs("div",{className:"relative",children:[e.jsx(k,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4"}),e.jsx(j,{id:"new-password",type:g?"text":"password",placeholder:"•••••••",className:`pl-10 pr-10 ${h?"border-destructive focus-visible:ring-destructive":""}`,value:w,onChange:r=>{A(r.target.value),h&&v(null)},required:!0,minLength:6}),e.jsx("button",{type:"button",tabIndex:-1,onClick:()=>U(!g),className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-muted-foreground hover:text-foreground",children:g?e.jsx(C,{className:"h-4 w-4"}):e.jsx(R,{className:"h-4 w-4"})})]}),h&&e.jsx("p",{className:"text-sm text-destructive",role:"alert",children:h})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{htmlFor:"confirm-password",className:"text-sm font-medium",children:s("resetPassword.confirmPassword","Confirm Password")}),e.jsxs("div",{className:"relative",children:[e.jsx(k,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4"}),e.jsx(j,{id:"confirm-password",type:y?"text":"password",placeholder:"•••••••",className:`pl-10 pr-10 ${f?"border-destructive focus-visible:ring-destructive":""}`,value:S,onChange:r=>{T(r.target.value),f&&P(null)},required:!0,minLength:6}),e.jsx("button",{type:"button",tabIndex:-1,onClick:()=>I(!y),className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-muted-foreground hover:text-foreground",children:y?e.jsx(C,{className:"h-4 w-4"}):e.jsx(R,{className:"h-4 w-4"})})]}),f&&e.jsx("p",{className:"text-sm text-destructive",role:"alert",children:f})]}),e.jsx(b,{type:"submit",variant:"primary",size:"pill",className:"w-full",disabled:p,children:p?e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"h-4 w-4 border-2 border-white border-t-transparent rounded-full animate-spin"}),s("resetPassword.updating","Updating...")]}):s("resetPassword.updatePassword","Update Password")})]})})]})};export{ce as default};