"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVideoLocalViewerValidator = exports.videoViewValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const video_view_1 = require("../../../helpers/custom-validators/video-view");
const video_1 = require("../../../lib/video");
const local_video_viewer_1 = require("../../../models/view/local-video-viewer");
const misc_1 = require("../../../helpers/custom-validators/misc");
const shared_1 = require("../shared");
const getVideoLocalViewerValidator = [
    (0, express_validator_1.param)('localViewerId')
        .custom(misc_1.isIdValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const localViewer = yield local_video_viewer_1.LocalVideoViewerModel.loadFullById(+req.params.localViewerId);
        if (!localViewer) {
            return res.fail({
                status: 404,
                message: 'Local viewer not found'
            });
        }
        res.locals.localViewerFull = localViewer;
        return next();
    })
];
exports.getVideoLocalViewerValidator = getVideoLocalViewerValidator;
const videoViewValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (0, express_validator_1.body)('currentTime')
        .optional()
        .customSanitizer(misc_1.toIntOrNull)
        .custom(misc_1.isIntOrNull),
    (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.videoId, res, 'only-immutable-attributes')))
            return;
        const video = res.locals.onlyImmutableVideo;
        const { duration } = yield (0, video_1.getCachedVideoDuration)(video.id);
        if (!(0, misc_1.exists)(req.body.currentTime)) {
            req.body.currentTime = Math.min(duration !== null && duration !== void 0 ? duration : 0, 30);
        }
        const currentTime = req.body.currentTime;
        if (!(0, video_view_1.isVideoTimeValid)(currentTime, duration)) {
            return res.fail({
                status: 400,
                message: 'Current time is invalid'
            });
        }
        return next();
    })
];
exports.videoViewValidator = videoViewValidator;
//# sourceMappingURL=video-view.js.map