import{r as n,j as e}from"./react-vendor-MPLM8hiW.js";import{z as E,A as F,B as m,C as A,E as P}from"./vendor-CwCtuXvS.js";import{u as g,a as M,b as q,s as z,C as j,c as b,d as w,e as v,B,L as y,I as N,f as I,g as R,h as K}from"./index-saDuzM6I.js";import{M as W}from"./MfaEnrollmentWizard-B6OHylc2.js";import{M as $}from"./MfaVerifyStage-D51QpfYY.js";import{l as H,L as V,aK as D,aL as G}from"./icons-vendor-H2PpUqT3.js";import"./tanstack-vendor-Dk_RmDT8.js";import"./radix-vendor-aVdTdbL0.js";import"./clipboard-Boy2u0Jj.js";function se(){const o=E(),[k]=F(),d=g(s=>s.setAuth),[x,h]=n.useState(k.get("error")),[u,C]=n.useState([]),[l,c]=n.useState({kind:"credentials"}),{t:r}=M();q(),n.useEffect(()=>{let s=!1;return(async()=>{try{const a=await z.publicList();s||C(a.data??[])}catch{}})(),()=>{s=!0}},[]);const S=n.useMemo(()=>m.object({identifier:m.string().min(1,r("validation.required")),password:m.string().min(1,r("validation.required"))}),[r]),{register:f,handleSubmit:T,formState:{errors:t,isSubmitting:p}}=A({resolver:P(S)}),L=async s=>{try{h(null);const i=(await K.login(s.identifier,s.password)).data;if("mfaRequired"in i){c(i.mfaPurpose==="enroll"?{kind:"mfa-enroll",challengeToken:i.challengeToken}:{kind:"mfa-verify",challengeToken:i.challengeToken});return}d(i.token,i.user),o("/")}catch(a){h(a instanceof Error?a.message:r("auth.invalidCredentials"))}};return l.kind==="mfa-verify"?e.jsx($,{challengeToken:l.challengeToken,onCancel:()=>c({kind:"credentials"}),onSuccess:(s,a)=>{d(s,a),o("/")}}):l.kind==="mfa-enroll"?e.jsxs(j,{className:"w-full max-w-2xl mx-auto my-8",children:[e.jsx(b,{children:e.jsxs(w,{className:"flex items-center gap-2",children:[e.jsx(H,{className:"h-5 w-5"})," ",r("mfa.enrollmentTitle")]})}),e.jsxs(v,{children:[e.jsx("p",{className:"text-sm text-text-secondary mb-4",children:r("mfa.enrollmentRequiredDesc")}),e.jsx(W,{challengeToken:l.challengeToken,onCompleted:(s,a)=>{a?d(s,a):g.getState().setToken(s),o("/")},onCancel:()=>c({kind:"credentials"})})]})]}):e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-background p-4",children:e.jsxs(j,{className:"w-full max-w-md",children:[e.jsxs(b,{className:"space-y-1 text-center",children:[e.jsx("div",{className:"flex justify-center mb-4",children:e.jsx(B,{size:"lg"})}),e.jsx(w,{className:"text-2xl",children:r("dashboard.welcome")})]}),e.jsxs("form",{onSubmit:T(L),children:[e.jsxs(v,{className:"space-y-4",children:[x&&e.jsx("div",{className:"p-3 rounded-lg bg-error-light text-error text-sm",role:"alert",children:x}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(y,{htmlFor:"identifier",children:r("auth.email")}),e.jsx(N,{id:"identifier",type:"text",placeholder:r("auth.identifierPlaceholder"),autoComplete:"username","aria-invalid":!!t.identifier||void 0,"aria-describedby":t.identifier?"identifier-error":void 0,...f("identifier")}),t.identifier&&e.jsx("p",{id:"identifier-error",className:"text-sm text-error",role:"alert",children:t.identifier.message})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(y,{htmlFor:"password",children:r("auth.password")}),e.jsx(N,{id:"password",type:"password",placeholder:"••••••••",autoComplete:"current-password","aria-invalid":!!t.password||void 0,"aria-describedby":t.password?"password-error":void 0,...f("password")}),t.password&&e.jsx("p",{id:"password-error",className:"text-sm text-error",role:"alert",children:t.password.message})]})]}),e.jsxs(I,{className:"flex flex-col gap-3",children:[e.jsx(R,{type:"submit",className:"w-full",disabled:p,children:p?e.jsxs(e.Fragment,{children:[e.jsx(V,{className:"animate-spin"}),r("common.loading")]}):e.jsxs(e.Fragment,{children:[e.jsx(D,{}),r("auth.signIn")]})}),u.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center gap-3 w-full text-text-secondary text-xs",children:[e.jsx("div",{className:"flex-1 border-t border-border"}),e.jsx("span",{children:r("auth.orContinueWith")}),e.jsx("div",{className:"flex-1 border-t border-border"})]}),e.jsx("div",{className:"w-full space-y-2",children:u.map(s=>e.jsxs("a",{href:`/api/admin/auth/sso/${s.slug}/login`,className:"w-full inline-flex items-center justify-center gap-2 rounded-md border border-border bg-background px-4 py-2 text-sm font-medium hover:bg-surface-hover transition-colors",children:[e.jsx(G,{className:"h-4 w-4"}),s.buttonLabel??s.name]},s.id))})]})]})]})]})})}export{se as LoginPage};
