import{a as e,i as t,l as n}from"./preact.module-CNakwbc9.js";import{d as r,s as i,t as a}from"./hooks.module-BrXTvcfS.js";import{a as o,i as s}from"./i18n-BaLR3UdQ.js";import{t as c}from"./jsxRuntime.module-Cao1NCKs.js";import{t as l}from"./Button-Du0dYm4Z.js";import{r as u,t as d}from"./Card-DHxlpCRf.js";import{t as f}from"./FormTextBox-BLQGoABI.js";import{i as p,t as m}from"./OptionsRow-CcGZJWUG.js";import{t as h}from"./SetupPage-BAdY9JLT.js";import{t as g}from"./icon-color-DBWzU2L8.js";n(),r();async function _(){await s(window.glob.currentLocale?.id??`en`);let e=document.createElement(`div`);e.classList.add(`setup-outer-wrapper`),document.body.classList.add(`setup`),t(c(v,{}),e),document.body.replaceChildren(e)}function v(){let t=window.glob.login,n=c(`img`,{src:g,alt:``,className:`illustration-logo`}),[r,a]=i(b(t)),[s,u]=i(0);function d(e){a(e),u(e=>e+1)}return t?.ssoEnabled?c(`div`,{class:`setup-container login-container oidc`,children:c(h,{className:`login`,title:o(`login.heading`),illustration:n,error:r,errorId:s,children:c(l,{className:`oidc-login`,onClick:()=>{window.location.href=`/authenticate`},text:c(e,{children:[t.ssoIssuerIcon?c(`img`,{src:t.ssoIssuerIcon,alt:``,onError:e=>{e.currentTarget.style.display=`none`}}):null,o(`login.sign_in_with_sso`,{ssoIssuerName:t.ssoIssuerName??``})]})})})}):c(`div`,{class:`setup-container login-container`,children:c(y,{illustration:n,totpEnabled:t?.totpEnabled??!1,error:r,errorId:s,onError:d})})}function y({illustration:e,totpEnabled:t,error:n,errorId:r,onError:s}){let g=a(null),_=a(null),[v,y]=i(!1),[b,x]=i(!1);async function S(e){if(e.preventDefault(),!b){x(!0);try{let e=new URLSearchParams({password:g.current?.value??``});t&&e.set(`totpToken`,_.current?.value??``),v&&e.set(`rememberMe`,`1`);let n=await fetch(`login`,{method:`POST`,headers:{"Content-Type":`application/x-www-form-urlencoded`},body:e});if(n.ok){window.location.assign(`.`);return}if(n.status===429){s(o(`login.too-many-attempts`));return}(n.status===401?(await n.json().catch(()=>({}))).factor:void 0)===`totp`?s(o(/^.{22}==$/.test(_.current?.value??``)?`login.incorrect-recovery-code`:`login.incorrect-totp`)):s(o(`login.incorrect-password`))}catch{s(o(`login.connection-error`))}finally{x(!1)}}}return c(`form`,{onSubmit:e=>void S(e),children:c(h,{className:`login`,title:o(`login.heading`),illustration:e,error:n,errorId:r,footer:c(l,{text:o(`login.button`),kind:`primary`,disabled:b}),children:c(d,{children:c(u,{children:[c(m,{name:`password`,label:o(`login.password`),stacked:!0,children:c(f,{inputRef:g,autoFocus:!0,type:`password`,name:`password`,autocomplete:`current-password`,required:!0})}),t&&c(m,{name:`totpToken`,label:o(`login.totp-token`),stacked:!0,children:c(f,{inputRef:_,name:`totpToken`,autocomplete:`one-time-code`,required:!0})}),c(p,{name:`rememberMe`,label:o(`login.remember-me`),currentValue:v,onChange:y})]})})})})}function b(e){return e?.ssoError?e.ssoError===`wrong_account`?o(`login.sso-wrong-account`):o(`login.sso-not-enrolled`):e?.ssoConnectionFailed?o(`login.sso-connection-failed`):null}_();export{v as App,y as PasswordLogin};