"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideosModelGetQuerySubBuilder = exports.VideoModelGetQueryBuilder = void 0;
const tslib_1 = require("tslib");
const core_utils_1 = require("../../../../../shared/core-utils");
const abstract_video_query_builder_1 = require("./shared/abstract-video-query-builder");
const video_file_query_builder_1 = require("./shared/video-file-query-builder");
const video_model_builder_1 = require("./shared/video-model-builder");
const video_table_attributes_1 = require("./shared/video-table-attributes");
class VideoModelGetQueryBuilder {
    constructor(sequelize) {
        this.sequelize = sequelize;
        this.videoQueryBuilder = new VideosModelGetQuerySubBuilder(sequelize);
        this.webtorrentFilesQueryBuilder = new video_file_query_builder_1.VideoFileQueryBuilder(sequelize);
        this.streamingPlaylistFilesQueryBuilder = new video_file_query_builder_1.VideoFileQueryBuilder(sequelize);
        this.videoModelBuilder = new video_model_builder_1.VideoModelBuilder('get', new video_table_attributes_1.VideoTableAttributes('get'));
    }
    queryVideo(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const fileQueryOptions = Object.assign(Object.assign({}, (0, core_utils_1.pick)(options, ['id', 'url', 'transaction', 'logging'])), { includeRedundancy: this.shouldIncludeRedundancies(options) });
            const [videoRows, webtorrentFilesRows, streamingPlaylistFilesRows] = yield Promise.all([
                this.videoQueryBuilder.queryVideos(options),
                VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
                    ? this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(fileQueryOptions)
                    : Promise.resolve(undefined),
                VideoModelGetQueryBuilder.videoFilesInclude.has(options.type)
                    ? this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
                    : Promise.resolve(undefined)
            ]);
            const videos = this.videoModelBuilder.buildVideosFromRows({
                rows: videoRows,
                rowsWebTorrentFiles: webtorrentFilesRows,
                rowsStreamingPlaylist: streamingPlaylistFilesRows
            });
            if (videos.length > 1) {
                throw new Error('Video results is more than 1');
            }
            if (videos.length === 0)
                return null;
            return videos[0];
        });
    }
    shouldIncludeRedundancies(options) {
        return options.type === 'api';
    }
}
exports.VideoModelGetQueryBuilder = VideoModelGetQueryBuilder;
VideoModelGetQueryBuilder.videoFilesInclude = new Set(['api', 'full', 'account-blacklist-files', 'all-files']);
class VideosModelGetQuerySubBuilder extends abstract_video_query_builder_1.AbstractVideoQueryBuilder {
    constructor(sequelize) {
        super(sequelize, 'get');
        this.sequelize = sequelize;
    }
    queryVideos(options) {
        this.buildMainGetQuery(options);
        return this.runQuery(options);
    }
    buildMainGetQuery(options) {
        this.attributes = {
            '"video".*': ''
        };
        if (VideosModelGetQuerySubBuilder.thumbnailsInclude.has(options.type)) {
            this.includeThumbnails();
        }
        if (VideosModelGetQuerySubBuilder.blacklistedInclude.has(options.type)) {
            this.includeBlacklisted();
        }
        if (VideosModelGetQuerySubBuilder.accountInclude.has(options.type)) {
            this.includeChannels();
            this.includeAccounts();
        }
        if (VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)) {
            this.includeTags();
        }
        if (VideosModelGetQuerySubBuilder.scheduleUpdateInclude.has(options.type)) {
            this.includeScheduleUpdate();
        }
        if (VideosModelGetQuerySubBuilder.liveInclude.has(options.type)) {
            this.includeLive();
        }
        if (options.userId && VideosModelGetQuerySubBuilder.userHistoryInclude.has(options.type)) {
            this.includeUserHistory(options.userId);
        }
        if (VideosModelGetQuerySubBuilder.ownerUserInclude.has(options.type)) {
            this.includeOwnerUser();
        }
        if (VideosModelGetQuerySubBuilder.trackersInclude.has(options.type)) {
            this.includeTrackers();
        }
        this.whereId(options);
        this.query = this.buildQuery(options);
    }
    buildQuery(options) {
        const order = VideosModelGetQuerySubBuilder.tagsInclude.has(options.type)
            ? 'ORDER BY "Tags"."name" ASC'
            : '';
        const from = `SELECT * FROM "video" ${this.where} LIMIT 1`;
        return `${this.buildSelect()} FROM (${from}) AS "video" ${this.joins} ${order}`;
    }
}
exports.VideosModelGetQuerySubBuilder = VideosModelGetQuerySubBuilder;
VideosModelGetQuerySubBuilder.trackersInclude = new Set(['api']);
VideosModelGetQuerySubBuilder.liveInclude = new Set(['api', 'full']);
VideosModelGetQuerySubBuilder.scheduleUpdateInclude = new Set(['api', 'full']);
VideosModelGetQuerySubBuilder.tagsInclude = new Set(['api', 'full']);
VideosModelGetQuerySubBuilder.userHistoryInclude = new Set(['api', 'full']);
VideosModelGetQuerySubBuilder.accountInclude = new Set(['api', 'full', 'account-blacklist-files']);
VideosModelGetQuerySubBuilder.ownerUserInclude = new Set(['blacklist-rights']);
VideosModelGetQuerySubBuilder.blacklistedInclude = new Set([
    'api',
    'full',
    'account-blacklist-files',
    'thumbnails-blacklist',
    'blacklist-rights'
]);
VideosModelGetQuerySubBuilder.thumbnailsInclude = new Set([
    'api',
    'full',
    'account-blacklist-files',
    'all-files',
    'thumbnails',
    'thumbnails-blacklist'
]);
//# sourceMappingURL=video-model-get-query-builder.js.map