import{r as t,h as i,F as s}from"./p-dfbe004d.js";import{_ as e}from"./p-8415f806.js";import"./p-fd56940f.js";import{a as o}from"./p-7393a06c.js";import"./p-9a7a47e8.js";import"./p-1c2e2695.js";const r=class{constructor(i){t(this,i),this.step=0,this.email="",this.password="",this.verifyCode=""}handleStepChange(){1===this.step&&setTimeout((()=>{var t,i;null===(i=null===(t=this.passwordInput)||void 0===t?void 0:t.triggerFocus)||void 0===i||i.call(t)}),50),2===this.step&&setTimeout((()=>{var t,i;null===(i=null===(t=this.codeInput)||void 0===t?void 0:t.triggerFocus)||void 0===i||i.call(t)}),50)}handleLoadingChange(t){t&&(this.error=null)}handleVerifyCodeChange(t){(null==t?void 0:t.length)>=6&&this.submitCode()}handleError(t){console.error(this.error),this.error=t||{message:e("Something went wrong","surecart")}}async createLoginCode(){try{this.loading=!0,await o({method:"POST",path:"surecart/v1/verification_codes",data:{login:this.email}}),this.step=this.step+1}catch(t){this.handleError(t)}finally{this.loading=!1}}async submitCode(){try{this.loading=!0;const{verified:t}=await o({method:"POST",path:"surecart/v1/verification_codes/verify",data:{login:this.email,code:this.verifyCode}});if(!t)throw{message:e("Verification code is not valid. Please try again.","surecart")};window.location.reload()}catch(t){this.handleError(t),this.loading=!1}}async login(){try{this.loading=!0;const{redirect_url:t}=await o({method:"POST",path:"surecart/v1/login",data:{login:this.email,password:this.password}});t?window.location.replace(t):window.location.reload()}catch(t){this.handleError(t),this.loading=!1}}async checkEmail(){try{this.loading=!0,await o({method:"POST",path:"surecart/v1/check_email",data:{login:this.email}}),this.step=this.step+1}catch(t){this.handleError(t)}finally{this.loading=!1}}renderInner(){return 2===this.step?i(s,null,i("div",{class:"login-form__title",part:"title"},e("Check your email for a confirmation code","surecart")),i("div",null,i("sc-form",{onScFormSubmit:()=>this.submitCode()},i("sc-input",{label:e("Confirmation code","surecart"),type:"text",ref:t=>this.codeInput=t,autofocus:!0,required:!0,onScInput:t=>this.verifyCode=t.target.value}),i("sc-button",{type:"primary",submit:!0,full:!0},i("sc-icon",{name:"lock",slot:"prefix"}),e("Login with Code","surecart"))))):1===this.step&&this.email?i(s,null,i("div",{class:"login-form__title",part:"title"},i("div",null,e("Welcome","surecart")),i("sc-button",{style:{fontSize:"18px"},size:"small",pill:!0,caret:!0,onClick:()=>this.step=this.step-1},i("sc-icon",{name:"user",slot:"prefix"}),this.email)),i("sc-flex",{flexDirection:"column",style:{"--sc-flex-column-gap":"var(--sc-spacing-large)"}},i("div",null,i("sc-form",{onScFormSubmit:()=>this.createLoginCode()},i("sc-button",{class:"login-code",type:"primary",submit:!0,full:!0},i("sc-icon",{name:"mail",slot:"prefix"}),e("Send a login code","surecart"))),i("sc-divider",{style:{"--spacing":"0.5em"}},e("or","surecart")),i("sc-form",{onScFormSubmit:()=>this.login()},i("sc-input",{label:e("Enter your password","surecart"),type:"password",ref:t=>this.passwordInput=t,onKeyDown:t=>"Enter"===t.key&&this.login(),autofocus:!0,required:!0,onScInput:t=>this.password=t.target.value}),i("sc-button",{type:"primary",outline:!0,submit:!0,full:!0},i("sc-icon",{name:"lock",slot:"prefix"}),e("Login","surecart")))))):i(s,null,i("div",{class:"login-form__title",part:"title"},i("slot",{name:"title"})),i("sc-form",{onScFormSubmit:()=>this.checkEmail()},i("sc-input",{type:"text",value:this.email,label:e("Username or Email Address","surecart"),onScInput:t=>this.email=t.target.value,onKeyDown:t=>"Enter"===t.key&&this.checkEmail(),required:!0,autofocus:!0}),i("sc-button",{type:"primary",submit:!0,full:!0},i("sc-icon",{name:"arrow-right",slot:"suffix"}),e("Next","surecart"))))}render(){var t,s;return i("div",{class:"login-form"},i("sc-card",null,!!this.error&&i("sc-alert",{open:!0,type:"danger",closable:!0,onScHide:()=>this.error=null},i("span",{slot:"title",innerHTML:null===(t=this.error)||void 0===t?void 0:t.message}),((null===(s=this.error)||void 0===s?void 0:s.additional_errors)||[]).map((({message:t})=>i("div",{innerHTML:t})))),this.renderInner()),this.loading&&i("sc-block-ui",{spinner:!0,style:{zIndex:"9","--sc-block-ui-opacity":"0.5"}}))}static get watchers(){return{step:["handleStepChange"],loading:["handleLoadingChange"],verifyCode:["handleVerifyCodeChange"]}}};r.style=".login-form{font-size:16px;margin:var(--sc-spacing-xx-large) auto;max-width:400px;position:relative}.login-form__title{margin-bottom:var(--sc-spacing-medium);font-size:var(--sc-font-size-xx-large);font-weight:var(--sc-font-weight-bold);line-height:var(--sc-line-height-dense);text-align:var(--sc-login-text-align, center)}.login-form__back{text-align:center;font-size:var(--sc-font-size-small)}sc-card{--sc-card-padding:var(--sc-spacing-xx-large)}";export{r as sc_login_form}