import define from "../../define.js";
import { CallBlockings } 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: "a863670f-a52a-4bf7-8ddf-d64a83a22dda"
        }
    },
    res: {
        type: "array",
        optional: false,
        nullable: false,
        items: {
            type: "object",
            optional: false,
            nullable: false
        }
    }
};
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(CallBlockings.createQueryBuilder("call_blocking"), ps.sinceId, ps.untilId);
    if (group) {
        query.andWhere("call_blocking.groupId = :groupId", {
            groupId: group.id
        });
    } else {
        query.andWhere("call_blocking.blockerId = :meId", {
            meId: me.id
        });
        query.andWhere("call_blocking.groupId IS NULL");
    }
    const blockings = await query.take(ps.limit).getMany();
    return await CallBlockings.packMany(blockings, me);
});
