"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPlaylistUrl = exports.getSteamUrl = exports.verifyJwtToken = exports.signJwtToken = exports.mapValues = exports.exists = void 0;
const jsonwebtoken_1 = require("jsonwebtoken");
const openapi_1 = require("./openapi");
function exists(json, key) {
    const value = json[key];
    return value !== null && value !== undefined;
}
exports.exists = exists;
function mapValues(data, fn) {
    return Object.keys(data).reduce((acc, key) => ({ ...acc, [key]: fn(data[key]) }), {});
}
exports.mapValues = mapValues;
function signJwtToken(data, key) {
    return jsonwebtoken_1.sign(data, key);
}
exports.signJwtToken = signJwtToken;
function verifyJwtToken(token, key, maxAge) {
    try {
        return jsonwebtoken_1.verify(token, key, {
            maxAge,
        });
    }
    catch (error) {
        console.log(error);
        return undefined;
    }
}
exports.verifyJwtToken = verifyJwtToken;
function getSteamUrl(torrent, file, encodeToken) {
    if (encodeToken) {
        return openapi_1.getRouteUrl('getStream', {
            torrent: signJwtToken({
                torrent,
                file,
            }, encodeToken),
        }, {});
    }
    return openapi_1.getRouteUrl('getStream', {
        torrent,
    }, { file });
}
exports.getSteamUrl = getSteamUrl;
function getPlaylistUrl(torrent, encodeToken) {
    if (encodeToken) {
        return openapi_1.getRouteUrl('getPlaylist', {
            torrent: signJwtToken({
                torrent,
            }, encodeToken),
        }, {});
    }
    return openapi_1.getRouteUrl('getPlaylist', { torrent }, {});
}
exports.getPlaylistUrl = getPlaylistUrl;
