"use strict";(self.webpackChunkhome_assistant_frontend=self.webpackChunkhome_assistant_frontend||[]).push([["27092"],{8927:function(e,n,s){s.d(n,{createExternalAuth:function(){return d}});s(16280),s(33110),s(3362);var t=s(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 n=++this.msgId;return e.id=n,this._sendExternal(e),new Promise((e,s)=>{this.commands[n]={resolve:e,reject:s}})}fireMessage(e){e.id||(e.id=++this.msgId),this._sendExternal(e)}receiveMessage(e){if("command"===e.type){if(!this._commandHandler||!this._commandHandler(e)){let n,s;this._commandHandler?(n="not_ready",s="Command handler not ready"):(n="unknown_command",s=`Unknown command ${e.command}`),console.warn(s,e),this.fireMessage({id:e.id,type:"result",success:!1,error:{code:n,message:s}})}return}const n=this.commands[e.id];n?"result"===e.type&&(e.success?n.resolve(e.result):n.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",i="externalAuthRevokeToken";if(!window.externalApp&&!window.webkit)throw new Error("External auth requires either externalApp or webkit defined on Window object.");class o extends t.Nj{async refreshAccessToken(e){if(this._tokenCallbackPromise&&!e)try{return void(await this._tokenCallbackPromise)}catch(t){this._tokenCallbackPromise=void 0}const n={callback:r};e&&(n.force=!0),this._tokenCallbackPromise=new Promise((e,n)=>{window[r]=(s,t)=>s?e(t):n(t)}),await Promise.resolve(),window.externalApp?window.externalApp.getExternalAuth(JSON.stringify(n)):window.webkit.messageHandlers.getExternalAuth.postMessage(n);const s=await this._tokenCallbackPromise;this.data.access_token=s.access_token,this.data.expires=1e3*s.expires_in+Date.now(),this._tokenCallbackPromise=void 0}async revoke(){const e={callback:i},n=new Promise((e,n)=>{window[i]=(s,t)=>s?e(t):n(t)});await Promise.resolve(),window.externalApp?window.externalApp.revokeExternalAuth(JSON.stringify(e)):window.webkit.messageHandlers.revokeExternalAuth.postMessage(e),await n}constructor(e){super({hassUrl:e,clientId:"",refresh_token:"",access_token:"",expires_in:0,expires:0})}}const d=async e=>{var n;const s=new o(e);return(null!==(n=window.externalApp)&&void 0!==n&&n.externalBus||window.webkit&&window.webkit.messageHandlers.externalBus)&&(s.external=new a,await s.external.attach()),s}}}]);
//# sourceMappingURL=27092.05f6673b4777c03c.js.map