{"size": 1911, "ext": "js", "max_stars_repo_path": "files/unminified/static/js/mangadex.js", "max_stars_repo_name": "fiwippi/tanuki", "max_stars_repo_stars_event_min_datetime": "2021-08-02T19:05:34.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T07:27:45.000Z", "max_issues_repo_path": "files/unminified/static/js/mangadex.js", "max_issues_repo_name": "fiwippi/tanuki", "max_issues_repo_issues_event_min_datetime": "2022-03-05T23:59:55.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-27T01:03:45.000Z", "max_forks_repo_path": "files/unminified/static/js/mangadex.js", "max_forks_repo_name": "fiwippi/tanuki", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 6, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 28.9545454545, "max_line_length": 155, "alphanum_fraction": 0.5196232339}
export const name = 'mangadex';

async function fetchResource(endpoint, query) {
    const options = {
        method: 'POST',
        body: JSON.stringify({
            endpoint: endpoint,
            query: query,
        })
    };

    return fetch("/api/proxy/mangadex", options)
        .then(response => {
            if (response.status >= 400 && response.status <= 500) {
                throw Error(`invalid response status: ${response.status}, resp: ${response.body}`)
                return
            }

            return response.json()
        })
        .catch(error => {throw error})
}

export class Search {
    static async Manga(title, limit, coverArt) {
        if (title.length <= 0) return
        if (limit === undefined) limit = 15
        return fetchResource(`manga`, `title=${title}&limit=${limit}${coverArt ? "&includes[]=cover_art" : ""}`)
    }
}

export class Manga {
    static async View(id, coverArt) {
        return fetchResource(`manga/${id}`, `${coverArt ? "includes[]=cover_art" : ""}`)
    }

    static async Feed(id, limit, offset) {
        if (limit === undefined) limit = 10
        if (offset === undefined) offset = 0
        return fetchResource(`manga/${id}/feed`, `limit=${limit}&offset=${offset}&translatedLanguage[]=en&order[chapter]=desc&includes[]=scanlation_group`)
    }

    static async FeedAll(id) {
        let feed = []
        let moreLeft = true
        let limit = 500
        let offset = 0

        while (moreLeft) {
            await this.Feed(id, limit, offset)
                .then(resp => {
                    feed.push(...resp.data)
                    offset += limit
                    if (feed.length >= resp.total) {
                        moreLeft = false
                    }
                })
        }

        return feed
    }

    static async Chapters(id) {
        return fetchResource(`manga/${id}/aggregate`)
    }
}