"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideosModelListQueryBuilder = void 0;
const abstract_video_query_builder_1 = require("./shared/abstract-video-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('list');
        this.sequelize = sequelize;
        this.videoModelBuilder = new video_model_builder_1.VideoModelBuilder(this.mode, this.tables);
    }
    queryVideos(options) {
        this.buildInnerQuery(options);
        this.buildMainQuery(options);
        return this.runQuery()
            .then(rows => 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.include & 8) {
            this.includeWebtorrentFiles();
            this.includeStreamingPlaylistFiles();
        }
        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