import define from "../../../../define.js";
import { ApiError } from "../../../../error.js";
import { UserEmojis, UserGroups } from "../../../../../../models/index.js";
import { clearGroupEmojiCache } from "../../../../../../misc/populate-emojis.js";
export const meta = {
    tags: [
        "groups"
    ],
    requireCredential: true,
    kind: "write:user-groups",
    errors: {
        noSuchEmoji: {
            message: "No such group emoji.",
            code: "NO_SUCH_GROUP_EMOJI",
            id: "e22d8bd7-d9c5-4470-8ef0-9a9095985b3f"
        }
    }
};
export const paramDef = {
    type: "object",
    properties: {
        id: {
            type: "string",
            format: "misskey:id"
        }
    },
    required: [
        "id"
    ]
};
export default define(meta, paramDef, async (ps, me)=>{
    const emoji = await UserEmojis.findOneBy({
        id: ps.id
    });
    if (!emoji?.userGroupId) throw new ApiError(meta.errors.noSuchEmoji);
    const group = await UserGroups.findOneBy({
        id: emoji.userGroupId,
        userId: me.id
    });
    if (!group) throw new ApiError(meta.errors.noSuchEmoji);
    await UserEmojis.delete(emoji.id);
    await clearGroupEmojiCache(emoji.name, group.username);
});
