import define from "../../define.js";
import { Blockings } from "../../../../models/index.js";
import { makePaginationQuery } from "../../common/make-pagination-query.js";
import { ApiError } from "../../error.js";
import { getGroupActor } from "../../common/get-group-actor.js";
export const meta = {
    tags: [
        "account"
    ],
    requireCredential: true,
    kind: "read:blocks",
    errors: {
        noSuchGroup: {
            message: "No such group.",
            code: "NO_SUCH_GROUP",
            id: "b31c1162-4adf-4b4b-b3e3-3f2fb8ffcd5f"
        }
    },
    res: {
        type: "array",
        optional: false,
        nullable: false,
        items: {
            type: "object",
            optional: false,
            nullable: false,
            ref: "Blocking"
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        limit: {
            type: "integer",
            minimum: 1,
            maximum: 100,
            default: 30
        },
        sinceId: {
            type: "string",
            format: "misskey:id"
        },
        untilId: {
            type: "string",
            format: "misskey:id"
        },
        groupId: {
            type: "string",
            format: "misskey:id",
            nullable: true
        }
    },
    required: []
};
export default define(meta, paramDef, async (ps, me)=>{
    const group = await getGroupActor(ps.groupId, me);
    if (ps.groupId != null && group == null) throw new ApiError(meta.errors.noSuchGroup);
    const query = makePaginationQuery(Blockings.createQueryBuilder("blocking"), ps.sinceId, ps.untilId);
    if (group) {
        query.andWhere("blocking.groupId = :groupId", {
            groupId: group.id
        });
    } else {
        query.andWhere("blocking.blockerId = :meId", {
            meId: me.id
        });
        query.andWhere("blocking.groupId IS NULL");
    }
    const blockings = await query.take(ps.limit).getMany();
    return await Blockings.packMany(blockings, me);
});
