"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceListFollowingQueryBuilder = void 0;
const tslib_1 = require("tslib");
const shared_1 = require("../../shared");
const utils_1 = require("../../utils");
const instance_list_follows_query_builder_1 = require("./shared/instance-list-follows-query-builder");
class InstanceListFollowingQueryBuilder extends instance_list_follows_query_builder_1.InstanceListFollowsQueryBuilder {
    constructor(sequelize, options) {
        super(sequelize, options);
        this.sequelize = sequelize;
        this.options = options;
    }
    listFollowing() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.buildListQuery();
            const results = yield this.runQuery({ nest: true });
            const modelBuilder = new shared_1.ModelBuilder(this.sequelize);
            return modelBuilder.createModels(results, 'ActorFollow');
        });
    }
    countFollowing() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.buildCountQuery();
            const result = yield this.runQuery();
            return (0, utils_1.parseRowCountResult)(result);
        });
    }
    getWhere() {
        let where = 'WHERE "ActorFollowModel"."actorId" = :followerId ';
        this.replacements.followerId = this.options.followerId;
        if (this.options.state) {
            where += 'AND "ActorFollowModel"."state" = :state ';
            this.replacements.state = this.options.state;
        }
        if (this.options.search) {
            const escapedLikeSearch = this.sequelize.escape('%' + this.options.search + '%');
            where += `AND (` +
                `"ActorFollowing->Server"."host" ILIKE ${escapedLikeSearch} ` +
                `OR "ActorFollowing"."preferredUsername" ILIKE ${escapedLikeSearch} ` +
                `)`;
        }
        if (this.options.actorType) {
            where += `AND "ActorFollowing"."type" = :actorType `;
            this.replacements.actorType = this.options.actorType;
        }
        return where;
    }
}
exports.InstanceListFollowingQueryBuilder = InstanceListFollowingQueryBuilder;
//# sourceMappingURL=instance-list-following-query-builder.js.map