(function($){typeof define=="function"&&define.amd?define($):$()})(function(){"use strict";const $=(r,t)=>{document.createElement("ha-panel-custom").navigate(r,t)},X=async(r,t)=>{if(!customElements.get(r)){const e=[{url_path:"tmp",component_name:t}],n=document.createElement("partial-panel-resolver");await(n._getRoutes||n.getRoutes).call(n,e).routes.tmp.load()}},Se=async(r,t)=>{let e=null;try{e=await r.callWS({type:"supervisor/api",endpoint:`/addons/${t}/info`,method:"get"})}catch{}return e},Z=r=>(document.cookie=`ingress_session=${r};path=/api/hassio_ingress/;SameSite=Strict${location.protocol==="https:"?";Secure":""}`,r),K=async r=>{const t=await r.callWS({type:"supervisor/api",endpoint:"/ingress/session",method:"post"});return Z(t.session)},_e=async(r,t)=>{await r.callWS({type:"supervisor/api",endpoint:"/ingress/validate_session",method:"post",data:{session:t}}),Z(t)};let g=window.__ingressSession;if(!g){const r=()=>({session:"",refCount:0});let t=r();g=window.__ingressSession={get session(){return t.session},get refCount(){return t.refCount},async init(e){if(!e)return!1;if(t.hass=e,t.timer===void 0){try{t.session=await K(e)}catch{return!1}t.timer===void 0&&(t.timer=setInterval(async()=>{const n=t.hass;try{await _e(n,t.session)}catch{t.session=await K(n)}},6e4),t.refCount=0)}return++t.refCount,!0},fini(){t.timer!==void 0&&(--t.refCount,clearTimeout(t.finiTimer),t.finiTimer=setTimeout(()=>{delete t.finiTimer,t.refCount<=0&&t.timer!==void 0&&(clearInterval(t.timer),t=r())},6e4))}}}const N=r=>(document.cookie=`ha_ingress_session=${r};path=/api/ingress/;SameSite=Strict${location.protocol==="https:"?";Secure":""}`,r),ee=async r=>{const t=await r.callWS({type:"ha-ingress/session"});return N(t.session)},Ee=async(r,t)=>{await r.callWS({type:"ha-ingress/validate_session",session:t}),N(t)};let R=window.__haIngressSession;if(!R){let t={session:""};R=window.__haIngressSession={get session(){return t.session},async init(e){if(!e)return!1;if(t.hass=e,t.timer===void 0){try{t.session=await ee(e)}catch{return!1}t.timer===void 0&&(t.timer=setInterval(async()=>{const n=t.hass;try{await Ee(n,t.session)}catch{t.session=await ee(n)}},6e4))}return!0}}}var F=function(r,t){return F=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,n){e.__proto__=n}||function(e,n){for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(e[i]=n[i])},F(r,t)};function x(r,t){if(typeof t!="function"&&t!==null)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");F(r,t);function e(){this.constructor=r}r.prototype=t===null?Object.create(t):(e.prototype=t.prototype,new e)}function xe(r,t,e,n){function i(o){return o instanceof e?o:new e(function(s){s(o)})}return new(e||(e=Promise))(function(o,s){function a(f){try{u(n.next(f))}catch(d){s(d)}}function c(f){try{u(n.throw(f))}catch(d){s(d)}}function u(f){f.done?o(f.value):i(f.value).then(a,c)}u((n=n.apply(r,t||[])).next())})}function te(r,t){var e={label:0,sent:function(){if(o[0]&1)throw o[1];return o[1]},trys:[],ops:[]},n,i,o,s;return s={next:a(0),throw:a(1),return:a(2)},typeof Symbol=="function"&&(s[Symbol.iterator]=function(){return this}),s;function a(u){return function(f){return c([u,f])}}function c(u){if(n)throw new TypeError("Generator is already executing.");for(;s&&(s=0,u[0]&&(e=0)),e;)try{if(n=1,i&&(o=u[0]&2?i.return:u[0]?i.throw||((o=i.return)&&o.call(i),0):i.next)&&!(o=o.call(i,u[1])).done)return o;switch(i=0,o&&(u=[u[0]&2,o.value]),u[0]){case 0:case 1:o=u;break;case 4:return e.label++,{value:u[1],done:!1};case 5:e.label++,i=u[1],u=[0];continue;case 7:u=e.ops.pop(),e.trys.pop();continue;default:if(o=e.trys,!(o=o.length>0&&o[o.length-1])&&(u[0]===6||u[0]===2)){e=0;continue}if(u[0]===3&&(!o||u[1]>o[0]&&u[1]=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function T(r,t){var e=typeof Symbol=="function"&&r[Symbol.iterator];if(!e)return r;var n=e.call(r),i,o=[],s;try{for(;(t===void 0||t-- >0)&&!(i=n.next()).done;)o.push(i.value)}catch(a){s={error:a}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(s)throw s.error}}return o}function M(r,t,e){if(e||arguments.length===2)for(var n=0,i=t.length,o;n1||c(l,p)})},h&&(i[l]=h(i[l])))}function c(l,h){try{u(n[l](h))}catch(p){v(o[0][3],p)}}function u(l){l.value instanceof I?Promise.resolve(l.value.v).then(f,d):v(o[0][2],l)}function f(l){c("next",l)}function d(l){c("throw",l)}function v(l,h){l(h),o.shift(),o.length&&c(o[0][0],o[0][1])}}function Pe(r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t=r[Symbol.asyncIterator],e;return t?t.call(r):(r=typeof S=="function"?S(r):r[Symbol.iterator](),e={},n("next"),n("throw"),n("return"),e[Symbol.asyncIterator]=function(){return this},e);function n(o){e[o]=r[o]&&function(s){return new Promise(function(a,c){s=r[o](s),i(a,c,s.done,s.value)})}}function i(o,s,a,c){Promise.resolve(c).then(function(u){o({value:u,done:a})},s)}}typeof SuppressedError=="function"&&SuppressedError;function y(r){return typeof r=="function"}function re(r){var t=function(n){Error.call(n),n.stack=new Error().stack},e=r(t);return e.prototype=Object.create(Error.prototype),e.prototype.constructor=e,e}var B=re(function(r){return function(e){r(this),this.message=e?e.length+` errors occurred during unsubscription: `+e.map(function(n,i){return i+1+") "+n.toString()}).join(` `):"",this.name="UnsubscriptionError",this.errors=e}});function Y(r,t){if(r){var e=r.indexOf(t);0<=e&&r.splice(e,1)}}var j=function(){function r(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}return r.prototype.unsubscribe=function(){var t,e,n,i,o;if(!this.closed){this.closed=!0;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=S(s),c=a.next();!c.done;c=a.next()){var u=c.value;u.remove(this)}}catch(p){t={error:p}}finally{try{c&&!c.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}else s.remove(this);var f=this.initialTeardown;if(y(f))try{f()}catch(p){o=p instanceof B?p.errors:[p]}var d=this._finalizers;if(d){this._finalizers=null;try{for(var v=S(d),l=v.next();!l.done;l=v.next()){var h=l.value;try{oe(h)}catch(p){o=o??[],p instanceof B?o=M(M([],T(o)),T(p.errors)):o.push(p)}}}catch(p){n={error:p}}finally{try{l&&!l.done&&(i=v.return)&&i.call(v)}finally{if(n)throw n.error}}}if(o)throw new B(o)}},r.prototype.add=function(t){var e;if(t&&t!==this)if(this.closed)oe(t);else{if(t instanceof r){if(t.closed||t._hasParent(this))return;t._addParent(this)}(this._finalizers=(e=this._finalizers)!==null&&e!==void 0?e:[]).push(t)}},r.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},r.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},r.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&Y(e,t)},r.prototype.remove=function(t){var e=this._finalizers;e&&Y(e,t),t instanceof r&&t._removeParent(this)},r.EMPTY=function(){var t=new r;return t.closed=!0,t}(),r}(),ne=j.EMPTY;function ie(r){return r instanceof j||r&&"closed"in r&&y(r.remove)&&y(r.add)&&y(r.unsubscribe)}function oe(r){y(r)?r():r.unsubscribe()}var se={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},ae={setTimeout:function(r,t){for(var e=[],n=2;n0},enumerable:!1,configurable:!0}),t.prototype._trySubscribe=function(e){return this._throwIfClosed(),r.prototype._trySubscribe.call(this,e)},t.prototype._subscribe=function(e){return this._throwIfClosed(),this._checkFinalizedStatuses(e),this._innerSubscribe(e)},t.prototype._innerSubscribe=function(e){var n=this,i=this,o=i.hasError,s=i.isStopped,a=i.observers;return o||s?ne:(this.currentObservers=null,a.push(e),new j(function(){n.currentObservers=null,Y(a,e)}))},t.prototype._checkFinalizedStatuses=function(e){var n=this,i=n.hasError,o=n.thrownError,s=n.isStopped;i?e.error(o):s&&e.complete()},t.prototype.asObservable=function(){var e=new m;return e.source=this,e},t.create=function(e,n){return new he(e,n)},t}(m),he=function(r){x(t,r);function t(e,n){var i=r.call(this)||this;return i.destination=e,i.source=n,i}return t.prototype.next=function(e){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.next)===null||i===void 0||i.call(n,e)},t.prototype.error=function(e){var n,i;(i=(n=this.destination)===null||n===void 0?void 0:n.error)===null||i===void 0||i.call(n,e)},t.prototype.complete=function(){var e,n;(n=(e=this.destination)===null||e===void 0?void 0:e.complete)===null||n===void 0||n.call(e)},t.prototype._subscribe=function(e){var n,i;return(i=(n=this.source)===null||n===void 0?void 0:n.subscribe(e))!==null&&i!==void 0?i:ne},t}(fe),Ue=function(r){x(t,r);function t(e){var n=r.call(this)||this;return n._value=e,n}return Object.defineProperty(t.prototype,"value",{get:function(){return this.getValue()},enumerable:!1,configurable:!0}),t.prototype._subscribe=function(e){var n=r.prototype._subscribe.call(this,e);return!n.closed&&e.next(this._value),n},t.prototype.getValue=function(){var e=this,n=e.hasError,i=e.thrownError,o=e._value;if(n)throw i;return this._throwIfClosed(),o},t.prototype.next=function(e){r.prototype.next.call(this,this._value=e)},t}(fe),de=new m(function(r){return r.complete()}),pe=function(r){return r&&typeof r.length=="number"&&typeof r!="function"};function Ve(r){return y(r==null?void 0:r.then)}function Re(r){return y(r[G])}function Fe(r){return Symbol.asyncIterator&&y(r==null?void 0:r[Symbol.asyncIterator])}function Be(r){return new TypeError("You provided "+(r!==null&&typeof r=="object"?"an invalid object":"'"+r+"'")+" where a stream was expected. You can provide an Observable, Promise, ReadableStream, Array, AsyncIterable, or Iterable.")}function Ye(){return typeof Symbol!="function"||!Symbol.iterator?"@@iterator":Symbol.iterator}var qe=Ye();function We(r){return y(r==null?void 0:r[qe])}function ze(r){return Ie(this,arguments,function(){var e,n,i,o;return te(this,function(s){switch(s.label){case 0:e=r.getReader(),s.label=1;case 1:s.trys.push([1,,9,10]),s.label=2;case 2:return[4,I(e.read())];case 3:return n=s.sent(),i=n.value,o=n.done,o?[4,I(void 0)]:[3,5];case 4:return[2,s.sent()];case 5:return[4,I(i)];case 6:return[4,s.sent()];case 7:return s.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}})})}function De(r){return y(r==null?void 0:r.getReader)}function O(r){if(r instanceof m)return r;if(r!=null){if(Re(r))return Ge(r);if(pe(r))return Qe(r);if(Ve(r))return Je(r);if(Fe(r))return ve(r);if(We(r))return Xe(r);if(De(r))return Ze(r)}throw Be(r)}function Ge(r){return new m(function(t){var e=r[G]();if(y(e.subscribe))return e.subscribe(t);throw new TypeError("Provided object does not correctly implement Symbol.observable")})}function Qe(r){return new m(function(t){for(var e=0;e>>");let n=t;return e.find((i,o)=>(o===0?n=t.querySelector(e[o]):n instanceof Element&&(n=((n==null?void 0:n.shadowRoot)??n).querySelector(e[o])),n===null)),n===t?null:n}function J(r){return r>1?r:window.innerWidth*r}const ft=(r,t)=>t.length&&r.composedPath().some(e=>e instanceof Element&&e.matches(t.join())),we={haPanelEnergy:{onLock:()=>{const r=C("home-assistant >>> home-assistant-main >>> ha-drawer ha-panel-energy >>> ha-top-app-bar-fixed >>> header");r&&(r.style.top="0px")}}};function ht(){Object.assign(document.body.style,{position:"fixed",inset:0,top:`-${document.documentElement.scrollTop}px`}),Object.values(we).forEach(({onLock:r})=>r==null?void 0:r())}function dt(){const r=Math.abs(parseInt(document.body.style.top))||document.documentElement.scrollTop;Object.assign(document.body.style,{position:"",inset:""}),window.scrollTo({top:r,left:0,behavior:"auto"}),Object.values(we).forEach(({onUnlock:t})=>t==null?void 0:t())}const pt=({startThreshold:r,endThreshold:t,preventOthers:e,lockVerticalScroll:n,exclusions:i})=>_(document,"touchstart",{capture:e}).pipe(A(o=>!ft(o,i)),A(({touches:[{clientX:o}]})=>o{n&&ht(),e&&o.stopPropagation()}),Q(({touches:[{clientY:o}]})=>_(document,"touchmove",{capture:e}).pipe(A(({touches:s})=>s.length<2),V(s=>e&&(s==null?void 0:s.stopPropagation())),lt(_(document,"touchend",{capture:e}).pipe(V(s=>{n&&dt(),e&&(s==null||s.stopPropagation())}),be(1))),ct(1),k(({changedTouches:[{clientX:s,clientY:a}]})=>({x:s,y:a})),A(({x:s,y:a})=>s>Math.abs(a-o)&&s>J(t))))),vt=({preventOthers:r,threshold:t})=>_(document,"touchstart").pipe(V(e=>r&&e.stopPropagation()),Q(({touches:[{clientX:e}]})=>_(document,"touchend").pipe(A(({changedTouches:[{clientX:n}]})=>e-n>J(t)),be(1))));let ge=!1;const yt=r=>{var v,l;if(ge)return!0;const t=C("home-assistant >>> home-assistant-main >>> ha-drawer"),e=C("ha-sidebar",t);if(!e||getComputedStyle(e).display==="none")return!1;if(!r){const h=C("ha-panel-lovelace",t);r=((l=(v=h==null?void 0:h.lovelace)==null?void 0:v.config)==null?void 0:l.sidebar_swipe)||{}}const{start_threshold:n=.1,end_threshold:i=.13,back_threshold:o=50,prevent_others:s=!0,lock_vertical_scroll:a=!0,exclusions:c=[]}=r,u=new Ue(!1),f=vt({threshold:o,preventOthers:s}),d=pt({startThreshold:n,endThreshold:i,preventOthers:s,lockVerticalScroll:a,exclusions:c});return t&&new MutationObserver(h=>{u.next(h[0].oldValue===null)}).observe(t,{attributes:!0,attributeOldValue:!0,attributeFilter:["open"]}),u.pipe(Q(h=>h?f.pipe(k(()=>!1)):d.pipe(k(()=>!0)))).subscribe(h=>{var p;(p=C("home-assistant >>> home-assistant-main"))==null||p.dispatchEvent(new CustomEvent("hass-toggle-menu",{detail:{open:h}}))}),ge=!0,!0};var mt="M2,21V19H20V21H2M20,8V5H18V8H20M20,3A2,2 0 0,1 22,5V8A2,2 0 0,1 20,10H18V13A4,4 0 0,1 14,17H8A4,4 0 0,1 4,13V3H20M16,5H6V13A2,2 0 0,0 8,15H14A2,2 0 0,0 16,13V5Z";const bt=async(r,t,e,n)=>{const i=a=>{n||((r.shadowRoot||r).innerHTML=`
${a}
`)},o=await Se(t,e);if(!o)return i(`Unable to fetch add-on info of '${e}'`);if(!o.ingress_url)return i(`Add-on '${e}' does not support Ingress`);const s=o.ingress_url.replace(/\/+$/,"");return!n&&!await g.init(t)?i("Unable to create an Ingress session"):s};class wt extends HTMLElement{constructor(){super(),this._panelPath="",this.attachShadow({mode:"open"})}connectedCallback(){this._isHassio&&g.init(this._isHassio)}disconnectedCallback(){this._isHassio&&g.fini()}async setProperties(t){this._setProperties&&this._setProperties(t);const e=this._getPanelConfig(t);if(!e)return;const n=await this._getTargetUrl(e,t);n&&await this._createIngressView(n,e,t)}_getPanelConfig(t){var o,s,a;if(!t.panel&&t.route&&(t.panel=(s=(o=t.hass)==null?void 0:o.panels)==null?void 0:s[t.route.prefix.slice(1)]),!t.panel)return;const e=t.panel;let{config:n,url_path:i}=e;if(n.title=e.title,n.icon=e.icon,n.children){const c=(((a=t.route)==null?void 0:a.path)||"").split("/",2)[1];c&&c in n.children&&(n=n.children[c],i=`${i}/${c}`)}if(this._panelPath!==i)return this._panelPath=i,delete this._setProperties,n}async _getTargetUrl(t,e,n){let i="";const o=new URLSearchParams(window.location.search),{url:s,addon:a}=t,c=s===void 0;if(typeof s=="object"){const{match:d,replace:v}=s;if(i=s.default,d){const l=window.location.origin,h=l.replace(new RegExp(`^${d}$`,"i"),v);l!==h&&(i=h)}}else if(c){const d=t.token.value;if(a){const v=await bt(this,e.hass,a,n);if(!v)return;i=v}else await R.init(e.hass),i=`/api/ingress/${d}`}else i=s;n||(this._isHassio&&(g.fini(),delete this._isHassio),a&&(this._isHassio=e.hass,this.isConnected||g.fini()));let{index:u,ui_mode:f}=t;if(u!==void 0){const d=o.get("index");!n&&d&&!/(^|\/)\.\.\//.test(d)&&(u=d.replace(/^\/+/,"")),i=`${i}/${u}`}if(t.token&&!c){const d=new URL(i);d.searchParams.set(t.field,t.token.value),i=d.href}if(n||f==="custom")return i;if(o.has("replace"))a&&(i=`/files/ingress/iframe.html?ingress=${encodeURIComponent(i)}`),window.location.href=i;else if(f==="replace")i.indexOf("://")!==-1&&(window.location.href=i),$(i,{replace:!0});else return i}async _createIngressView(t,e,n){const i=(v,l)=>"setProperties"in v?h=>v.setProperties(h):h=>{for(const p of l)p in h&&(v[p]=h[p])},o=this.shadowRoot,{ui_mode:s,title:a,icon:c}=e;if(s==="custom"){const v={};for(const[l,h]of Object.entries(e.children||{})){const p=await this._getTargetUrl(h,n,!0);if(p){const{title:P,icon:E,ui_mode:H}=h;v[l]={url:p,title:P,icon:E,ui_mode:H}}}try{const{default:l}=await import(t),h=await l({url:t,title:a,icon:c,children:v,ensureHaElem:X});if(!(h instanceof HTMLElement))throw new Error("custom should export default async(config)=>HTMLElement");o.innerHTML="",o.appendChild(h),this._setProperties=i(h,["hass","narrow","route"]),this._setProperties(n)}catch(l){o.innerHTML=`
custom url[${t}] is invalid:
${l}
`}return}let u=` `,f=` iframe { border: 0; width: 100%; height: 100%; display: block; background-color: var(--primary-background-color); } `;const d=s==="toolbar";if(yt()&&(u+='
',f+=` #swipebar { position: fixed; top: 0; width: 20px; height: 100%; } `),d&&(await X("hass-subpage","iframe"),u=`${u} `),o.innerHTML=`${u}`,d){const v=o.querySelector("hass-subpage");this._setButtons(v),v.header=a,this._setProperties=i(v,["hass","narrow"]),this._setProperties(n)}}_setButtons(t){for(const[e,n]of t.querySelectorAll("ha-icon-button").entries())switch(e){case 0:{n.label="Donate",n.path=mt,n.addEventListener("click",()=>{const i=document.createElement("a");i.href="https://buymeacoffee.com/lovelylain",i.target="_blank",i.rel="noreferrer",i.click()});break}}}}customElements.define("ha-panel-ingress",wt)});