"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBrowseRouter = void 0;
const common_stuff_1 = require("common-stuff");
const torrent_search_1 = require("../services/torrent-search");
const openapi_1 = require("../helpers/openapi");
function getBrowseRouter({}) {
    return [
        openapi_1.createRoute('searchTorrents', async (req, resp) => {
            const { provider, query, category } = req.query;
            if (!torrent_search_1.isProviderSupported(provider)) {
                throw new common_stuff_1.HttpError(common_stuff_1.HttpStatusCodes.BAD_REQUEST, `Provider ${provider} is not supported`);
            }
            return resp.json(await torrent_search_1.search([provider], query, {
                category: category,
                limit: 50,
            }));
        }),
        openapi_1.createRoute('getProviders', async (_req, resp) => {
            return resp.json(await torrent_search_1.getProvidersInfo());
        }),
        openapi_1.createRoute('getMagnet', async (req, res) => {
            const { provider, torrentId } = req.params;
            if (!torrent_search_1.isProviderSupported(provider)) {
                throw new common_stuff_1.HttpError(common_stuff_1.HttpStatusCodes.NOT_FOUND, `Provider ${provider} not found`);
            }
            const magnet = await torrent_search_1.providers[provider].getMagnet(torrentId);
            if (!magnet) {
                throw new common_stuff_1.HttpError(common_stuff_1.HttpStatusCodes.NOT_FOUND, `Torrent ${torrentId} not found`);
            }
            return res.json({
                magnet,
            });
        }),
    ];
}
exports.getBrowseRouter = getBrowseRouter;
