import{LitElement as t}from"../../lit/index.js";class JwtLogin extends t{constructor(){super(),this.windowControllers=new AbortController,this.auto=!1,this.method="GET",this.body={},this.key="jwt",this.jwt=null,this.ready=!1}lastErrorChanged(t){t&&"logout"!=this.__context&&(console.warn(t),this.dispatchEvent(new CustomEvent("jwt-login-refresh-error",{composed:!0,bubbles:!0,cancelable:!1,detail:{value:t}})))}static get tag(){return"jwt-login"}static get properties(){return{auto:{type:Boolean},refreshUrl:{type:String,attribute:"refresh-url"},redirectUrl:{type:String,attribute:"redirect-url"},logoutUrl:{type:String,attribute:"logout-url"},url:{type:String},method:{type:String},body:{type:Object},key:{type:String},jwt:{type:String}}}updated(t){super.updated&&super.updated(t),t.forEach(((t,e)=>{["auto","method","url"].includes(e)&&this.url&&!this.jwt&&this.ready&&this.auto&&(clearTimeout(this.__debounce),this.__debounce=setTimeout((()=>{this.generateRequest(this.url,this.body)}),0)),"jwt"==e&&(this._jwtChanged(this[e],t),this.dispatchEvent(new CustomEvent("jwt-changed",{detail:{value:this[e]}})))}))}_jwtChanged(t,e){if(null!=t&&""!=t&&"null"!=t||void 0===e){if(t){try{localStorage.setItem(this.key,JSON.stringify(t))}catch(t){}this.dispatchEvent(new CustomEvent("jwt-token",{bubbles:!0,cancelable:!0,composed:!0,detail:t})),this.dispatchEvent(new CustomEvent("jwt-logged-in",{bubbles:!0,cancelable:!0,composed:!0,detail:!0}))}}else localStorage.removeItem(this.key),this.dispatchEvent(new CustomEvent("jwt-logged-in",{bubbles:!0,cancelable:!0,composed:!0,detail:!1}))}connectedCallback(){super.connectedCallback(),globalThis.addEventListener("jwt-login-refresh-token",this.requestRefreshToken.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("jwt-login-toggle",this.toggleLogin.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("jwt-login-login",this.loginRequest.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("jwt-login-logout",this.logoutRequest.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){this.windowControllers.abort(),super.disconnectedCallback()}firstUpdated(t){super.firstUpdated&&super.firstUpdated(t),this.ready=!0;try{this.jwt=JSON.parse(localStorage.getItem(this.key))}catch(t){this.jwt=null}}requestRefreshToken(t){this.__context="refresh",t.detail.element&&(this.__element=t.detail.element),this.generateRequest(this.refreshUrl)}generateRequest(t,e={}){let i={method:this.method,headers:{"Content-Type":"application/json"}};"GET"!=this.method&&(i.body=JSON.stringify(e)),fetch(t,i).then((t=>{if(t.ok)return t.json();"logout"==this.__context&&this.__redirect&&this.redirectUrl?setTimeout((()=>{globalThis.location.href=this.redirectUrl}),100):"login"==this.__context&&this.dispatchEvent(new CustomEvent("jwt-login-login-failed",{bubbles:!0,cancelable:!0,composed:!0,detail:!0})),this.lastErrorChanged(t)})).then((t=>{try{const e=t;e&&(e.jwt?this.loginResponse(e.jwt):this.loginResponse(e))}catch(t){console.warn(t)}}))}toggleLogin(t){null==this.jwt?this.loginRequest(t):this.logoutRequest(t)}loginRequest(t){this.__context="login",this.body=t.detail,this.generateRequest(this.url,this.body)}logoutRequest(t){this.__context="logout",this.__redirect=t.detail.redirect,this.body={},this.jwt=null,this.generateRequest(this.logoutUrl)}loginResponse(t){switch(this.__context){case"login":this.jwt=t;break;case"refresh":this.jwt=t,this.__element&&(this.__element.obj[this.__element.callback](this.jwt,...this.__element.params),this.__element=!1);break;case"logout":this.__redirect&&this.redirectUrl&&setTimeout((()=>{globalThis.location.href=this.redirectUrl}),100)}}}customElements.define(JwtLogin.tag,JwtLogin);export{JwtLogin};