"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideosModelListQueryBuilder = 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 videos_id_list_query_builder_1 = require("./videos-id-list-query-builder");
class VideosModelListQueryBuilder extends abstract_video_query_builder_1.AbstractVideoQueryBuilder {
    constructor(sequelize) {
        super(sequelize, 'list');
        this.sequelize = sequelize;
        this.videoModelBuilder = new video_model_builder_1.VideoModelBuilder(this.mode, this.tables);
        this.webtorrentFilesQueryBuilder = new video_file_query_builder_1.VideoFileQueryBuilder(sequelize);
        this.streamingPlaylistFilesQueryBuilder = new video_file_query_builder_1.VideoFileQueryBuilder(sequelize);
    }
    queryVideos(options) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.buildInnerQuery(options);
            this.buildMainQuery(options);
            const rows = yield this.runQuery();
            if (options.include & 8) {
                const videoIds = Array.from(new Set(rows.map(r => r.id)));
                if (videoIds.length !== 0) {
                    const fileQueryOptions = Object.assign(Object.assign({}, (0, core_utils_1.pick)(options, ['transaction', 'logging'])), { ids: videoIds, includeRedundancy: false });
                    const [rowsWebTorrentFiles, rowsStreamingPlaylist] = yield Promise.all([
                        this.webtorrentFilesQueryBuilder.queryWebTorrentVideos(fileQueryOptions),
                        this.streamingPlaylistFilesQueryBuilder.queryStreamingPlaylistVideos(fileQueryOptions)
                    ]);
                    return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include, rowsStreamingPlaylist, rowsWebTorrentFiles });
                }
            }
            return this.videoModelBuilder.buildVideosFromRows({ rows, include: options.include });
        });
    }
    buildInnerQuery(options) {
        const idsQueryBuilder = new videos_id_list_query_builder_1.VideosIdListQueryBuilder(this.sequelize);
        const { query, sort, replacements } = idsQueryBuilder.getQuery(options);
        this.replacements = replacements;
        this.innerQuery = query;
        this.innerSort = sort;
    }
    buildMainQuery(options) {
        this.attributes = {
            '"video".*': ''
        };
        this.addJoin('INNER JOIN "video" ON "tmp"."id" = "video"."id"');
        this.includeChannels();
        this.includeAccounts();
        this.includeThumbnails();
        if (options.user) {
            this.includeUserHistory(options.user.id);
        }
        if (options.videoPlaylistId) {
            this.includePlaylist(options.videoPlaylistId);
        }
        if (options.include & 2) {
            this.includeBlacklisted();
        }
        if (options.include & 4) {
            this.includeBlockedOwnerAndServer(options.serverAccountIdForBlock, options.user);
        }
        const select = this.buildSelect();
        this.query = `${select} FROM (${this.innerQuery}) AS "tmp" ${this.joins} ${this.innerSort}`;
    }
}
exports.VideosModelListQueryBuilder = VideosModelListQueryBuilder;
//# sourceMappingURL=videos-model-list-query-builder.js.map