import{j as e}from"./jsx-runtime-u17CrQMm.js";import{r as l,c as y}from"./index-CUcfswc1.js";import{G as S,M as w,P as F,A as C,B as f,s as E,a as p,l as k}from"./api-DaCbsii3.js";function B({googleEnabled:a,microsoftEnabled:i,googleRedirect:t,microsoftRedirect:o}){return!a&&!i?null:e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"auth-divider",children:"o continúa con"}),a&&e.jsxs("a",{href:t,className:"auth-btn-oauth",children:[e.jsx(S,{})," Continuar con Google"]}),i&&e.jsxs("a",{href:o,className:"auth-btn-oauth",children:[e.jsx(w,{})," Continuar con Microsoft"]})]})}function I({bootstrap:a,onSubmit:i,onForgotPassword:t,loading:o,error:s}){const[u,n]=l.useState(a.old.username),[c,m]=l.useState(""),[x,j]=l.useState(!0);function d(r){r.preventDefault(),i(u,c,x)}return e.jsxs(e.Fragment,{children:[e.jsx("h1",{children:"Bienvenido de nuevo"}),e.jsx("p",{className:"auth-sub",children:"Ingresa tus credenciales para continuar con tu jornada."}),a.statusBag&&e.jsx("div",{className:`auth-alert auth-alert-${a.statusBag.type}`,children:a.statusBag.message}),s&&e.jsx("div",{className:"auth-alert auth-alert-danger",children:s}),e.jsxs("form",{onSubmit:d,noValidate:!0,children:[e.jsxs("div",{className:"auth-field",children:[e.jsx("label",{htmlFor:"username",children:"Usuario o correo"}),e.jsx("input",{id:"username",name:"username",type:"text",value:u,placeholder:"usuario",autoComplete:"username",autoFocus:!0,className:s?"auth-input-err":"",onChange:r=>n(r.target.value)})]}),e.jsx(F,{id:"password",label:"Contraseña",value:c,onChange:m,hasError:!!s}),e.jsxs("div",{className:"auth-row-between",children:[e.jsxs("label",{className:"auth-chk",children:[e.jsx("input",{type:"checkbox",checked:x,onChange:r=>j(r.target.checked)}),"Recordarme"]}),e.jsx("button",{type:"button",className:"auth-link-btn",onClick:t,children:"¿Olvidaste la contraseña?"})]}),e.jsxs("button",{className:"auth-btn-primary",type:"submit",disabled:o,children:[o&&e.jsx("span",{className:"auth-spinner"}),o?"Ingresando…":"Iniciar sesión"]})]}),e.jsx(B,{googleEnabled:a.googleLoginEnabled,microsoftEnabled:a.microsoftLoginEnabled,googleRedirect:a.routes.googleRedirect,microsoftRedirect:a.routes.microsoftRedirect}),e.jsxs("p",{className:"auth-footnote",children:["¿Necesitas ayuda con tu usuario? ",e.jsx("a",{href:"mailto:soporte@medforge.io",children:"Contacta a soporte"})]})]})}function R({onSubmit:a,onBack:i,loading:t,error:o}){const[s,u]=l.useState("");function n(c){c.preventDefault(),a(s)}return e.jsxs(e.Fragment,{children:[e.jsx("h1",{children:"Recuperar contraseña"}),e.jsx("p",{className:"auth-sub",children:"Ingresa tu correo y te enviaremos un enlace para restablecerla."}),o&&e.jsx("div",{className:"auth-alert auth-alert-danger",children:o}),e.jsxs("form",{onSubmit:n,noValidate:!0,children:[e.jsxs("div",{className:"auth-field",children:[e.jsx("label",{htmlFor:"forgot-email",children:"Correo"}),e.jsx("input",{id:"forgot-email",name:"email",type:"email",value:s,placeholder:"tu@correo.com",autoComplete:"email",autoFocus:!0,onChange:c=>u(c.target.value)})]}),e.jsxs("button",{className:"auth-btn-primary",type:"submit",disabled:t,children:[t&&e.jsx("span",{className:"auth-spinner"}),t?"Enviando…":"Enviar enlace"]})]}),e.jsx("p",{className:"auth-back-row",children:e.jsx("button",{type:"button",className:"auth-link-btn",onClick:i,children:"← Volver a iniciar sesión"})})]})}function P({bootstrap:a}){const[i,t]=l.useState("login"),[o,s]=l.useState(!1),[u,n]=l.useState(""),[c,m]=l.useState("");async function x(r,h,N){if(!r||!h){n("Ingresa usuario y contraseña.");return}n(""),s(!0);try{const g=await k(a.csrfToken,r,h,N);window.location.href=g.redirect??a.routes.dashboard}catch(g){s(!1),n(g instanceof p?g.message:"No se pudo iniciar sesión. Intenta de nuevo.")}}async function j(r){if(!r){n("Ingresa tu correo.");return}n(""),s(!0);try{await E(a.csrfToken,r),m(r),t("sent")}catch(h){n(h instanceof p?h.message:"No se pudo enviar el enlace. Intenta de nuevo.")}finally{s(!1)}}let d;return i==="sent"?d=e.jsxs(e.Fragment,{children:[e.jsx(f,{}),e.jsx("h1",{style:{marginTop:20},children:"Revisa tu correo"}),e.jsxs("p",{className:"auth-sub",children:["Si ",c," existe en el sistema, enviamos un enlace para restablecer tu contraseña."]}),e.jsx("button",{className:"auth-btn-primary",onClick:()=>{t("login"),m("")},children:"Volver a iniciar sesión"})]}):i==="forgot"?d=e.jsxs(e.Fragment,{children:[e.jsx(f,{}),e.jsx(R,{onSubmit:j,onBack:()=>{t("login"),n("")},loading:o,error:u})]}):d=e.jsxs(e.Fragment,{children:[e.jsx(f,{}),e.jsx(I,{bootstrap:a,onSubmit:x,onForgotPassword:()=>{t("forgot"),n("")},loading:o,error:u})]}),e.jsx(C,{children:d})}function L(){const a=document.getElementById("auth-login-bootstrap");if(!a?.textContent)return null;try{return JSON.parse(a.textContent)}catch{return null}}const b=document.getElementById("auth-login-root"),v=L();b&&v&&y.createRoot(b).render(e.jsx(P,{bootstrap:v}));
