import define from "../../define.js";
import { makePaginationQuery } from "../../common/make-pagination-query.js";
import { Mutings } from "../../../../models/index.js";
import { ApiError } from "../../error.js";
import { getGroupActor } from "../../common/get-group-actor.js";
export const meta = {
    tags: [
        "account"
    ],
    requireCredential: true,
    kind: "read:mutes",
    errors: {
        noSuchGroup: {
            message: "No such group.",
            code: "NO_SUCH_GROUP",
            id: "d31b97bc-b1f9-4d7f-9402-52161e9d416f"
        }
    },
    res: {
        type: "array",
        optional: false,
        nullable: false,
        items: {
            type: "object",
            optional: false,
            nullable: false,
            ref: "Muting"
        }
    }
};
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(Mutings.createQueryBuilder("muting"), ps.sinceId, ps.untilId);
    if (group) {
        query.andWhere("muting.groupId = :groupId", {
            groupId: group.id
        });
    } else {
        query.andWhere("muting.muterId = :meId", {
            meId: me.id
        });
        query.andWhere("muting.groupId IS NULL");
    }
    const mutings = await query.take(ps.limit).getMany();
    return await Mutings.packMany(mutings, me);
});
