async function searchResults(keyword) {
    const urls = [
        `https://www.verseriesonline.net/recherche?q=${encodeURIComponent(keyword)}`,
        `https://www.verseriesonline.net/recherche?q=${encodeURIComponent(keyword)}&page=2`,
        `https://www.verseriesonline.net/recherche?q=${encodeURIComponent(keyword)}&page=3`,
        `https://www.verseriesonline.net/recherche?q=${encodeURIComponent(keyword)}&page=4`
    ];
    const regex = /<div class="short gridder-list">.*?<a[^>]+href="([^"]+)"[^>]*>.*?<img[^>]+src="([^"]+)"[^>]*>.*?<div class="short_title"[^>]*>.*?<a[^>]+title="([^"]+)"/gs;
    try {
        const fetchPromises = urls.map(url => fetchv2(url).then(r => r.text()));
        const htmls = await Promise.all(fetchPromises);
        const results = [];
        for (const html of htmls) {
            let match;
            while ((match = regex.exec(html)) !== null) {
                results.push({
                    href: match[1].trim(),
                    image: "https://www.verseriesonline.net" + match[2].trim(),
                    title: match[3].trim().replace("regarder ","")
                });
            }
            regex.lastIndex = 0;
        }
        return JSON.stringify(results);
    } catch (err) {
        return JSON.stringify([{
            title: "Error",
            image: "Error",
            href: "Error"
        }]);
    }
}

async function extractDetails(url) {
    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const regex = /<div class="full_content-desc">.*?<span>(.*?)<\/span>/s;
        const match = regex.exec(html);
        const description = match ? match[1].trim() : "N/A";

        return JSON.stringify([{
            description: description,
            aliases: "N/A",
            airdate: "N/A"
        }]);
    } catch (err) {
        return JSON.stringify([{
            description: "Error",
            aliases: "Error",
            airdate: "Error"
        }]);
    }
}

async function extractEpisodes(url) {
    const results = [];

    try {
        const response = await fetchv2(url);
        const html = await response.text();

        const seasonRegex = /<a class="th-hover" href="([^"]+)">.*?<div class="th-title1">temporada (\d+)<\/div>/gs;
        const seasons = [];
        let seasonMatch;
        while ((seasonMatch = seasonRegex.exec(html)) !== null) {
            seasons.push({
                url: seasonMatch[1].trim(),
                number: parseInt(seasonMatch[2], 10)
            });
        }

        await Promise.all(seasons.map(async (season) => {
            const res = await fetchv2(season.url);
            const seasonHtml = await res.text();

            const episodeRegex = /<a href="([^"]+)">.*?<span class="name">Capítulo (\d+)<\/span>/gs;
            let epMatch;
            while ((epMatch = episodeRegex.exec(seasonHtml)) !== null) {
                results.push({
                    season: season.number,
                    href: epMatch[1].trim(),
                    number: parseInt(epMatch[2], 10)
                });
            }
        }));

        results.sort((a, b) => {
            if (a.season !== b.season) {
                return a.season - b.season;
            }
            return a.number - b.number;
        });

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


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

        const blockRegex = /<div class="lien fx-row"[^>]*data-hash="([^"]+)"[^>]*>([\s\S]*?)<\/div>/g;
        let hash = null;
        let blockMatch;
        while ((blockMatch = blockRegex.exec(html)) !== null) {
            const blockContent = blockMatch[2];
            if (/class="serv"[^>]*>uqload<\/span>/.test(blockContent)) {
                hash = blockMatch[1].trim();
                break;
            }
        }

        const tokenRegex = /_token:\s*"([^"]+)"/;
        const tokenMatch = html.match(tokenRegex);
        const token = tokenMatch ? tokenMatch[1].trim() : null;

        console.log("Hash:"+ hash);
        console.log("Token:"+ token);

        const embedResponse = await fetchv2("https://www.verseriesonline.net/hashembedlink", {}, "POST", `hash=${encodeURIComponent(hash)}&_token=${encodeURIComponent(token)}`);
        const embedJson = await embedResponse.json();
        const uqloadUrl = embedJson.link;

        const someHtml = await fetchv2(uqloadUrl);
        const someText = await someHtml.text();

        const finalUrl = await uqloadExtractor(someText, uqloadUrl);
        const streamObj = {
            streams: [
                {
                    title: "Server 1",
                    streamUrl: finalUrl,
                    headers: {
                        referer: "https://uqload.cx/"
                    }
                }
            ],
            subtitle: "https://none.com"
        };
        return JSON.stringify(streamObj);
    } catch (err) {
        return  "https://error.org/";
    }
}

/* SCHEME START */

/**
 * @name uqloadExtractor
 * @author scigward
 */
async function uqloadExtractor(html, embedUrl) {
    try {
        const match = html.match(/sources:\s*\[\s*"([^"]+\.mp4)"\s*\]/);
        const videoSrc = match ? match[1] : "";

        return videoSrc;
    } catch (error) {
        console.log("uqloadExtractor error:", error.message);
        return null;
    }
}

/* SCHEME END */

