"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoCommentsFeedsValidator = exports.videoSubscriptionFeedsValidator = exports.videoFeedsValidator = exports.setFeedFormatContentType = exports.feedsFormatValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const feeds_1 = require("../../helpers/custom-validators/feeds");
const misc_1 = require("../../helpers/custom-validators/misc");
const shared_1 = require("./shared");
const feedsFormatValidator = [
    (0, express_validator_1.param)('format')
        .optional()
        .custom(feeds_1.isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
    (0, express_validator_1.query)('format')
        .optional()
        .custom(feeds_1.isValidRSSFeed).withMessage('Should have a valid format (rss, atom, json)'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.feedsFormatValidator = feedsFormatValidator;
function setFeedFormatContentType(req, res, next) {
    const format = req.query.format || req.params.format || 'rss';
    let acceptableContentTypes;
    if (format === 'atom' || format === 'atom1') {
        acceptableContentTypes = ['application/atom+xml', 'application/xml', 'text/xml'];
    }
    else if (format === 'json' || format === 'json1') {
        acceptableContentTypes = ['application/json'];
    }
    else if (format === 'rss' || format === 'rss2') {
        acceptableContentTypes = ['application/rss+xml', 'application/xml', 'text/xml'];
    }
    else {
        acceptableContentTypes = ['application/xml', 'text/xml'];
    }
    if (req.accepts(acceptableContentTypes)) {
        res.set('Content-Type', req.accepts(acceptableContentTypes));
    }
    else {
        return res.fail({
            status: 406,
            message: `You should accept at least one of the following content-types: ${acceptableContentTypes.join(', ')}`
        });
    }
    return next();
}
exports.setFeedFormatContentType = setFeedFormatContentType;
const videoFeedsValidator = [
    (0, express_validator_1.query)('accountId')
        .optional()
        .custom(misc_1.isIdValid),
    (0, express_validator_1.query)('accountName')
        .optional(),
    (0, express_validator_1.query)('videoChannelId')
        .optional()
        .custom(misc_1.isIdValid),
    (0, express_validator_1.query)('videoChannelName')
        .optional(),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (req.query.accountId && !(yield (0, shared_1.doesAccountIdExist)(req.query.accountId, res)))
            return;
        if (req.query.videoChannelId && !(yield (0, shared_1.doesVideoChannelIdExist)(req.query.videoChannelId, res)))
            return;
        if (req.query.accountName && !(yield (0, shared_1.doesAccountNameWithHostExist)(req.query.accountName, res)))
            return;
        if (req.query.videoChannelName && !(yield (0, shared_1.doesVideoChannelNameWithHostExist)(req.query.videoChannelName, res)))
            return;
        return next();
    })
];
exports.videoFeedsValidator = videoFeedsValidator;
const videoSubscriptionFeedsValidator = [
    (0, express_validator_1.query)('accountId')
        .custom(misc_1.isIdValid),
    (0, express_validator_1.query)('token')
        .custom(misc_1.exists),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesAccountIdExist)(req.query.accountId, res)))
            return;
        if (!(yield (0, shared_1.doesUserFeedTokenCorrespond)(res.locals.account.userId, req.query.token, res)))
            return;
        return next();
    })
];
exports.videoSubscriptionFeedsValidator = videoSubscriptionFeedsValidator;
const videoCommentsFeedsValidator = [
    (0, express_validator_1.query)('videoId')
        .optional()
        .customSanitizer(misc_1.toCompleteUUID)
        .custom(misc_1.isIdOrUUIDValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
            return res.fail({ message: 'videoId cannot be mixed with a channel filter' });
        }
        if (req.query.videoId) {
            if (!(yield (0, shared_1.doesVideoExist)(req.query.videoId, res)))
                return;
            if (!(yield (0, shared_1.checkCanSeeVideo)({ req, res, paramId: req.query.videoId, video: res.locals.videoAll })))
                return;
        }
        return next();
    })
];
exports.videoCommentsFeedsValidator = videoCommentsFeedsValidator;
//# sourceMappingURL=feeds.js.map