async function searchResults(keyword) {
    const results = [];
    const regex = /<div title='.*?<img .*?src="(.*?)".*?>.*?<a class="item_movies_link" href="(.*?)">(.*?)<\/a>/gs;

    try {
        const response = await fetchv2("https://kisscartoon.sh/Search/?s=" + encodeURIComponent(keyword));
        const html = await response.text();

        let match;
        while ((match = regex.exec(html)) !== null) {
            results.push({
                image: match[1].trim(),
                href: match[2].trim(),
                title: match[3].replace(/<.*?>/g, '').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 match = html.match(/<div class="summary">\s*<p>(.*?)<\/p>\s*<\/div>/s);
        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 = [];

    const regex = /<div>\s*<div>\s*<h3>\s*<a href="(.*?)" title=".*?">/gs;

    const dateRegex = /<div>\s*<div>\s*<h3>.*?<\/h3>\s*<\/div>\s*<div>(.*?)<\/div>/s;

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

        let match;
        let count = 1;
        while ((match = regex.exec(html)) !== null) {
            results.push({
                href: match[1].trim(),
                number: count++
            });
        }

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

async function extractStreamUrl(url) {
    try {
        const idMatch = url.match(/id=(\d+)/);
        const id = idMatch ? idMatch[1] : null;

        const headers = {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'X-Requested-With': 'XMLHttpRequest',
            'Referer': url,
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
        };
        
        const postData = `episode_id=${id}`;
        const response = await fetchv2("https://kisscartoon.sh/ajax/anime/load_episodes_v2?s=tserver", headers, 'POST', postData);
        const json = await response.json();

        const iframeMatch = json.value.match(/<iframe .*?src="(.*?)"/);
        const iframeSrc = iframeMatch ? iframeMatch[1] : null;

        if (!iframeSrc) return "https://error.org/";

        const headers2 = {
            'Referer': "https://kisscartoon.sh",
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
        };
        const iframeResponse = await fetchv2(iframeSrc, headers2);
        const iframeHtml = await iframeResponse.text();

        const sourcesMatch = iframeHtml.match(/sources:\s*(\[\{.*?\}\])/s);
        if (!sourcesMatch) return "https://error.org/";

        const sources = JSON.parse(sourcesMatch[1]);
        const fileUrl = sources.length > 0 ? sources[0].file : null;

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



