import{r as t,h as s}from"./p-b891c2f1.js";import{_ as i}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{a as r}from"./p-6fd3642a.js";import"./p-112455b1.js";import"./p-1c2e2695.js";const e=class{constructor(s){t(this,s),this.step="",this.email="",this.password=""}handleStepChange(){"password"===this.step&&setTimeout((()=>{this.passwordInput.triggerFocus()}),50)}handleEmailChange(){this.email=this.emailInput.value}async submitMagicLink(){try{this.loading=!0,await r({method:"POST",path:"surecart/v1/customer_links",data:{email:this.email,return_url:window.location.href}}),this.step="complete"}catch(t){this.error=(null==t?void 0:t.message)?t.message:i("Something went wrong","surecart"),console.error(this.error)}finally{this.loading=!1}}async login(){try{this.loading=!0;const{redirect_url:t}=await r({method:"POST",path:"surecart/v1/login",data:{login:this.email,password:this.password}});t?window.location.replace(t):window.location.reload()}catch(t){console.error(this.error),this.loading=!1,this.error=(null==t?void 0:t.message)?t.message:i("Something went wrong","surecart")}}renderInner(){return"complete"===this.step?s("sc-alert",{type:"success",open:!0},s("sc-icon",{slot:"icon",name:"check"}),s("span",{slot:"title"},i("Sent!","surecart")),s("p",null,i("You should receive an email shortly with a link to login.","surecart"))):"password"===this.step&&this.email?s("div",null,s("sc-form",{onScFormSubmit:()=>this.submitMagicLink()},s("sc-button",{type:"primary",submit:!0,full:!0},s("sc-icon",{name:"mail",slot:"prefix"}),i("Send a magic link","surecart"))),s("sc-divider",null,i("or","surecart")),s("sc-form",{onScFormSubmit:()=>this.login()},s("sc-input",{label:i("Enter your password","surecart"),type:"password",ref:t=>this.passwordInput=t,autofocus:!0,required:!0,onScChange:t=>this.password=t.target.value}),s("sc-button",{type:"primary",outline:!0,submit:!0,full:!0},s("sc-icon",{name:"lock",slot:"prefix"}),i("Login","surecart")))):s("sc-form",{onScFormSubmit:()=>this.step="password"},s("sc-input",{ref:t=>this.emailInput=t,label:"Email Address",onScChange:()=>this.handleEmailChange(),required:!0,autofocus:!0,type:"email"}),s("sc-button",{type:"primary",submit:!0,full:!0},s("sc-icon",{name:"arrow-right",slot:"suffix"}),i("Next","surecart")))}renderError(){if(this.error)return s("sc-alert",{open:!0,type:"danger"},s("span",{slot:"title"},i("Error","surecart")),s("span",{innerHTML:this.error}))}render(){return s("div",{class:"login-form"},s("div",{class:"login-form__title",part:"title"},s("slot",{name:"title"})),s("sc-card",null,this.renderInner()),this.loading&&s("sc-block-ui",{spinner:!0}),this.renderError())}static get watchers(){return{step:["handleStepChange"]}}};e.style=".login-form{margin:var(--sc-spacing-xx-large) auto;max-width:400px;position:relative}.login-form__title{text-align:center;margin-bottom:var(--sc-spacing-medium);font-size:var(--sc-font-size-xx-large);font-weight:var(--sc-font-weight-bold)}";export{e as sc_login_form}