"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.studioRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const express_utils_1 = require("../../../helpers/express-utils");
const constants_1 = require("../../../initializers/constants");
const job_queue_1 = require("../../../lib/job-queue");
const video_studio_1 = require("../../../lib/video-studio");
const middlewares_1 = require("../../../middlewares");
const studioRouter = express_1.default.Router();
exports.studioRouter = studioRouter;
const tasksFiles = (0, express_utils_1.createAnyReqFiles)(constants_1.MIMETYPES.VIDEO.MIMETYPE_EXT, (req, file, cb) => {
    const body = req.body;
    const matches = file.fieldname.match(/tasks\[(\d+)\]/);
    if (!matches)
        return cb(new Error('Cannot find array element indice for ' + file.fieldname));
    const indice = parseInt(matches[1]);
    const task = body.tasks[indice];
    if (!task)
        return cb(new Error('Cannot find array element of indice ' + indice + ' for ' + file.fieldname));
    if (['add-intro', 'add-outro', 'add-watermark'].includes(task.name) &&
        file.fieldname === (0, video_studio_1.buildTaskFileFieldname)(indice)) {
        return cb(null, true);
    }
    return cb(null, false);
});
studioRouter.post('/:videoId/studio/edit', middlewares_1.authenticate, tasksFiles, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoStudioAddEditionValidator), (0, middlewares_1.asyncMiddleware)(createEditionTasks));
function createEditionTasks(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const files = req.files;
        const body = req.body;
        const video = res.locals.videoAll;
        video.state = 9;
        yield video.save();
        const payload = {
            videoUUID: video.uuid,
            tasks: body.tasks.map((t, i) => buildTaskPayload(t, i, files))
        };
        job_queue_1.JobQueue.Instance.createJobAsync({ type: 'video-studio-edition', payload });
        return res.sendStatus(204);
    });
}
const taskPayloadBuilders = {
    'add-intro': buildIntroOutroTask,
    'add-outro': buildIntroOutroTask,
    'cut': buildCutTask,
    'add-watermark': buildWatermarkTask
};
function buildTaskPayload(task, indice, files) {
    return taskPayloadBuilders[task.name](task, indice, files);
}
function buildIntroOutroTask(task, indice, files) {
    return {
        name: task.name,
        options: {
            file: (0, video_studio_1.getTaskFile)(files, indice).path
        }
    };
}
function buildCutTask(task) {
    return {
        name: task.name,
        options: {
            start: task.options.start,
            end: task.options.end
        }
    };
}
function buildWatermarkTask(task, indice, files) {
    return {
        name: task.name,
        options: {
            file: (0, video_studio_1.getTaskFile)(files, indice).path
        }
    };
}
//# sourceMappingURL=studio.js.map