"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOrUpdateLocalVideoViewer = void 0;
const tslib_1 = require("tslib");
const local_video_viewer_1 = require("../../models/view/local-video-viewer");
const local_video_viewer_watch_section_1 = require("../../models/view/local-video-viewer-watch-section");
const activity_1 = require("./activity");
function createOrUpdateLocalVideoViewer(watchAction, video, t) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const stats = yield local_video_viewer_1.LocalVideoViewerModel.loadByUrl(watchAction.id);
        if (stats)
            yield stats.destroy({ transaction: t });
        const localVideoViewer = yield local_video_viewer_1.LocalVideoViewerModel.create({
            url: watchAction.id,
            uuid: watchAction.uuid,
            watchTime: (0, activity_1.getDurationFromActivityStream)(watchAction.duration),
            startDate: new Date(watchAction.startTime),
            endDate: new Date(watchAction.endTime),
            country: watchAction.location
                ? watchAction.location.addressCountry
                : null,
            videoId: video.id
        }, { transaction: t });
        yield local_video_viewer_watch_section_1.LocalVideoViewerWatchSectionModel.bulkCreateSections({
            localVideoViewerId: localVideoViewer.id,
            watchSections: watchAction.watchSections.map(s => ({
                start: s.startTimestamp,
                end: s.endTimestamp
            })),
            transaction: t
        });
    });
}
exports.createOrUpdateLocalVideoViewer = createOrUpdateLocalVideoViewer;
//# sourceMappingURL=local-video-viewer.js.map