import{o as A,s as j,d as _,e as P,f as S,u as w,r as u,j as e,h as O,T as i,L as f,B as d,F as R,i as M,k as v,I as N,l as b,A as F,m as n,n as L,p as C,N as D}from"./index-DiQX2EYw.js";import{l as K}from"./login_logo-DQELi4uI.js";const g=A({email:j().min(1,"fields.email.required").email("fields.email.invalid")});function k(){const s=_({resolver:P(g),defaultValues:{email:""}}),r={email:S(g,"email")};return{form:s,constraints:r}}const Y=400,V={EMAIL_NOT_EXISTS:"VALIDATION",TOO_MANY_ATTEMPTS:"VALIDATION",EMAIL_RECOVERY_FAILED:"SERVER"},B={EMAIL_NOT_EXISTS:"forgotPassword.emailNotExists",TOO_MANY_ATTEMPTS:"forgotPassword.tooManyAttempts",EMAIL_RECOVERY_FAILED:"forgotPassword.emailRecoveryFailed"};function W(s){if(!s||typeof s!="object")return null;const r=s.error;return r==="EMAIL_NOT_EXISTS"||r==="TOO_MANY_ATTEMPTS"||r==="EMAIL_RECOVERY_FAILED"?r:null}function H(){const{form:s,constraints:r}=k(),{t}=w("auth"),[p,m]=u.useState(!1),[E,h]=u.useState(!1),y=async({email:o})=>{m(!0);try{await b("/auth/forgot-password",{method:"POST",body:{email:o},timeoutMs:15e3}),h(!0)}catch(a){if(a instanceof F){const l=W(a.body);l?n.emit({type:V[l],messageKey:B[l],...l==="EMAIL_RECOVERY_FAILED"&&{durationSec:12}}):n.emit({type:"UNKNOWN",messageKey:"forgotPassword.failed"})}else a instanceof Error&&a.name===L?n.emit({type:"NETWORK",messageKey:"forgotPassword.network"}):a instanceof TypeError?n.emit({type:"NETWORK",messageKey:"forgotPassword.network"}):n.emit({type:"UNKNOWN",messageKey:"forgotPassword.failed"})}finally{m(!1)}},{control:x,handleSubmit:T,formState:{errors:I}}=s,c=I.email?.message;return e.jsx(O,{style:{width:Y},children:E?e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:16,alignItems:"center"},children:[e.jsx(i,{variant:"body",children:t("forgotPassword.success")}),e.jsx(f,{to:"/login",children:e.jsx(d,{type:"link",children:t("forgotPassword.backToLogin")})})]}):e.jsx(R,{...s,children:e.jsx(M,{constraints:r,children:e.jsxs("form",{onSubmit:T(y),style:{display:"flex",flexDirection:"column",gap:16},children:[e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:8},children:[e.jsx(i,{variant:"title",as:"h2",children:t("forgotPassword.title")}),e.jsx(i,{variant:"body",isSubtle:!0,children:t("forgotPassword.description")})]}),e.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:6},children:[e.jsx(i,{variant:"label",isBold:!0,children:t("fields.email.label")}),e.jsx(v,{name:"email",control:x,render:({field:o})=>e.jsx(N,{type:"text",value:o.value??"",onChange:o.onChange,onBlur:o.onBlur,name:o.name,placeholder:t("fields.email.placeholder"),error:!!c,autoFocus:!0,testId:"forgot-password-email"})}),c&&e.jsx(i,{variant:"caption",isDanger:!0,children:t(c)})]}),e.jsx(d,{htmlType:"submit",type:"primary",loading:p,style:{width:"100%"},testId:"forgot-password-submit",children:t("forgotPassword.submit")}),e.jsx(f,{to:"/login",style:{alignSelf:"center"},children:e.jsx(d,{type:"link",children:t("forgotPassword.backToLogin")})})]})})})})}function q(){const{isAuthenticated:s}=C();return s?e.jsx(D,{to:"/",replace:!0}):e.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",minHeight:"100vh",padding:16,background:"var(--color-background-app)"},children:e.jsxs("div",{style:{display:"flex",flexDirection:"column",alignItems:"center",gap:24},children:[e.jsx("img",{src:K,alt:"OpenCelium",style:{height:56,width:"auto"}}),e.jsx(H,{})]})})}export{q as default};
