"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAfterVideoChannelImport = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../helpers/logger");
const video_channel_sync_1 = require("../../../models/video/video-channel-sync");
function processAfterVideoChannelImport(job) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const payload = job.data;
        if (!payload.channelSyncId)
            return;
        logger_1.logger.info('Processing after video channel import in job %s.', job.id);
        const sync = yield video_channel_sync_1.VideoChannelSyncModel.loadWithChannel(payload.channelSyncId);
        if (!sync) {
            logger_1.logger.error('Unknown sync id %d.', payload.channelSyncId);
            return;
        }
        const childrenValues = yield job.getChildrenValues();
        let errors = 0;
        let successes = 0;
        for (const value of Object.values(childrenValues)) {
            if (value.resultType === 'success')
                successes++;
            else if (value.resultType === 'error')
                errors++;
        }
        if (errors > 0) {
            sync.state = 4;
            logger_1.logger.error(`Finished synchronizing "${sync.VideoChannel.Actor.preferredUsername}" with failures.`, { errors, successes });
        }
        else {
            sync.state = 3;
            logger_1.logger.info(`Finished synchronizing "${sync.VideoChannel.Actor.preferredUsername}" successfully.`, { successes });
        }
        yield sync.save();
    });
}
exports.processAfterVideoChannelImport = processAfterVideoChannelImport;
//# sourceMappingURL=after-video-channel-import.js.map