"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoChannelSyncLatestScheduler = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../helpers/logger");
const config_1 = require("../../initializers/config");
const video_channel_1 = require("../../models/video/video-channel");
const video_channel_sync_1 = require("../../models/video/video-channel-sync");
const constants_1 = require("../../initializers/constants");
const sync_channel_1 = require("../sync-channel");
const abstract_scheduler_1 = require("./abstract-scheduler");
class VideoChannelSyncLatestScheduler extends abstract_scheduler_1.AbstractScheduler {
    constructor() {
        super();
        this.schedulerIntervalMs = constants_1.SCHEDULER_INTERVALS_MS.CHANNEL_SYNC_CHECK_INTERVAL;
    }
    internalExecute() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
                logger_1.logger.debug('Discard channels synchronization as the feature is disabled');
                return;
            }
            logger_1.logger.info('Checking channels to synchronize');
            const channelSyncs = yield video_channel_sync_1.VideoChannelSyncModel.listSyncs();
            for (const sync of channelSyncs) {
                const channel = yield video_channel_1.VideoChannelModel.loadAndPopulateAccount(sync.videoChannelId);
                logger_1.logger.info('Creating video import jobs for "%s" sync with external channel "%s"', channel.Actor.preferredUsername, sync.externalChannelUrl);
                const onlyAfter = sync.lastSyncAt || sync.createdAt;
                yield (0, sync_channel_1.synchronizeChannel)({
                    channel,
                    externalChannelUrl: sync.externalChannelUrl,
                    videosCountLimit: config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.VIDEOS_LIMIT_PER_SYNCHRONIZATION,
                    channelSync: sync,
                    onlyAfter
                });
            }
        });
    }
    static get Instance() {
        return this.instance || (this.instance = new this());
    }
}
exports.VideoChannelSyncLatestScheduler = VideoChannelSyncLatestScheduler;
//# sourceMappingURL=video-channel-sync-latest-scheduler.js.map