import {ApiCall} from "tsrpc";
import {FriendManage} from '../../public/friend/manage';
import {ReqRespond, ResRespond} from "../../shared/protocols/friend/PtlRespond";

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

    if (!my.data.applyList.includes(call.req.uid)) return call.error(globalThis.lng.friend_15);

    const he = await FriendManage.getFriend(call.req.uid);

    if (call.req.agree) {
        if (my.data.friendList.includes(call.req.uid)) return call.error(globalThis.lng.friend_16);
        if (my.data.blacklist.includes(call.req.uid)) return call.error(globalThis.lng.friend_17);
        if (my.data.friendList.length >= G.gc.friend.maxFriendNum) return call.error(globalThis.lng.friend_18);

        if (he) {
            if (he.data.friendList.length >= G.gc.friend.maxFriendNum) return call.error(globalThis.lng.friend_19);
            if (he.data.blacklist.includes(call.uid)) return call.error(globalThis.lng.friend_7)
            he.addFriend(call.uid);
        }

        my.addFriend(call.req.uid);
    }
    my.delApply(call.req.uid);
    he?.delMyApply(call.uid);
    call.succ({});
}