"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processVideoChannelImport = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../helpers/logger");
const config_1 = require("../../../initializers/config");
const sync_channel_1 = require("../../sync-channel");
const video_channel_1 = require("../../../models/video/video-channel");
const video_channel_sync_1 = require("../../../models/video/video-channel-sync");
function processVideoChannelImport(job) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const payload = job.data;
        logger_1.logger.info('Processing video channel import in job %s.', job.id);
        if (!config_1.CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
            throw new Error('Cannot import channel as the HTTP upload is disabled');
        }
        if (!config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
            throw new Error('Cannot import channel as the synchronization is disabled');
        }
        let channelSync;
        if (payload.partOfChannelSyncId) {
            channelSync = yield video_channel_sync_1.VideoChannelSyncModel.loadWithChannel(payload.partOfChannelSyncId);
            if (!channelSync) {
                throw new Error('Unlnown channel sync specified in videos channel import');
            }
        }
        const videoChannel = yield video_channel_1.VideoChannelModel.loadAndPopulateAccount(payload.videoChannelId);
        logger_1.logger.info(`Starting importing videos from external channel "${payload.externalChannelUrl}" to "${videoChannel.name}" `);
        yield (0, sync_channel_1.synchronizeChannel)({
            channel: videoChannel,
            externalChannelUrl: payload.externalChannelUrl,
            channelSync,
            videosCountLimit: config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.FULL_SYNC_VIDEOS_LIMIT
        });
    });
}
exports.processVideoChannelImport = processVideoChannelImport;
//# sourceMappingURL=video-channel-import.js.map