async function searchResults(keyword) {
    const results = [];
    try {
        const response = await fetchv2("https://123animes.ru/search?keyword=" + encodeURIComponent(keyword));
        const html = await response.text();

        const filmListMatch = html.match(/<div class="film-list">([\s\S]*?)<div class="clearfix"><\/div>/);
        if (!filmListMatch) {
            return JSON.stringify(results);
        }

        const filmList = filmListMatch[1];
        const itemRegex = /<div class="item">[\s\S]*?<a href="([^"]+)"[^>]*class="poster"[\s\S]*?<img[^>]*alt="([^"]+)"[^>]*src="([^"]+)"/g;
        let match;
        while ((match = itemRegex.exec(filmList)) !== null) {
            results.push({
                title: match[2].trim(),
                image: "https://123animes.ru" + match[3].trim(),
                href: "https://123animes.ru" + 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();

        let description = "N/A";
        let aliases = "N/A";
        let airdate = "N/A";

        const descMatch = html.match(/<div class="desc">([\s\S]*?)<\/div>/);
        if (descMatch) {
            description = descMatch[1].replace(/<[^>]*>/g, "").replace(/\s+/g, " ").trim();
        }

        const aliasMatch = html.match(/<p class="alias">([^<]*)<\/p>/);
        if (aliasMatch) {
            aliases = aliasMatch[1].trim();
        }

        const airdateMatch = html.match(/<dt>Released:<\/dt>\s*<dd>\s*<a[^>]*>(\d+)<\/a>/);
        if (airdateMatch) {
            airdate = airdateMatch[1].trim();
        }

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

async function extractEpisodes(url) {
    const results = [];
    try {
        const animeId = url.split('/').pop();
        
        const response = await fetchv2("https://123animes.ru/ajax/film/sv?id=" + animeId);
        const jsonData = await response.json();
        const html = jsonData.html;

        const episodesMatch = html.match(/<ul class="episodes range"[^>]*>([\s\S]*?)<\/ul>/);
        if (!episodesMatch) {
            return JSON.stringify(results);
        }

        const episodesHTML = episodesMatch[1];
        
        const episodeRegex = /data-pop='(\d+)'/g;
        let match;
        const seenEpisodes = new Set();
        
        while ((match = episodeRegex.exec(episodesHTML)) !== null) {
            const episodeNum = parseInt(match[1], 10);

            if (!seenEpisodes.has(episodeNum)) {
                seenEpisodes.add(episodeNum);
                results.push({
                    href: animeId + "/" + episodeNum + "/vidstreaming.io",
                    number: episodeNum
                });
            }
        }

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

async function extractStreamUrl(ID) {
    try {
        const response = await fetchv2("https://123animes.ru/ajax/episode/info?epr=" + ID);
        const data = await response.json();

        const target = data.target;
        const streamID = target.split('/').pop();

        const responseTwo = await fetchv2("https://play.shipimagesbolt.online/hs/getSources?id=" + streamID);
        const dataTwo = await responseTwo.json();

        const stream = dataTwo.sources;

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