package com.player.musicoo.innertube.utils

import com.player.musicoo.innertube.Innertube
import com.player.musicoo.innertube.models.PlaylistPanelVideoRenderer

fun Innertube.SongItem.Companion.from(renderer: PlaylistPanelVideoRenderer): Innertube.SongItem? {
    return Innertube.SongItem(
        info = Innertube.Info(
            name = renderer
                .title
                ?.text,
            endpoint = renderer
                .navigationEndpoint
                ?.watchEndpoint
        ),
        authors = renderer
            .longBylineText
            ?.splitBySeparator()
            ?.getOrNull(0)
            ?.map(Innertube::Info),
        album = renderer
            .longBylineText
            ?.splitBySeparator()
            ?.getOrNull(1)
            ?.getOrNull(0)
            ?.let(Innertube::Info),
        bigThumbnail = renderer
            .thumbnail
            ?.thumbnails
            ?.let {
                it.getOrNull(5) ?: it.getOrNull(4)
                ?: it.getOrNull(3) ?: it.getOrNull(2)
                ?: it.getOrNull(1) ?: it.getOrNull(0)
            },
        thumbnail = renderer
            .thumbnail
            ?.thumbnails
            ?.getOrNull(0),
        durationText = renderer
            .lengthText
            ?.text
    ).takeIf { it.info?.endpoint?.videoId != null }
}
