async function searchResults(keyword) {
    const results = [];
    try {
        const response = await fetchv2("https://animedefenders.me/view/?wd=" + encodeURIComponent(keyword)); 
        const html = await response.text();

        const regex = /<a class="vod-item-img shining" href="(.*?)">[\s\S]*?data-original="(.*?)"[\s\S]*?<h3 class="vod-item-title.*?"><a href=".*?">(.*?)<\/a><\/h3>/g;
        let match;
        while ((match = regex.exec(html)) !== null) {
            results.push({
                title: match[3].trim(),
                image: match[2].trim(),
                href: "https://animedefenders.me" + match[1].trim()
            });
        }

        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 descriptionRegex = /<div class="detail-desc-content layout-box">\s*<p>(.*?)<\/p>\s*<\/div>/s;
        const match = html.match(descriptionRegex);
        
        let description = "N/A";
        if (match && match[1]) {
            description = match[1]
                .replace(/<br\s*\/?>/gi, ' ')
                .replace(/<[^>]*>/g, '')
                .replace(/\s+/g, ' ')
                .trim();
        }
        
        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 regex = /<a class="text-overflow ep" href="([^"]+)">(\d+)<\/a>/g;
        let match;
        
        while ((match = regex.exec(html)) !== null) {
            results.push({
                href: "https://animedefenders.me" + match[1].trim(),
                number: parseInt(match[2], 10)
            });
        }
        
        return JSON.stringify(results.reverse());
    } catch (err) {
        return JSON.stringify([{
            href: "Error",
            number: "Error"
        }]);
    }
}

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

        const subUrlRegex = /data-url="(https:\/\/ee\.anih1\.top\/bb\/sub[^"]+)"/;
        const match = html.match(subUrlRegex);

        if (match && match[1]) {
            const subUrl = match[1];
            const headers = {
                "Referer": "https://ee.anih1.top",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
            };
            
            const subResponse = await fetchv2(subUrl, headers);
            const subHtml = await subResponse.text();
            
            const artplayerRegex = /url:\s*'([^']+\.m3u8)'/;
            const artMatch = subHtml.match(artplayerRegex);
            
            if (artMatch && artMatch[1]) {
                return artMatch[1];
            }
        }
        console.log("No stream URL found");
        return "https://error.org/";
    } catch (err) {
        return "https://error.org/";
    }
}
