import{o as N,s as x,d as k,e as A,u as O,a as C,q as K,r as i,j as s,h as R,T as t,F as B,k as y,I as P,B as j,L as F,l as L,A as _,m as d,n as M,p as W,N as q}from"./index-DiQX2EYw.js";import{l as H}from"./login_logo-DQELi4uI.js";const l={upper:/[A-Z]/,lower:/[a-z]/,number:/\d/,special:/[^A-Za-z0-9]/},V=N({password:x().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:x().min(1,"setPassword.fields.repeatPassword.required")}).refine(a=>a.password===a.repeatPassword,{message:"setPassword.fields.repeatPassword.mismatch",path:["repeatPassword"]});function z(){return k({resolver:A(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}=O("auth"),w=C(),[v]=K(),b=v.get("token")??"",[I,f]=i.useState(!1),[o,S]=i.useState(!1),[c,E]=i.useState(Z);i.useEffect(()=>{if(!o)return;const r=setInterval(()=>E(m=>Math.max(0,m-1)),1e3);return()=>clearInterval(r)},[o]),i.useEffect(()=>{o&&c===0&&w("/login")},[o,c,w]);const T=async({password:r,repeatPassword:m})=>{f(!0);try{await L("/auth/reset-password",{method:"POST",body:{token:b,newPassword:r,confirmPassword:m},timeoutMs:15e3}),S(!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:g}}=a,p=g.password?.message,u=g.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:I,style:{width:"100%"},testId:"set-password-submit",children:e("setPassword.submit")}),s.jsx(F,{to:"/login",style:{alignSelf:"center"},children:s.jsx(j,{type:"link",children:e("setPassword.backToLogin")})})]})})})}function Y(){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("img",{src:H,alt:"OpenCelium",style:{height:56,width:"auto"}}),s.jsx(J,{})]})})}export{Y as default};
