import{r as s,j as t}from"./react-vendor-MPLM8hiW.js";import{a as j,u as v,$ as b,L as S,I as C,g as y,aN as k,A as X}from"./index-saDuzM6I.js";import{l as N,L as w}from"./icons-vendor-H2PpUqT3.js";function I({open:m,onClose:o,onVerified:l}){const{t:e}=j(),f=v(n=>n.setToken),[c,i]=s.useState(""),[a,p]=s.useState(!1),[u,r]=s.useState(!1),[x,h]=s.useState(null),g=async n=>{n.preventDefault(),h(null),r(!0);try{const d=await k.stepUp({code:c.trim(),useRecovery:a});f(d.data.token),i(""),await l()}catch(d){h(d instanceof Error?d.message:e("mfa.invalidCode"))}finally{r(!1)}};return t.jsx(b,{open:m,onClose:o,title:e("mfa.stepUpTitle"),children:t.jsxs("form",{onSubmit:g,className:"space-y-4",children:[t.jsxs("p",{className:"text-sm text-text-secondary flex items-start gap-2",children:[t.jsx(N,{className:"h-4 w-4 mt-0.5 shrink-0"}),e("mfa.stepUpDescription")]}),x&&t.jsx("div",{className:"p-3 rounded-lg bg-error-light text-error text-sm",role:"alert",children:x}),t.jsxs("div",{className:"space-y-2",children:[t.jsx(S,{htmlFor:"step-up-code",children:e(a?"mfa.recoveryCode":"mfa.totpCode")}),t.jsx(C,{id:"step-up-code",value:c,onChange:n=>i(n.target.value),inputMode:a?"text":"numeric",autoComplete:"one-time-code",placeholder:a?"XXXXX-XXXXX":"000000",autoFocus:!0})]}),t.jsx("button",{type:"button",className:"text-xs text-primary hover:underline",onClick:()=>{p(n=>!n),i("")},children:e(a?"mfa.useTotpInstead":"mfa.useRecoveryInstead")}),t.jsxs("div",{className:"flex justify-end gap-2 pt-2",children:[t.jsx(y,{type:"button",variant:"ghost",onClick:o,children:e("common.cancel")}),t.jsx(y,{type:"submit",disabled:u||c.length===0,children:u?t.jsx(w,{className:"animate-spin h-4 w-4"}):e("mfa.verify")})]})]})})}function R(){const[m,o]=s.useState(!1),[l,e]=s.useState(null),f=s.useCallback(async a=>{var p,u;try{return await a()}catch(r){if(r instanceof X&&r.status===401&&((u=(p=r.payload)==null?void 0:p.data)==null?void 0:u.code)==="mfaStepUpRequired"){e(()=>a),o(!0);return}throw r}},[]),c=s.useCallback(async()=>{if(l)try{await l()}finally{e(null),o(!1)}},[l]),i=s.useCallback(()=>{e(null),o(!1)},[]);return{run:f,isOpen:m,close:i,retry:l?c:null}}export{I as M,R as u};
