import { ApiCall } from "tsrpc";
import { FriendManage } from '../../public/friend/manage';
import { ReqRmBlackList, ResRmBlackList } from "../../shared/protocols/friend/PtlRmBlackList";

export default async function (call: ApiCall<ReqRmBlackList, ResRmBlackList>) {
    const my = await FriendManage.getFriend(call.uid);

    if (call.req.rm) {
        if (!my.data.blacklist.includes(call.req.uid)) return call.error(globalThis.lng.friend_20);
        my.delBlack(call.req.uid);
    } else {
        const he = await FriendManage.getFriend(call.req.uid);

        if (he) {
            if (my.data.blacklist.includes(call.req.uid)) return call.error(globalThis.lng.friend_21);
            if (my.data.friendList.includes(call.req.uid)) my.delFriend(call.req.uid);
            if (he.data.friendList.includes(call.uid)) he.delFriend(call.uid);
            my.addBlack(call.req.uid);
        }
    }

    call.succ({});
}