async function searchResults(keyword) {
    try {
        const response = await fetchv2(`https://api-v2.soundcloud.com/search/tracks?q=${encodeURIComponent(keyword)}&facet=genre&user_id=618848-625616-52355-612546&client_id=xwYTVSni6n4FghaI0c4uJ8T9c4pyJ3rh&limit=20&offset=0&linked_partitioning=1&app_version=1761897122&app_locale=en`);
        const data = await response.json();

        const results = data.collection
            .map(track => {
                const hlsLink = track.media.transcodings.find(t => t.format.protocol === "hls" && t.format.mime_type.includes("mp4"))?.url || "";
                const imageUrl = track.artwork_url ? track.artwork_url.replace(/-large\.jpg$/, '-t500x500.jpg') : "";
                return {
                    title: track.title,
                    image: imageUrl,
                    href: `${hlsLink}?title=${encodeURIComponent(track.title)}&artist=${encodeURIComponent(track.user.username)}`,
                    hasHls: hlsLink !== ""
                };
            })
            .filter(track => track.hasHls)
            .map(track => ({
                title: track.title,
                image: track.image,
                href: track.href
            }));

        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            title: "Error",
            image: "Error",
            href: "Error"
        }]);
    }
}

async function extractDetails(url) {
	console.log(url);
    try {
        const params = url.split('?')[1] || '';
        const pairs = params.split('&');
        let title = '';
        let artist = '';
        
        for (const pair of pairs) {
            const [key, value] = pair.split('=');
            if (key === 'title') title = decodeURIComponent(value);
            if (key === 'artist') artist = decodeURIComponent(value);
        }
        
        const description = `${title} by ${artist}`;
        return JSON.stringify([{
            description: description,
            aliases: "",
            airdate: ""
        }]);
    } catch (err) {
        return JSON.stringify([{
            description: "Error",
            aliases: "Error",
            airdate: "Error"
        }]);
    }
}

async function extractEpisodes(url) {
    const results = [];
    try {
        const cleanUrl = url.split('?')[0];
        results.push({
            href: cleanUrl,
            number: 1
        });
        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            href: "Error",
            number: "Error"
        }]);
    }
}

async function extractStreamUrl(url) {
    try {
        const response = await fetchv2(url+"?client_id=xwYTVSni6n4FghaI0c4uJ8T9c4pyJ3rh&track_authorization=");
        const data = await response.json();

        return data.url || "https://error.org/";
    } catch (err) {
        return "https://error.org/";
    }
}
