/*
"Now playing" slip, top-right: taped paper with cover, title and artist. Visible exactly while a song is playing; hides on pause/stop.
NowPlaying.update(np) np: { playing, title, artist, album, cover (data URL or null) }
Small grace period on hide so skipping to the next song does not make it flicker.
*/
(function () {
const HIDE_GRACE_MS = 1500;
const esc = s => String(s ?? '').replace(/[&<>"]/g, c => ({ '&':'&', '<':'<', '>':'>', '"':'"' }[c]));
let el = null, shown = false, hideTimer = null, key = '';
function root() {
if (!el) {
el = document.createElement('div'); el.id = 'now-playing';
el.innerHTML = `
`;
document.body.appendChild(el);
}
return el;
}
function show() {
clearTimeout(hideTimer);
if (shown) return;
shown = true; el.style.visibility = 'visible';
el.getAnimations().forEach(a => a.cancel());
el.animate([{ transform:'translateY(-140px)', opacity:0 }, { transform:'translateY(6px)', opacity:1, offset:.75 }, { transform:'translateY(0)', opacity:1 }],
{ duration:550, easing:'cubic-bezier(.2,.8,.3,1)' });
}
function hide() {
clearTimeout(hideTimer);
hideTimer = setTimeout(() => {
if (!shown) return;
shown = false;
el.animate([{ transform:'translateY(0)', opacity:1 }, { transform:'translateY(-140px)', opacity:0 }], { duration:450, easing:'ease-in', fill:'forwards' })
.finished.then(() => { if (!shown) el.style.visibility = 'hidden'; }).catch(() => {});
}, HIDE_GRACE_MS);
}
function update(np) {
root();
if (!np || !np.title) { hide(); return; }
const k = `${np.title}|${np.artist}`;
if (k !== key) {
key = k;
el.querySelector('.t').textContent = np.title;
el.querySelector('.a').textContent = np.artist || np.album || '';
el.querySelector('.cover').style.backgroundImage = np.cover ? `url("${np.cover}")` : '';
if (shown) el.querySelector('.slip').animate([{ opacity:.2, transform:'rotate(-1.5deg) translateX(14px)' }, { opacity:1, transform:'rotate(-1.5deg)' }], { duration:350 });
}
np.playing ? show() : hide();
}
window.NowPlaying = { update };
})();