let progressInterval=null,progressSpeed=0,lastUpdateTime=Date.now(),currentProgress=0,targetProgress=0,trackDuration=0,currentTrackId=null,trackurl=null;async function updateSpotifyWidget(){try{const e=await fetch("/api/v1/playing");if(!e.ok)return;const n=await e.json();if(n.error||n.message){if(document.getElementById("spotify-song").textContent)return;return document.getElementById("spotify-album-art").src="/assets/img/external/spotify.png",document.getElementById("spotify-album-art").style.cursor="default",document.getElementById("spotify-song").textContent="Not Playing",document.getElementById("spotify-artist").textContent="",document.getElementById("spotify-album").textContent="",document.getElementById("spotify-icon-playing").style.display="none",document.getElementById("spotify-icon-paused").style.display="none",document.getElementById("spotify-icon-stopped").style.display="inline",updateProgressBar(0,1),clearInterval(progressInterval),progressInterval=null,currentProgress=0,currentTrackId=null,void(trackurl=null)}const r=n.spotify;var t=!1;document.getElementById("spotify-song").textContent||(t=!0);const o=r.song_name+r.artist;null!==currentTrackId&¤tTrackId!==o&&(currentProgress=0,document.getElementById("spotify-progress").style.transition="none",document.getElementById("spotify-progress").style.width="0%",document.getElementById("spotify-progress").offsetHeight,document.getElementById("spotify-progress").style.transition="width 0.1s linear"),currentTrackId=o,trackurl=r.url,document.getElementById("spotify-album-art").src=r.album_art,document.getElementById("spotify-album-art").style.cursor="pointer",document.getElementById("spotify-song").textContent=r.song_name,document.getElementById("spotify-artist").textContent=r.artist,document.getElementById("spotify-album").textContent=r.album_name,r.is_playing?(currentProgress=r.progress_ms,trackDuration=r.duration_ms,lastUpdateTime=Date.now(),updateProgressBar(r.progress_ms,r.duration_ms),progressInterval&&clearInterval(progressInterval),progressInterval=setInterval(animateProgressBar,10),document.getElementById("spotify-icon-playing").style.display="inline",document.getElementById("spotify-icon-paused").style.display="none",document.getElementById("spotify-icon-stopped").style.display="none"):(updateProgressBar(r.progress_ms,r.duration_ms),clearInterval(progressInterval),progressInterval=null,currentProgress=r.progress_ms,trackDuration=r.duration_ms,document.getElementById("spotify-icon-playing").style.display="none",document.getElementById("spotify-icon-paused").style.display="inline",document.getElementById("spotify-icon-stopped").style.display="none"),t&&updateVisibility()}catch(t){console.error("Failed to fetch Spotify data",t)}}function updateProgressBar(t,e){if(0===e)return;const n=t/e*100,r=document.getElementById("spotify-progress");r.style.width=n+"%",r.setAttribute("aria-valuenow",t),r.setAttribute("aria-valuemax",e),r.setAttribute("aria-valuemin",0)}function animateProgressBar(){if(!trackDuration)return;const t=Date.now(),e=t-lastUpdateTime;if(lastUpdateTime=t,currentProgress+=e,currentProgress>trackDuration)return clearInterval(progressInterval),void updateSpotifyWidget();updateProgressBar(currentProgress,trackDuration)}updateSpotifyWidget(),setInterval(updateSpotifyWidget,5e3),document.getElementById("spotify-album-art").onclick=()=>{trackurl&&window.open(trackurl,"_blank")},document.getElementById("spotify-icon-playing").onclick=()=>{trackurl&&window.open(trackurl,"_blank")},document.getElementById("spotify-icon-paused").onclick=()=>{trackurl&&window.open(trackurl,"_blank")},document.getElementById("spotify-icon-stopped").onclick=()=>{trackurl&&window.open(trackurl,"_blank")};