"use strict";
var VideoLiveSessionModel_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoLiveSessionModel = exports.ScopeNames = void 0;
const tslib_1 = require("tslib");
const sequelize_typescript_1 = require("sequelize-typescript");
const extra_utils_1 = require("../../../shared/extra-utils");
const video_1 = require("./video");
var ScopeNames;
(function (ScopeNames) {
    ScopeNames["WITH_REPLAY"] = "WITH_REPLAY";
})(ScopeNames = exports.ScopeNames || (exports.ScopeNames = {}));
let VideoLiveSessionModel = VideoLiveSessionModel_1 = class VideoLiveSessionModel extends sequelize_typescript_1.Model {
    static load(id) {
        return VideoLiveSessionModel_1.findOne({
            where: { id }
        });
    }
    static findSessionOfReplay(replayVideoId) {
        const query = {
            where: {
                replayVideoId
            }
        };
        return VideoLiveSessionModel_1.scope(ScopeNames.WITH_REPLAY).findOne(query);
    }
    static findCurrentSessionOf(videoId) {
        return VideoLiveSessionModel_1.findOne({
            where: {
                liveVideoId: videoId,
                endDate: null
            },
            order: [['startDate', 'DESC']]
        });
    }
    static findLatestSessionOf(videoId) {
        return VideoLiveSessionModel_1.findOne({
            where: {
                liveVideoId: videoId
            },
            order: [['startDate', 'DESC']]
        });
    }
    static listSessionsOfLiveForAPI(options) {
        const { videoId } = options;
        const query = {
            where: {
                liveVideoId: videoId
            },
            order: [['startDate', 'ASC']]
        };
        return VideoLiveSessionModel_1.scope(ScopeNames.WITH_REPLAY).findAll(query);
    }
    toFormattedJSON() {
        const replayVideo = this.ReplayVideo
            ? {
                id: this.ReplayVideo.id,
                uuid: this.ReplayVideo.uuid,
                shortUUID: (0, extra_utils_1.uuidToShort)(this.ReplayVideo.uuid)
            }
            : undefined;
        return {
            id: this.id,
            startDate: this.startDate.toISOString(),
            endDate: this.endDate
                ? this.endDate.toISOString()
                : null,
            endingProcessed: this.endingProcessed,
            saveReplay: this.saveReplay,
            replayVideo,
            error: this.error
        };
    }
};
tslib_1.__decorate([
    sequelize_typescript_1.CreatedAt,
    tslib_1.__metadata("design:type", Date)
], VideoLiveSessionModel.prototype, "createdAt", void 0);
tslib_1.__decorate([
    sequelize_typescript_1.UpdatedAt,
    tslib_1.__metadata("design:type", Date)
], VideoLiveSessionModel.prototype, "updatedAt", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(false),
    (0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.DATE),
    tslib_1.__metadata("design:type", Date)
], VideoLiveSessionModel.prototype, "startDate", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(true),
    (0, sequelize_typescript_1.Column)(sequelize_typescript_1.DataType.DATE),
    tslib_1.__metadata("design:type", Date)
], VideoLiveSessionModel.prototype, "endDate", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(true),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Number)
], VideoLiveSessionModel.prototype, "error", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(false),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Boolean)
], VideoLiveSessionModel.prototype, "saveReplay", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.AllowNull)(false),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Boolean)
], VideoLiveSessionModel.prototype, "endingProcessed", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.ForeignKey)(() => video_1.VideoModel),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Number)
], VideoLiveSessionModel.prototype, "replayVideoId", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.BelongsTo)(() => video_1.VideoModel, {
        foreignKey: {
            allowNull: true,
            name: 'replayVideoId'
        },
        as: 'ReplayVideo',
        onDelete: 'set null'
    }),
    tslib_1.__metadata("design:type", video_1.VideoModel)
], VideoLiveSessionModel.prototype, "ReplayVideo", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.ForeignKey)(() => video_1.VideoModel),
    sequelize_typescript_1.Column,
    tslib_1.__metadata("design:type", Number)
], VideoLiveSessionModel.prototype, "liveVideoId", void 0);
tslib_1.__decorate([
    (0, sequelize_typescript_1.BelongsTo)(() => video_1.VideoModel, {
        foreignKey: {
            allowNull: true,
            name: 'liveVideoId'
        },
        as: 'LiveVideo',
        onDelete: 'set null'
    }),
    tslib_1.__metadata("design:type", video_1.VideoModel)
], VideoLiveSessionModel.prototype, "LiveVideo", void 0);
VideoLiveSessionModel = VideoLiveSessionModel_1 = tslib_1.__decorate([
    (0, sequelize_typescript_1.Scopes)(() => ({
        [ScopeNames.WITH_REPLAY]: {
            include: [
                {
                    model: video_1.VideoModel.unscoped(),
                    as: 'ReplayVideo',
                    required: false
                }
            ]
        }
    })),
    (0, sequelize_typescript_1.Table)({
        tableName: 'videoLiveSession',
        indexes: [
            {
                fields: ['replayVideoId'],
                unique: true
            },
            {
                fields: ['liveVideoId']
            }
        ]
    })
], VideoLiveSessionModel);
exports.VideoLiveSessionModel = VideoLiveSessionModel;
//# sourceMappingURL=video-live-session.js.map