"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoViewsManager = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../helpers/logger");
const shared_1 = require("./shared");
const lTags = (0, logger_1.loggerTagsFactory)('views');
class VideoViewsManager {
    constructor() {
    }
    init() {
        this.videoViewerStats = new shared_1.VideoViewerStats();
        this.videoViewerCounters = new shared_1.VideoViewerCounters();
        this.videoViews = new shared_1.VideoViews();
    }
    processLocalView(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { video, ip, viewEvent, currentTime } = options;
            logger_1.logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags());
            yield this.videoViewerStats.addLocalViewer({ video, ip, viewEvent, currentTime });
            const successViewer = yield this.videoViewerCounters.addLocalViewer({ video, ip });
            const watchTime = yield this.videoViewerStats.getWatchTime(video.id, ip);
            const successView = yield this.videoViews.addLocalView({ video, watchTime, ip });
            return { successView, successViewer };
        });
    }
    processRemoteView(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const { video, viewerId, viewerExpires } = options;
            logger_1.logger.debug('Processing remote view for %s.', video.url, Object.assign({ viewerExpires, viewerId }, lTags()));
            if (viewerExpires)
                yield this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires });
            else
                yield this.videoViews.addRemoteView({ video });
        });
    }
    getViewers(video) {
        return this.videoViewerCounters.getViewers(video);
    }
    getTotalViewers(options) {
        return this.videoViewerCounters.getTotalViewers(options);
    }
    buildViewerExpireTime() {
        return this.videoViewerCounters.buildViewerExpireTime();
    }
    processViewerStats() {
        return this.videoViewerStats.processViewerStats();
    }
    static get Instance() {
        return this.instance || (this.instance = new this());
    }
}
exports.VideoViewsManager = VideoViewsManager;
//# sourceMappingURL=video-views-manager.js.map