import{o as N,s as g,d as A,f as k,u as K,a as L,v as O,r as i,j as s,i as R,T as t,F as B,l as y,I as P,B as j,L as C,m as F,A as _,n as d,p as M,q as W,N as q,t as H}from"./index-C7k9Fo_-.js";const l={upper:/[A-Z]/,lower:/[a-z]/,number:/\d/,special:/[^A-Za-z0-9]/},V=N({password:g().min(1,"setPassword.fields.password.required").min(8,"setPassword.fields.password.minLength").max(16,"setPassword.fields.password.maxLength").regex(l.upper,"setPassword.fields.password.upper").regex(l.lower,"setPassword.fields.password.lower").regex(l.number,"setPassword.fields.password.number").regex(l.special,"setPassword.fields.password.special"),repeatPassword:g().min(1,"setPassword.fields.repeatPassword.required")}).refine(a=>a.password===a.repeatPassword,{message:"setPassword.fields.repeatPassword.mismatch",path:["repeatPassword"]});function z(){return A({resolver:k(V),defaultValues:{password:"",repeatPassword:""}})}const U=400,Z=5;function G(a){return a.status!==400?!1:a.body?.error==="INVALID_TOKEN"}function J(){const a=z(),{t:e}=K("auth"),m=L(),[v]=O(),b=v.get("token")??"",[S,f]=i.useState(!1),[o,I]=i.useState(!1),[c,E]=i.useState(Z);i.useEffect(()=>{if(!o)return;const r=setInterval(()=>E(w=>Math.max(0,w-1)),1e3);return()=>clearInterval(r)},[o]),i.useEffect(()=>{o&&c===0&&m("/login")},[o,c,m]);const T=async({password:r,repeatPassword:w})=>{f(!0);try{await F("/auth/reset-password",{method:"POST",body:{token:b,newPassword:r,confirmPassword:w},timeoutMs:15e3}),I(!0)}catch(n){n instanceof _&&G(n)?d.emit({type:"VALIDATION",messageKey:"setPassword.invalidToken"}):n instanceof Error&&n.name===M?d.emit({type:"NETWORK",messageKey:"setPassword.network"}):n instanceof TypeError?d.emit({type:"NETWORK",messageKey:"setPassword.network"}):d.emit({type:"UNKNOWN",messageKey:"setPassword.failed"})}finally{f(!1)}},{control:h,handleSubmit:D,formState:{errors:x}}=a,p=x.password?.message,u=x.repeatPassword?.message;return s.jsx(R,{style:{width:U},children:o?s.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:8,alignItems:"center"},children:[s.jsx(t,{variant:"body",children:e("setPassword.success.message")}),s.jsx(t,{variant:"body",isSubtle:!0,children:e("setPassword.success.redirect",{seconds:c})})]}):s.jsx(B,{...a,children:s.jsxs("form",{onSubmit:D(T),style:{display:"flex",flexDirection:"column",gap:16},children:[s.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:8},children:[s.jsx(t,{variant:"title",as:"h2",children:e("setPassword.title")}),s.jsx(t,{variant:"body",isSubtle:!0,children:e("setPassword.description")})]}),s.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:6},children:[s.jsx(t,{variant:"label",isBold:!0,children:e("setPassword.fields.password.label")}),s.jsx(y,{name:"password",control:h,render:({field:r})=>s.jsx(P,{type:"password",value:r.value??"",onChange:r.onChange,onBlur:r.onBlur,name:r.name,placeholder:e("setPassword.fields.password.placeholder"),error:!!p,autoFocus:!0,testId:"set-password-password"})}),p&&s.jsx(t,{variant:"caption",isDanger:!0,children:e(p)})]}),s.jsxs("div",{style:{display:"flex",flexDirection:"column",gap:6},children:[s.jsx(t,{variant:"label",isBold:!0,children:e("setPassword.fields.repeatPassword.label")}),s.jsx(y,{name:"repeatPassword",control:h,render:({field:r})=>s.jsx(P,{type:"password",value:r.value??"",onChange:r.onChange,onBlur:r.onBlur,name:r.name,placeholder:e("setPassword.fields.repeatPassword.placeholder"),error:!!u,testId:"set-password-repeat"})}),u&&s.jsx(t,{variant:"caption",isDanger:!0,children:e(u)})]}),s.jsx(j,{htmlType:"submit",type:"primary",loading:S,style:{width:"100%"},testId:"set-password-submit",children:e("setPassword.submit")}),s.jsx(C,{to:"/login",style:{alignSelf:"center"},children:s.jsx(j,{type:"link",children:e("setPassword.backToLogin")})})]})})})}function X(){const{isAuthenticated:a}=W();return a?s.jsx(q,{to:"/",replace:!0}):s.jsx("div",{style:{display:"flex",alignItems:"center",justifyContent:"center",minHeight:"100vh",padding:16,background:"var(--color-background-app)"},children:s.jsxs("div",{style:{display:"flex",flexDirection:"column",alignItems:"center",gap:24},children:[s.jsx(H,{}),s.jsx(J,{})]})})}export{X as default};
