"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoStreamRepository = void 0;
const common_1 = require("@nestjs/common");
const kysely_1 = require("kysely");
const nestjs_kysely_1 = require("nestjs-kysely");
const decorators_1 = require("../decorators");
const database_1 = require("../utils/database");
let VideoStreamRepository = class VideoStreamRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    createSession(session) {
        return this.db.insertInto('video_stream_session').values(session).returning(['id']).executeTakeFirstOrThrow();
    }
    createVariant(variant) {
        return this.db.insertInto('video_stream_variant').values(variant).returning(['id']).executeTakeFirstOrThrow();
    }
    async createSegment(segment) {
        await this.db.insertInto('video_stream_segment').values(segment).execute();
    }
    getSession(id) {
        return this.db
            .selectFrom('video_stream_session')
            .selectAll()
            .where('id', '=', id)
            .where('expiresAt', '>', new Date())
            .executeTakeFirst();
    }
    getVariant(id) {
        return this.db.selectFrom('video_stream_variant').selectAll().where('id', '=', id).executeTakeFirst();
    }
    getSegment(variantId, index) {
        return this.db
            .selectFrom('video_stream_segment')
            .selectAll()
            .where('variantId', '=', variantId)
            .where('index', '=', index)
            .executeTakeFirst();
    }
    getExpiredSessions() {
        return this.db
            .selectFrom('video_stream_session')
            .innerJoin('asset', 'asset.id', 'video_stream_session.assetId')
            .select(['video_stream_session.id', 'asset.ownerId'])
            .where('video_stream_session.expiresAt', '<=', new Date())
            .execute();
    }
    async extendSession(id, expiresAt) {
        await this.db.updateTable('video_stream_session').set({ expiresAt }).where('id', '=', id).execute();
    }
    async deleteSession(id) {
        await this.db.deleteFrom('video_stream_session').where('id', '=', id).execute();
    }
    async getForMainPlaylist(id) {
        return this.db
            .selectFrom('asset')
            .innerJoin('asset_exif', 'asset.id', 'asset_exif.assetId')
            .where('asset.id', '=', id)
            .innerJoin('asset_video', 'asset.id', 'asset_video.assetId')
            .innerJoin('asset_keyframe', 'asset.id', 'asset_keyframe.assetId')
            .select((eb) => (0, database_1.withVideoStream)(eb).$notNull().as('videoStream'))
            .select((eb) => (0, database_1.withVideoPackets)(eb).$notNull().as('packets'))
            .executeTakeFirst();
    }
    async getForMediaPlaylist(id, sessionId) {
        return this.db
            .selectFrom('asset')
            .innerJoin('asset_exif', 'asset.id', 'asset_exif.assetId')
            .innerJoin('video_stream_session', 'asset.id', 'video_stream_session.assetId')
            .where('asset.id', '=', id)
            .where('video_stream_session.id', '=', sessionId)
            .where('video_stream_session.expiresAt', '>', new Date())
            .innerJoin('asset_video', 'asset.id', 'asset_video.assetId')
            .innerJoin('asset_keyframe', 'asset.id', 'asset_keyframe.assetId')
            .select((eb) => (0, database_1.withVideoStream)(eb).$notNull().as('videoStream'))
            .select((eb) => (0, database_1.withVideoPackets)(eb).$notNull().as('packets'))
            .executeTakeFirst();
    }
    async getForTranscoding(id) {
        return this.db
            .selectFrom('asset')
            .innerJoin('asset_exif', 'asset.id', 'asset_exif.assetId')
            .where('asset.id', '=', id)
            .leftJoin('asset_audio', 'asset.id', 'asset_audio.assetId')
            .innerJoin('asset_video', 'asset.id', 'asset_video.assetId')
            .innerJoin('asset_keyframe', 'asset.id', 'asset_keyframe.assetId')
            .select('asset.originalPath')
            .select((eb) => (0, database_1.withAudioStream)(eb).as('audioStream'))
            .select((eb) => (0, database_1.withVideoStream)(eb).$notNull().as('videoStream'))
            .select((eb) => (0, database_1.withVideoFormat)(eb).$notNull().as('format'))
            .select((eb) => (0, database_1.withVideoPackets)(eb).$notNull().as('packets'))
            .executeTakeFirst();
    }
};
exports.VideoStreamRepository = VideoStreamRepository;
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], VideoStreamRepository.prototype, "getSession", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", void 0)
], VideoStreamRepository.prototype, "getVariant", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID, decorators_1.DummyValue.NUMBER] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Number]),
    __metadata("design:returntype", void 0)
], VideoStreamRepository.prototype, "getSegment", null);
__decorate([
    (0, decorators_1.GenerateSql)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], VideoStreamRepository.prototype, "getExpiredSessions", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID, decorators_1.DummyValue.DATE] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Date]),
    __metadata("design:returntype", Promise)
], VideoStreamRepository.prototype, "extendSession", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], VideoStreamRepository.prototype, "deleteSession", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], VideoStreamRepository.prototype, "getForMainPlaylist", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID, decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], VideoStreamRepository.prototype, "getForMediaPlaylist", null);
__decorate([
    (0, decorators_1.GenerateSql)({ params: [decorators_1.DummyValue.UUID] }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], VideoStreamRepository.prototype, "getForTranscoding", null);
exports.VideoStreamRepository = VideoStreamRepository = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, nestjs_kysely_1.InjectKysely)()),
    __metadata("design:paramtypes", [kysely_1.Kysely])
], VideoStreamRepository);
//# sourceMappingURL=video-stream.repository.js.map