import { ApiCall } from "tsrpc";
import { EmailFun } from '../../public/email';
import { PlayerFun } from '../../public/player';
import { ReqReceive, ResReceive } from "../../shared/protocols/email/PtlReceive";

export default async function (call: ApiCall<ReqReceive, ResReceive>) {
    let email = await EmailFun.getEmail(call.uid, call.req._id);

    if (!email) return call.error(globalThis.lng.email_3);
    if (!email.prizeData || (email.prizeData.prize && email.prizeData.prize.length < 1)) return call.error(globalThis.lng.email_5);

    // 全服邮件领取判断
    if (email.uid == "system" && (email?.prizelist || []).includes(call.uid)){
        return call.error(globalThis.lng.email_4)
    }

    if (email.prizeData.isGet) return call.error(globalThis.lng.email_4);

    let prize = await PlayerFun.sendPrize(call, email.prizeData.prize);

    call.succ({
        prize: prize,
        change: EmailFun.receiveEmail(call.uid, call.req._id)
    });
}