import{j as e}from"./query-vendor-CZu0EHPe.js";import{u as O,r as a,L as E}from"./react-vendor-0bvDbF03.js";import{a as $,u as W,s as m,S as Y,B as y,a2 as k}from"./index-BnG94z5-.js";import{I as v}from"./input-B1L7dKkC.js";import{u as K}from"./i18n-vendor-Bg2yf_Zw.js";import{M as V}from"./mail-BZCaXzxQ.js";import{A as z}from"./arrow-left-BmbJfZmf.js";import{C as G}from"./circle-check-big-CFjeGhZo.js";import{E as C}from"./eye-off-C3rq1Uhf.js";import{E as L}from"./eye-Cc8SEXRC.js";import"./supabase-vendor-CNV44oIa.js";import"./helmet-vendor-NlTFkN97.js";const ie=()=>{const{t:s}=K("auth"),R=O(),{toast:o}=$(),{user:w}=W(),[l,d]=a.useState("request"),[N,F]=a.useState(""),[u,q]=a.useState(""),[P,T]=a.useState(""),[f,U]=a.useState(!1),[g,A]=a.useState(!1),[h,n]=a.useState(!1),[S,_]=a.useState(!1),[x,b]=a.useState(null),[p,j]=a.useState(null);a.useEffect(()=>{new URLSearchParams(window.location.hash.substring(1)).get("type")==="recovery"&&d("set")},[]),a.useEffect(()=>{const{data:{subscription:r}}=m.auth.onAuthStateChange(t=>{t==="PASSWORD_RECOVERY"&&d("set")});return()=>r.unsubscribe()},[]),a.useEffect(()=>{w&&!S&&l==="request"&&(_(!0),(async()=>{n(!0);try{const{error:t}=await m.auth.resetPasswordForEmail(w.email,{redirectTo:`${window.location.origin}/reset-password`});if(t){const i=t.message?.toLowerCase().includes("rate limit");o({title:"Error",description:i?s("resetPassword.rateLimited","Please wait a moment before requesting another reset email."):t.message,variant:"destructive"});return}d("sent")}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}})())},[w,S,l]);const I=async r=>{r.preventDefault(),n(!0);try{const{error:t}=await m.auth.resetPasswordForEmail(N,{redirectTo:`${window.location.origin}/reset-password`});if(t){const i=t.message?.toLowerCase().includes("rate limit");o({title:"Error",description:i?s("resetPassword.rateLimited","Please wait a moment before requesting another reset email."):t.message,variant:"destructive"});return}d("sent")}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}},D=async r=>{if(r.preventDefault(),b(null),j(null),u.length<6){b(s("resetPassword.passwordTooShort"));return}if(u!==P){j(s("resetPassword.passwordsMismatch"));return}n(!0);try{const{data:t,error:i}=await m.auth.updateUser({password:u});if(i){o({title:"Error",description:i.message,variant:"destructive"});return}o({title:"Password updated!",description:"Your password has been successfully reset."});const c=t?.user;if(c?.email){const B=c.user_metadata?.display_name||c.user_metadata?.name||c.user_metadata?.full_name||"User";m.functions.invoke("send-password-reset-success",{body:{email:c.email,name:B}}).catch(M=>console.error("Failed to send reset success email:",M))}setTimeout(()=>R("/stories"),1500)}catch(t){o({title:"Error",description:t.message||"Something went wrong",variant:"destructive"})}finally{n(!1)}};return e.jsxs("div",{className:"min-h-screen flex items-center justify-center px-4 py-12 bg-page-surface",children:[e.jsx(Y,{title:"Reset Password — StorySparkle",description:"Reset your StorySparkle password securely.",canonical:"/reset-password",noIndex:!0}),e.jsx("div",{className:"bg-white rounded-2xl shadow-xl overflow-hidden max-w-md w-full border border-border",children:e.jsxs("div",{className:"p-6 sm:p-8",children:[e.jsx("div",{className:"flex justify-center mb-8",children:e.jsxs(E,{to:"/",className:"flex items-center gap-2",children:[e.jsx("img",{src:"https://cdn.storysparkle.org/logo_assets/favicon.webp",alt:"StorySparkle Logo",width:32,height:32,decoding:"async",className:"h-8 w-8"}),e.jsxs("span",{className:"font-bold text-xl",children:[e.jsx("span",{className:"text-nature-red",children:"Story"}),e.jsx("span",{className:"text-nature-coral",children:"Sparkle"})]})]})}),l==="request"&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-2xl font-bold mb-2 text-center",children:s("resetPassword.title","Forgot your password?")}),e.jsx("p",{className:"text-muted-foreground text-center text-sm mb-6",children:s("resetPassword.subtitle","Enter your email and we'll send you a reset link.")}),e.jsxs("form",{onSubmit:I,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(V,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4"}),e.jsx(v,{id:"reset-email",type:"email",placeholder:"you@example.com",className:"pl-10",value:N,onChange:r=>F(r.target.value),required:!0})]})]}),e.jsx(y,{type:"submit",className:"w-full bg-nature-red hover:bg-nature-coral",disabled:h,children:h?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"}),"Sending..."]}):s("resetPassword.sendLink","Send Reset Link")})]}),e.jsx("p",{className:"mt-6 text-center text-sm text-muted-foreground",children:e.jsxs(E,{to:"/login",className:"text-nature-red font-medium hover:underline inline-flex items-center gap-1",children:[e.jsx(z,{className:"h-3 w-3"}),s("resetPassword.backToLogin","Back to login")]})})]}),l==="sent"&&e.jsxs("div",{className:"text-center space-y-4",children:[e.jsx("div",{className:"flex justify-center",children:e.jsx(G,{className:"h-16 w-16 text-nature-forest"})}),e.jsx("h1",{className:"text-2xl font-bold",children:s("resetPassword.checkEmail","Check your email")}),e.jsx("p",{className:"text-muted-foreground text-sm",children:s("resetPassword.emailSent","We've sent a password reset link to your email. Click the link to set a new password.")}),e.jsx("p",{className:"text-muted-foreground text-xs",children:s("resetPassword.checkSpam","Didn't receive it? Check your spam folder.")}),e.jsx(y,{variant:"outline",onClick:()=>d("request"),className:"mt-4",children:s("resetPassword.tryAgain","Try a different email")})]}),l==="set"&&e.jsxs(e.Fragment,{children:[e.jsx("h1",{className:"text-2xl font-bold mb-2 text-center",children:s("resetPassword.newPasswordTitle","Set a new password")}),e.jsx("p",{className:"text-muted-foreground text-center text-sm mb-6",children:s("resetPassword.newPasswordSubtitle","Enter your new password below.")}),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(v,{id:"new-password",type:f?"text":"password",placeholder:"•••••••",className:`pl-10 pr-10 ${x?"border-destructive focus-visible:ring-destructive":""}`,value:u,onChange:r=>{q(r.target.value),x&&b(null)},required:!0,minLength:6}),e.jsx("button",{type:"button",tabIndex:-1,onClick:()=>U(!f),className:"absolute right-3 top-1/2 transform -translate-y-1/2 text-muted-foreground hover:text-foreground",children:f?e.jsx(C,{className:"h-4 w-4"}):e.jsx(L,{className:"h-4 w-4"})})]}),x&&e.jsx("p",{className:"text-sm text-destructive",role:"alert",children:x})]}),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(v,{id:"confirm-password",type:g?"text":"password",placeholder:"•••••••",className:`pl-10 pr-10 ${p?"border-destructive focus-visible:ring-destructive":""}`,value:P,onChange:r=>{T(r.target.value),p&&j(null)},required:!0,minLength:6}),e.jsx("button",{type:"button",tabIndex:-1,onClick:()=>A(!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(L,{className:"h-4 w-4"})})]}),p&&e.jsx("p",{className:"text-sm text-destructive",role:"alert",children:p})]}),e.jsx(y,{type:"submit",className:"w-full bg-nature-red hover:bg-nature-coral",disabled:h,children:h?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"}),"Updating..."]}):s("resetPassword.updatePassword","Update Password")})]})]})]})})]})};export{ie as default};
//# sourceMappingURL=ResetPasswordPage-DqsCoMey.js.map