"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoRetentionStatsValidator = exports.videoTimeserieStatsValidator = exports.videoOverallStatsValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const misc_1 = require("../../../helpers/custom-validators/misc");
const video_stats_1 = require("../../../helpers/custom-validators/video-stats");
const constants_1 = require("../../../initializers/constants");
const shared_1 = require("../shared");
const videoOverallStatsValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (0, express_validator_1.query)('startDate')
        .optional()
        .custom(misc_1.isDateValid),
    (0, express_validator_1.query)('endDate')
        .optional()
        .custom(misc_1.isDateValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield commonStatsCheck(req, res)))
            return;
        return next();
    })
];
exports.videoOverallStatsValidator = videoOverallStatsValidator;
const videoRetentionStatsValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield commonStatsCheck(req, res)))
            return;
        if (res.locals.videoAll.isLive) {
            return res.fail({
                status: 400,
                message: 'Cannot get retention stats of live video'
            });
        }
        return next();
    })
];
exports.videoRetentionStatsValidator = videoRetentionStatsValidator;
const videoTimeserieStatsValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (0, express_validator_1.param)('metric')
        .custom(video_stats_1.isValidStatTimeserieMetric),
    (0, express_validator_1.query)('startDate')
        .optional()
        .custom(misc_1.isDateValid),
    (0, express_validator_1.query)('endDate')
        .optional()
        .custom(misc_1.isDateValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield commonStatsCheck(req, res)))
            return;
        const query = req.query;
        if ((query.startDate && !query.endDate) ||
            (!query.startDate && query.endDate)) {
            return res.fail({
                status: 400,
                message: 'Both start date and end date should be defined if one of them is specified'
            });
        }
        if (query.startDate && getIntervalByDays(query.startDate, query.endDate) > constants_1.STATS_TIMESERIE.MAX_DAYS) {
            return res.fail({
                status: 400,
                message: 'Star date and end date interval is too big'
            });
        }
        return next();
    })
];
exports.videoTimeserieStatsValidator = videoTimeserieStatsValidator;
function commonStatsCheck(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!(yield (0, shared_1.doesVideoExist)(req.params.videoId, res, 'all')))
            return false;
        if (!(0, shared_1.checkUserCanManageVideo)(res.locals.oauth.token.User, res.locals.videoAll, 20, res))
            return false;
        return true;
    });
}
function getIntervalByDays(startDateString, endDateString) {
    const startDate = new Date(startDateString);
    const endDate = new Date(endDateString);
    return (endDate.getTime() - startDate.getTime()) / 1000 / 86400;
}
//# sourceMappingURL=video-stats.js.map