async function searchContent(keyword, page = 0) {
    try {
        const response = await fetch("https://comix.to/api/v2/manga?order[relevance]=desc&keyword=" + encodeURIComponent(keyword) + "&limit=100");
        const data = await response.json();

        const results = data.result.items.map(manga => ({
            title: manga.title,
            imageURL: manga.poster.large,
            id: manga.hash_id + "-" + manga.slug 
        }));

        return results;
    } catch (err) {
        return [];
    }
}

async function getContentData(slug) {
    try {
        const response = await fetch("https://comix.to/title/" + slug);
        const html = await response.text();
        const descriptionMatch = html.match(/<meta name="description" content="([^"]*)"/);
        const description = descriptionMatch ? descriptionMatch[1] : "";

        function cleanDescription(obj) {
            if (!obj.description) return '';

            let text = obj.description;

            const htmlEntities = {
                '&#x27;': "'",
                '&amp;': '&',
                '&lt;': '<',
                '&gt;': '>',
                '&quot;': '"'
            };
            text = text.replace(/&#x27;|&amp;|&lt;|&gt;|&quot;/g, match => htmlEntities[match] || match);
            text = text.replace(/\[([^\]]+)\]\([^\)]+\)/g, '$1');
            text = text.replace(/[*_~`]/g, '');
            text = text.replace(/___.*$/g, ''); 
            text = text.replace(/[\n\r\t]+/g, ' ').replace(/\s+/g, ' ');
            return text.trim();
        }

        return {
            description: cleanDescription({ description }),
            tags: []
        };
    } catch (err) {
        return {
            description: "Error",
            tags: []
        };
    }
}

async function getChapters(url) {
    const hash = url.split("-")[0]
    const results = [];
    try {
        const firstResponse = await fetch("https://comix.to/api/v2/manga/" + hash + "/chapters?limit=100&page=1&order[number]=desc")
        const firstData = await firstResponse.json();
        const lastPage = firstData.result.pagination.last_page;
        let allItems = [];

        for (let page = 1; page <= lastPage; page++) {
            const response = await fetch("https://comix.to/api/v2/manga/" + hash + "/chapters?limit=100&page=" + page + "&order[number]=desc")
            const data = await response.json();
            allItems = allItems.concat(data.result.items);
        }

        allItems.reverse();

        allItems.forEach(item => {
            results.push([
                String(item.number),
                [{
                    id: url + "/" + item.chapter_id + "-chapter-" + item.number,
                    title: item.name || `Chapter ${item.number}`,
                    chapter: item.number,
                    scanlation_group: item.scanlation_group?.name || "Comix"
                }]
            ]);
        });

        return {
            en: results
        };
    } catch (err) {
        return {
            en: []
        };
    }
}

async function getChapterImages(url) {
    try {
        const response = await fetch("https://comix.to/title/" + url);
        const html = await response.text();
        
        const match = html.match(/\\"images\\":\[([^\]]+)\]/);
        if (match) {
            const imagesJson = '[' + match[1].replace(/\\"/g, '"') + ']';
            const urls = JSON.parse(imagesJson);
            return urls;
        }
        return [];
    } catch (err) {
        console.error(err);
        return [];
    }
}