import{cH as A,cI as e,dp as t,dq as i,cW as a,cJ as r,cK as o,cX as s,s as h,cP as c,dr as l,dl as g,dm as E,y as n,cS as C,ds as Q,c0 as B,cN as u,cL as m,l as w,bj as d,bk as I,bl as f,cO as p,bn as b}from"./card-555679fd.js";import{u as U}from"./media-layout-8e0c974f.js";
/**
 * @license
 * Copyright 2020 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */const k=A(class extends e{constructor(A){if(super(A),A.type!==t.PROPERTY&&A.type!==t.ATTRIBUTE&&A.type!==t.BOOLEAN_ATTRIBUTE)throw Error("The `live` directive is not allowed on child or event bindings");if(!i(A))throw Error("`live` bindings can only contain a single expression")}render(A){return A}update(A,[e]){if(e===a||e===r)return e;const i=A.element,s=A.name;if(A.type===t.PROPERTY){if(e===i[s])return a}else if(A.type===t.BOOLEAN_ATTRIBUTE){if(!!e===i.hasAttribute(s))return a}else if(A.type===t.ATTRIBUTE&&i.getAttribute(s)===e+"")return a;return o(A),e}});class H{constructor(A,e,t,i,a){this._timer=new s,this._timerSeconds=e,this._callback=t,this._timerStartCallback=i,this._timerStopCallback=a,(this._host=A).addController(this)}removeController(){this.stopTimer(),this._host.removeController(this)}get value(){return this._value}updateValue(){this._value=this._callback()}clearValue(){this._value=void 0}stopTimer(){this._timer.isRunning()&&(this._timer.stop(),this._timerStopCallback?.())}startTimer(){this.stopTimer(),this._timerSeconds>0&&(this._timerStartCallback?.(),this._timer.startRepeated(this._timerSeconds,(()=>{this.updateValue(),this._host.requestUpdate()})))}hasTimer(){return this._timer.isRunning()}hostConnected(){this.updateValue(),this.startTimer(),this._host.requestUpdate()}hostDisconnected(){this.clearValue(),this.stopTimer()}}var L="data:image/jpeg;base64,[encoded data, 24004 characters]";let q=class extends h{constructor(){super(...arguments),this._refImage=c(),this._boundVisibilityHandler=this._visibilityHandler.bind(this),this._mediaLoadedInfo=null}async play(){this._cachedValueController?.startTimer()}async pause(){this._cachedValueController?.stopTimer()}async mute(){}async unmute(){}isMuted(){return!0}async seek(A){}async setControls(A){}isPaused(){return!this._cachedValueController?.hasTimer()??!0}async getScreenshotURL(){return this._cachedValueController?.value??null}_getCameraEntity(){return(this.cameraConfig?.camera_entity||this.cameraConfig?.webrtc_card?.entity)??null}shouldUpdate(A){if(!this.hass||"visible"!==document.visibilityState)return!1;const e=this._getCameraEntity();return!A.has("hass")||1!=A.size||"camera"!==this.imageConfig?.mode||!e||!!l(this.hass,A.get("hass"),[e])&&(this._cachedValueController?.clearValue(),!0)}willUpdate(A){A.has("imageConfig")&&(this._cachedValueController&&this._cachedValueController.removeController(),this.imageConfig&&(this._cachedValueController=new H(this,this.imageConfig.refresh_seconds,this._getImageSource.bind(this),(()=>g(this)),(()=>E(this)))),U(this,this.imageConfig?.layout),A.has("imageConfig")&&this.imageConfig?.zoomable&&import("./zoomer-1857311a.js")),(A.has("cameraConfig")||A.has("view")||"camera"===this.imageConfig?.mode&&!this._getAcceptableState(this._getCameraEntity()))&&this._cachedValueController?.clearValue(),this._cachedValueController?.value||this._cachedValueController?.updateValue()}_getAcceptableState(A){const e=(A?this.hass?.states[A]:null)??null;return this.hass&&this.hass.connected&&e&&Date.now()-Date.parse(e.last_updated)<3e5?e:null}connectedCallback(){super.connectedCallback(),document.addEventListener("visibilitychange",this._boundVisibilityHandler),this._cachedValueController?.startTimer()}disconnectedCallback(){this._cachedValueController?.stopTimer(),document.removeEventListener("visibilitychange",this._boundVisibilityHandler),super.disconnectedCallback()}_visibilityHandler(){this._refImage.value&&("hidden"===document.visibilityState?(this._cachedValueController?.stopTimer(),this._cachedValueController?.clearValue(),this._forceSafeImage()):(this._cachedValueController?.startTimer(),this.requestUpdate()))}_buildImageURL(A){const e=new URL(A,document.baseURI);return e.searchParams.append("_t",String(Date.now())),e.toString()}_getImageSource(){if(this.hass&&"camera"===this.imageConfig?.mode){const A=this._getAcceptableState(this._getCameraEntity());if(A?.attributes.entity_picture)return this._buildImageURL(A.attributes.entity_picture)}return"screensaver"!==this.imageConfig?.mode&&this.imageConfig?.url?this._buildImageURL(this.imageConfig.url):L}_forceSafeImage(A){this._refImage.value&&(this._refImage.value.src=!A&&this.imageConfig?.url?this.imageConfig.url:L)}_useZoomIfRequired(A){return this.imageConfig?.zoomable?n` <frigate-card-zoomer> ${A} </frigate-card-zoomer>`:A}render(){const A=this._cachedValueController?.value;return A?this._useZoomIfRequired(n` <img
          ${C(this._refImage)}
          src=${k(A)}
          @load=${A=>{const e=Q(A,{player:this,capabilities:{supportsPause:!!this.imageConfig?.refresh_seconds}});e&&!B(this._mediaLoadedInfo,e)&&(this._mediaLoadedInfo=e,u(this,e))}}
          @error=${()=>{"camera"===this.imageConfig?.mode?this._forceSafeImage(!0):"url"===this.imageConfig?.mode&&m(this,w("error.image_load_error"),{context:this.imageConfig})}}
        />`):n``}static get styles(){return d("img {\n  width: 100%;\n  height: 100%;\n  display: block;\n  object-fit: var(--frigate-card-media-layout-fit, contain);\n  object-position: var(--frigate-card-media-layout-position-x, 50%) var(--frigate-card-media-layout-position-y, 50%);\n}")}};I([f({attribute:!1})],q.prototype,"hass",void 0),I([f({attribute:!1})],q.prototype,"view",void 0),I([f({attribute:!1})],q.prototype,"cameraConfig",void 0),I([f({attribute:!1,hasChanged:p})],q.prototype,"imageConfig",void 0),q=I([b("frigate-card-image")],q);export{q as FrigateCardImage};
