const MakaiNowPlaying = function(userName) {
    const noCoverUrl = 'https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png';
    const fetchTarget = `https://now.flash.moe/get.php?u=${userName}`;
    let lastResult;

    const format = info => {
        const coverUrl = info.images?.large ?? '';

        const result = {
            first: lastResult === undefined,
            changed: false,
            name: info.name,
            now_playing: !!info.nowplaying,
            url: info.url,
            cover: coverUrl === noCoverUrl ? '' : coverUrl,
            artist: {
                name: info.artist?.name ?? '',
                url: info.url.split('/_/')[0]
            },
        };

        if(lastResult === undefined || result.url !== lastResult.url || result.now_playing !== lastResult.now_playing)
            result.changed = true;

        lastResult = result;

        return result;
    };

    return {
        fetch: async () => {
            const { status, body } = await $xhr.get(fetchTarget, { type: 'json' });
            if(status !== 200)
                throw `http ${status}`;

            if(body.length < 1)
                throw 'no data';

            return format(body[0]);
        },
    };
};