"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTranscodingValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const video_transcoding_1 = require("../../../helpers/custom-validators/video-transcoding");
const config_1 = require("../../../initializers/config");
const video_job_info_1 = require("../../../models/video/video-job-info");
const shared_1 = require("../shared");
const createTranscodingValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (0, express_validator_1.body)('transcodingType')
        .custom(video_transcoding_1.isValidCreateTranscodingType),
    (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, 'all')))
            return;
        const video = res.locals.videoAll;
        if (video.remote) {
            return res.fail({
                status: 400,
                message: 'Cannot run transcoding job on a remote video'
            });
        }
        if (config_1.CONFIG.TRANSCODING.ENABLED !== true) {
            return res.fail({
                status: 400,
                message: 'Cannot run transcoding job because transcoding is disabled on this instance'
            });
        }
        const info = yield video_job_info_1.VideoJobInfoModel.load(video.id);
        if (info && info.pendingTranscode > 0) {
            return res.fail({
                status: 409,
                message: 'This video is already being transcoded'
            });
        }
        return next();
    })
];
exports.createTranscodingValidator = createTranscodingValidator;
//# sourceMappingURL=video-transcoding.js.map