import { ApiCall } from "tsrpc";
import { FriendManage } from '../../public/friend/manage';
import { ReqApply, ResApply } from "../../shared/protocols/friend/PtlApply";

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

    if (my.data.myApplyList?.includes(call.uid)) return call.error(globalThis.lng.friend_1);
    if (my.data.friendList.includes(call.req.uid)) return call.error(globalThis.lng.friend_2);
    if (my.data.blacklist.includes(call.req.uid)) return call.error(globalThis.lng.friend_3);
    if (my.data.friendList.length >= G.gc.friend.maxFriendNum) return call.error(globalThis.lng.friend_4);

    const he = await FriendManage.getFriend(call.req.uid);
    if (he) {
        if (he.data.friendList.length >= G.gc.friend.maxFriendNum) return call.error('', { code: -1, message: globalThis.lng.friend_5 });
        if (he.data.applyList.includes(call.uid)) return call.error('', { code: -2, message: globalThis.lng.friend_6 });
        if (he.data.blacklist.includes(call.uid)) return call.error('', { code: -3, message: globalThis.lng.friend_7 });

        he.addApply(call.uid);
    }
    my.addMyApply(call.req.uid);
    call.succ({});
}