'use strict';

const YandexAPI = window.externalAPI;

setupListeners();

function setupListeners() {
	YandexAPI.on(YandexAPI.EVENT_STATE, onEvent);
	YandexAPI.on(YandexAPI.EVENT_TRACK, onEvent);
}

function onEvent() {
	window.postMessage({
		sender: 'web-scrobbler',
		type: 'YANDEX_MUSIC_STATE',
		trackInfo: getTrackInfo(),
		isPlaying: YandexAPI.isPlaying(),
	}, '*');
}

function getTrackInfo() {
	const trackInfo = YandexAPI.getCurrentTrack();

	let track = trackInfo.title;
	const version = trackInfo.version;
	if (version) {
		track = `${track} (${version})`;
	}
	let album = null;
	if (trackInfo.album) {
		album = trackInfo.album.title;
	}

	const trackArt = `https://${trackInfo.cover.replace('%%', '400x400')}`;

	return {
		track, album, trackArt,

		artist: trackInfo.artists[0].title,
		duration: trackInfo.duration,
		currentTime: YandexAPI.getProgress().position,
		uniqueID: trackInfo.link,
	};
}

@@ -445,18 +445,20 @@
 ck();%0A%0A%09
-le
+cons
 t track 
@@ -480,98 +480,8 @@
 le;%0A
-%09const version = trackInfo.version;%0A%09if (version) %7B%0A%09%09track = %60$%7Btrack%7D ($%7Bversion%7D)%60;%0A%09%7D%0A
 %09let

Update yandex-music connector