import define from "../../../../define.js";
import { ApiError } from "../../../../error.js";
import { UserEmojis, UserGroupJoinings, UserGroups } from "../../../../../../models/index.js";
export const meta = {
    tags: [
        "groups"
    ],
    requireCredential: true,
    kind: "read:user-groups",
    errors: {
        noSuchGroup: {
            message: "No such group.",
            code: "NO_SUCH_GROUP",
            id: "e9f78109-710a-44fd-8ab2-51582f65dd97"
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        groupId: {
            type: "string",
            format: "misskey:id"
        }
    },
    required: [
        "groupId"
    ]
};
export default define(meta, paramDef, async (ps, me)=>{
    const group = await UserGroups.findOneBy({
        id: ps.groupId
    });
    if (!group) throw new ApiError(meta.errors.noSuchGroup);
    const member = await UserGroupJoinings.findOneBy({
        userGroupId: group.id,
        userId: me.id
    });
    if (group.userId !== me.id && !member) throw new ApiError(meta.errors.noSuchGroup);
    const emojis = await UserEmojis.find({
        where: {
            userGroupId: group.id
        },
        order: {
            createdAt: "DESC"
        }
    });
    return emojis.map((emoji)=>({
            id: emoji.id,
            name: emoji.name,
            url: emoji.publicUrl || emoji.originalUrl,
            glyph: emoji.glyph,
            glyphUrl: emoji.glyph ? emoji.originalUrl : null,
            width: emoji.width,
            height: emoji.height
        }));
});
