import{Fragment,h,Host}from"@stencil/core";import apiFetch from"../../../../functions/fetch";import{__}from"@wordpress/i18n";export class ScPasswordNag{constructor(){this.open=!0,this.type="primary",this.successUrl=void 0,this.set=void 0,this.loading=void 0,this.error=void 0,this.success=void 0,this.enableValidation=!0}handleSetChange(){setTimeout((()=>{this.input&&this.input.triggerFocus()}),50)}async dismiss(){this.loading=!0,this.error="";try{await apiFetch({path:"wp/v2/users/me",method:"PATCH",data:{meta:{default_password_nag:!1}}}),this.open=!1}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart"),this.loading=!1}}validatePassword(e){return!!new RegExp("^(?=.*?[#?!@$%^&*-]).{6,}$").test(e)}async handleSubmit(e){this.loading=!0,this.error="";try{const{password:t}=await e.target.getFormJson();await apiFetch({path:"wp/v2/users/me",method:"PATCH",data:{password:t,meta:{default_password_nag:!1}}}),this.dismiss(),this.success=!0}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart"),this.loading=!1}}render(){return this.success?h("sc-alert",{type:"success",open:!0},h("span",{slot:"title"},__("Success!","surecart")),__("You have successfully set your password.","surecart")):h(Host,{tabindex:0,"aria-label":__("You have not yet set a password. Please set a password for your account.","surecart")},h("sc-alert",{type:this.type,open:this.open,exportparts:"base, icon, text, title, message, close-icon",style:{position:"relative"}},!!this.error&&this.error,this.set?h("sc-dashboard-module",{class:"customer-details"},h("span",{slot:"heading"},__("Set A Password","surecart")," "),h("sc-button",{type:"text",size:"small",slot:"end",onClick:()=>this.set=!1},h("sc-icon",{name:"x",slot:"prefix"}),__("Cancel","surecart")),h("sc-card",null,h("sc-form",{onScFormSubmit:e=>this.handleSubmit(e)},h("sc-password",{enableValidation:this.enableValidation,label:__("New Password","surecart"),name:"password",confirmation:!0,ref:e=>this.input=e,required:!0}),h("div",null,h("sc-button",{type:"primary",full:!0,submit:!0,busy:this.loading},__("Set Password","surecart")))))):h(Fragment,null,h("slot",{name:"title",slot:"title"},__("Reminder","surecart")),h("slot",null,__("You have not yet set a password. Please set a password for your account.","surecart")),h("sc-flex",{"justify-content":"flex-start"},h("sc-button",{size:"small",type:"primary",onClick:()=>this.set=!0},__("Set A Password","surecart")),h("sc-button",{size:"small",type:"text",onClick:()=>this.dismiss()},__("Dismiss","surecart")))),this.loading&&h("sc-block-ui",{spinner:!0})))}static get is(){return"sc-password-nag"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-password-nag.css"]}}static get styleUrls(){return{$:["sc-password-nag.css"]}}static get properties(){return{open:{type:"boolean",mutable:!0,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"open",reflect:!1,defaultValue:"true"},type:{type:"string",mutable:!1,complexType:{original:"'primary' | 'success' | 'info' | 'warning' | 'danger'",resolved:'"danger" | "info" | "primary" | "success" | "warning"',references:{}},required:!1,optional:!1,docs:{tags:[],text:"The type of alert."},attribute:"type",reflect:!0,defaultValue:"'primary'"},successUrl:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The success url."},attribute:"success-url",reflect:!1},enableValidation:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Ensures strong password validation."},attribute:"enable-validation",reflect:!0,defaultValue:"true"}}}static get states(){return{set:{},loading:{},error:{},success:{}}}static get watchers(){return[{propName:"set",methodName:"handleSetChange"}]}}