import{proxyCustomElement,HTMLElement,createEvent,h,Host}from"@stencil/core/internal/client";import{a as apiFetch}from"./fetch.js";import{d as defineCustomElement$9}from"./sc-alert2.js";import{d as defineCustomElement$8}from"./sc-button2.js";import{d as defineCustomElement$7}from"./sc-dialog2.js";import{d as defineCustomElement$6}from"./sc-form2.js";import{d as defineCustomElement$5}from"./sc-form-control2.js";import{d as defineCustomElement$4}from"./sc-icon2.js";import{d as defineCustomElement$3}from"./sc-input2.js";import{d as defineCustomElement$2}from"./sc-spinner2.js";import{d as defineCustomElement$1}from"./sc-visually-hidden2.js";const scLoginProviderCss=":host{display:block}",ScLoginProviderStyle0=scLoginProviderCss,ScLoginProvider=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.scSetLoggedIn=createEvent(this,"scSetLoggedIn",7),this.scSetCustomer=createEvent(this,"scSetCustomer",7),this.loggedIn=void 0,this.order=void 0,this.notice=void 0,this.open=void 0,this.loading=void 0,this.error=void 0}handleLoginPrompt(){this.open=!0}handleLoginDialogChange(e){e&&setTimeout((()=>{this.loginForm.querySelector("sc-input").triggerFocus()}),100)}handleLoggedInChange(e,t){!1===t&&e&&(this.notice=!0)}handleOrderChange(e,t){(null==e?void 0:e.updated_at)!==(null==t?void 0:t.updated_at)&&(this.notice=!1)}async handleFormSubmit(e){e.preventDefault(),e.stopImmediatePropagation(),this.error=null;const{login:t,password:o}=await e.target.getFormJson();try{this.loading=!0;const{name:e,email:s}=await apiFetch({method:"POST",path:"surecart/v1/login",data:{login:t,password:o}});this.scSetLoggedIn.emit(!0),this.scSetCustomer.emit({name:e,email:s}),this.open=!1}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.loading=!1}}render(){return h(Host,{key:"4c5e121736909c4e3462f3a518103730c078d55d"},!!this.notice&&h("sc-alert",{key:"c0ca8a0477fc6c21452b25d321ff5a1c5cf24ad4",type:"success",open:!0,style:{marginBottom:"var(--sc-form-row-spacing)"},closable:!0},h("span",{key:"71983897ddbb14fe8371077bef734f677b25ed72",slot:"title"},wp.i18n.__("Welcome back!","surecart")),wp.i18n.__("You have logged in successfully.","surecart")),h("slot",{key:"5181b5b557e88ea9c73dfdc366440120285f7f69"}),!this.loggedIn&&h("sc-dialog",{key:"9e680f662c0be234101926960a75858cbbc9f3d3",label:wp.i18n.__("Login to your account","surecart"),open:this.open,onScRequestClose:()=>this.open=!1},h("sc-form",{key:"46b1dae6809de40d830278ee6c8fe69fef3a4d3c",ref:e=>this.loginForm=e,onScFormSubmit:e=>{e.preventDefault(),e.stopImmediatePropagation()},onScSubmit:e=>this.handleFormSubmit(e)},!!this.error&&h("sc-alert",{key:"64d295f44f8b6236a80d6344ceb899a6a4e475ee",type:"danger",open:!!this.error},this.error),h("sc-input",{key:"8a50f469d7f606debd6b29bf95344e4f1ba85d0b",label:wp.i18n.__("Email or Username","surecart"),type:"text",name:"login",required:!0,autofocus:this.open}),h("sc-input",{key:"2ebc186a50e92e48f355aa651db98a9ee838d0d5",label:wp.i18n.__("Password","surecart"),type:"password",name:"password",required:!0}),h("sc-button",{key:"6dc1d2df070be463e5b90fc007dfc0ce68e48820",type:"primary",full:!0,loading:this.loading,submit:!0},wp.i18n.__("Login","surecart")))))}static get watchers(){return{open:["handleLoginDialogChange"],loggedIn:["handleLoggedInChange"],order:["handleOrderChange"]}}static get style(){return ScLoginProviderStyle0}},[1,"sc-login-provider",{loggedIn:[4,"logged-in"],order:[16],notice:[32],open:[32],loading:[32],error:[32]},[[0,"scLoginPrompt","handleLoginPrompt"]],{open:["handleLoginDialogChange"],loggedIn:["handleLoggedInChange"],order:["handleOrderChange"]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-login-provider","sc-alert","sc-button","sc-dialog","sc-form","sc-form-control","sc-icon","sc-input","sc-spinner","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-login-provider":customElements.get(e)||customElements.define(e,ScLoginProvider);break;case"sc-alert":customElements.get(e)||defineCustomElement$9();break;case"sc-button":customElements.get(e)||defineCustomElement$8();break;case"sc-dialog":customElements.get(e)||defineCustomElement$7();break;case"sc-form":customElements.get(e)||defineCustomElement$6();break;case"sc-form-control":customElements.get(e)||defineCustomElement$5();break;case"sc-icon":customElements.get(e)||defineCustomElement$4();break;case"sc-input":customElements.get(e)||defineCustomElement$3();break;case"sc-spinner":customElements.get(e)||defineCustomElement$2();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$1()}}))}export{ScLoginProvider as S,defineCustomElement as d};