"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchRemotePlaylistElement = exports.fetchRemoteVideoPlaylist = void 0;
const tslib_1 = require("tslib");
const playlist_1 = require("../../../../helpers/custom-validators/activitypub/playlist");
const misc_1 = require("../../../../helpers/custom-validators/misc");
const logger_1 = require("../../../../helpers/logger");
const requests_1 = require("../../../../helpers/requests");
const url_1 = require("../../url");
function fetchRemoteVideoPlaylist(playlistUrl) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const lTags = (0, logger_1.loggerTagsFactory)('ap', 'video-playlist', playlistUrl);
        logger_1.logger.info('Fetching remote playlist %s.', playlistUrl, lTags());
        const { body, statusCode } = yield (0, requests_1.doJSONRequest)(playlistUrl, { activityPub: true });
        if ((0, playlist_1.isPlaylistObjectValid)(body) === false || (0, url_1.checkUrlsSameHost)(body.id, playlistUrl) !== true) {
            logger_1.logger.debug('Remote video playlist JSON is not valid.', Object.assign({ body }, lTags()));
            return { statusCode, playlistObject: undefined };
        }
        if (!(0, misc_1.isArray)(body.to)) {
            logger_1.logger.debug('Remote video playlist JSON does not have a valid audience.', Object.assign({ body }, lTags()));
            return { statusCode, playlistObject: undefined };
        }
        return { statusCode, playlistObject: body };
    });
}
exports.fetchRemoteVideoPlaylist = fetchRemoteVideoPlaylist;
function fetchRemotePlaylistElement(elementUrl) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const lTags = (0, logger_1.loggerTagsFactory)('ap', 'video-playlist', 'element', elementUrl);
        logger_1.logger.debug('Fetching remote playlist element %s.', elementUrl, lTags());
        const { body, statusCode } = yield (0, requests_1.doJSONRequest)(elementUrl, { activityPub: true });
        if (!(0, playlist_1.isPlaylistElementObjectValid)(body))
            throw new Error(`Invalid body in fetch playlist element ${elementUrl}`);
        if ((0, url_1.checkUrlsSameHost)(body.id, elementUrl) !== true) {
            throw new Error(`Playlist element url ${elementUrl} host is different from the AP object id ${body.id}`);
        }
        return { statusCode, elementObject: body };
    });
}
exports.fetchRemotePlaylistElement = fetchRemotePlaylistElement;
//# sourceMappingURL=url-to-object.js.map