async function searchResults(keyword) {
    const results = [];
    try {
        const urls = [
            'https://wearechecking.wtf/streams-pages/motorsports',
            'https://wearechecking.wtf/streams-pages/football',
            'https://wearechecking.wtf/streams-pages/others'
        ];

        const responses = await Promise.all(
            urls.map(url => fetchv2(url).then(res => res.text()))
        );

        for (const html of responses) {
            const wrapperRegex = /<div class="stream-wrapper[^"]*"[^>]*>([\s\S]*?)<\/div>\s*<!--.*?stream close -->/gi;
            let wrapperMatch;

            while ((wrapperMatch = wrapperRegex.exec(html)) !== null) {
                const wrapperContent = wrapperMatch[1];

                const imgRegex = /<img class="stream-thumb" src="([^"]+)"/i;
                const imgMatch = imgRegex.exec(wrapperContent);
                const imageUrl = imgMatch ? imgMatch[1].replace('..', 'https://wearechecking.wtf').replace(/\\/g, '/') : '';

                const seriesTitleRegex = /<img src="[^"]*[\/\\]([^\/\\]+)\.svg"/i;
                const seriesMatch = seriesTitleRegex.exec(wrapperContent);
                const sport = seriesMatch ? seriesMatch[1].toUpperCase() : 'UNKNOWN';

                const streamLinkRegex = /<div class="stream-feed[^"]*"[^>]*onclick="location\.href='([^']+)'[^>]*>\s*<p><span class="unix-timestamp">\d+<\/span>([^<]+)<\/p>/gi;
                let linkMatch;

                while ((linkMatch = streamLinkRegex.exec(wrapperContent)) !== null) {
                    const streamUrl = 'https://wearechecking.wtf' + linkMatch[1];
                    const eventName = linkMatch[2].trim();
                    const title = `[${sport}] ${eventName}`;

                    results.push({
                        title: title,
                        image: imageUrl,
                        href: streamUrl
                    });
                }
            }
        }

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

async function extractDetails(url) {
    try {
        return JSON.stringify([{
            description: "",
            aliases: "",
            airdate: ""
        }]);
    } catch (err) {
        return JSON.stringify([{
            description: "Error",
            aliases: "Error",
            airdate: "Error"
        }]);
    }
}

async function extractEpisodes(url) {
    const results = [];
    try {
        results.push({
            href: url,
            number: 1
        });

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

async function extractStreamUrl(pageUrl) {
    try {
        const response = await fetchv2(pageUrl);
        const html = await response.text();

        const iframeRegex = /(?:document\.getElementById\('feed-iframe'\)\.src|src)\s*=\s*['"]https:\/\/ihatestreams\.xyz\/embed\/([a-f0-9\-]+)['"]/i;
        const match = iframeRegex.exec(html);
        
        if (!match) return JSON.stringify({ streams: [], subtitle: null });
        
        const streamId = match[1];

        const streamResponse = await fetchv2(`https://ihatestreams.xyz/get-stream?id=${streamId}`);
        const streamData = await streamResponse.json();

        let streamUrl = streamData.pointer;
        let headers = {};

        if (!streamUrl.startsWith("http")) {
            const baseUrl = `https://gg.revoltedpeanuts.com/${streamUrl}/`;
            headers = {
                "Referer": "https://ihatestreams.xyz/",
                "Origin": "https://ihatestreams.xyz"
            };

            const manifestResponse = await fetchv2(baseUrl + "index.m3u8", headers);
            const manifestText = await manifestResponse.text();

            const lines = manifestText.split("\n").map(l => l.trim());
            const relativePath = lines.find(l => l && !l.startsWith("#")) || "index.m3u8";
            streamUrl = baseUrl + relativePath;
        }

        return JSON.stringify({
            streams: [
                {
                    title: streamData.title || "Unknown",
                    streamUrl,
                    headers
                }
            ],
            subtitle: streamData.subtitle || null
        });

    } catch (err) {
        return JSON.stringify({
            streams: [
                {
                    title: "Error",
                    streamUrl: "https://error.org/",
                    headers: {}
                }
            ],
            subtitle: null
        });
    }
}



