export const __rspack_esm_id="27092";export const __rspack_esm_ids=["27092"];export const __webpack_modules__={8927(e,s,t){t.d(s,{createExternalAuth:()=>d});t(33110);var n=t(32464);class a{async attach(){window.externalBus=e=>this.receiveMessage(e),window.addEventListener("connection-status",e=>this.fireMessage({type:"connection-status",payload:{event:e.detail}})),this.config=await this.sendMessage({type:"config/get"})}addCommandHandler(e){this._commandHandler=e}sendMessage(e){const s=++this.msgId;return e.id=s,this._sendExternal(e),new Promise((e,t)=>{this.commands[s]={resolve:e,reject:t}})}fireMessage(e){e.id||(e.id=++this.msgId),this._sendExternal(e)}receiveMessage(e){if("command"===e.type){if(!this._commandHandler||!this._commandHandler(e)){let s,t;this._commandHandler?(s="not_ready",t="Command handler not ready"):(s="unknown_command",t=`Unknown command ${e.command}`),console.warn(t,e),this.fireMessage({id:e.id,type:"result",success:!1,error:{code:s,message:t}})}return}const s=this.commands[e.id];s?"result"===e.type&&(e.success?s.resolve(e.result):s.reject(e.error)):console.warn("Received unknown msg ID",e.id)}_sendExternal(e){window.externalApp?window.externalApp.externalBus(JSON.stringify(e)):window.webkit.messageHandlers.externalBus.postMessage(e)}constructor(){this.commands={},this.msgId=0}}const r="externalAuthSetToken",o="externalAuthRevokeToken";if(!window.externalApp&&!window.webkit)throw new Error("External auth requires either externalApp or webkit defined on Window object.");class i extends n.Nj{async refreshAccessToken(e){if(this._tokenCallbackPromise&&!e)try{return void await this._tokenCallbackPromise}catch(e){this._tokenCallbackPromise=void 0}const s={callback:r};e&&(s.force=!0),this._tokenCallbackPromise=new Promise((e,s)=>{window[r]=(t,n)=>t?e(n):s(n)}),await Promise.resolve(),window.externalApp?window.externalApp.getExternalAuth(JSON.stringify(s)):window.webkit.messageHandlers.getExternalAuth.postMessage(s);const t=await this._tokenCallbackPromise;this.data.access_token=t.access_token,this.data.expires=1e3*t.expires_in+Date.now(),this._tokenCallbackPromise=void 0}async revoke(){const e={callback:o},s=new Promise((e,s)=>{window[o]=(t,n)=>t?e(n):s(n)});await Promise.resolve(),window.externalApp?window.externalApp.revokeExternalAuth(JSON.stringify(e)):window.webkit.messageHandlers.revokeExternalAuth.postMessage(e),await s}constructor(e){super({hassUrl:e,clientId:"",refresh_token:"",access_token:"",expires_in:0,expires:0})}}const d=async e=>{const s=new i(e);return(window.externalApp?.externalBus||window.webkit&&window.webkit.messageHandlers.externalBus)&&(s.external=new a,await s.external.attach()),s}}};
//# sourceMappingURL=27092.79462ce887800ab3.js.map