"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoRatingValidator = exports.getAccountVideoRateValidatorFactory = exports.videoUpdateRateValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const accounts_1 = require("../../../helpers/custom-validators/accounts");
const misc_1 = require("../../../helpers/custom-validators/misc");
const video_rates_1 = require("../../../helpers/custom-validators/video-rates");
const videos_1 = require("../../../helpers/custom-validators/videos");
const account_video_rate_1 = require("../../../models/account/account-video-rate");
const shared_1 = require("../shared");
const videoUpdateRateValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (0, express_validator_1.body)('rating')
        .custom(videos_1.isVideoRatingTypeValid),
    (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.doesVideoExist)(req.params.id, res)))
            return;
        if (!(yield (0, shared_1.checkCanSeeVideo)({ req, res, paramId: req.params.id, video: res.locals.videoAll })))
            return;
        return next();
    })
];
exports.videoUpdateRateValidator = videoUpdateRateValidator;
const getAccountVideoRateValidatorFactory = function (rateType) {
    return [
        (0, express_validator_1.param)('name')
            .custom(accounts_1.isAccountNameValid),
        (0, express_validator_1.param)('videoId')
            .custom(misc_1.isIdValid),
        (req, res, next) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            if ((0, shared_1.areValidationErrors)(req, res))
                return;
            const rate = yield account_video_rate_1.AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId);
            if (!rate) {
                return res.fail({
                    status: 404,
                    message: 'Video rate not found'
                });
            }
            res.locals.accountVideoRate = rate;
            return next();
        })
    ];
};
exports.getAccountVideoRateValidatorFactory = getAccountVideoRateValidatorFactory;
const videoRatingValidator = [
    (0, express_validator_1.query)('rating')
        .optional()
        .custom(video_rates_1.isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoRatingValidator = videoRatingValidator;
//# sourceMappingURL=video-rates.js.map