import {ApiCall} from "tsrpc";
import {EmailFun} from '../../public/email';
import {PlayerFun} from '../../public/player';
import {ReqAllReceive, ResAllReceive} from "../../shared/protocols/email/PtlAllReceive";

export default async function (call: ApiCall<ReqAllReceive, ResAllReceive>) {
    // 筛选出有奖励并且未领取的邮件
    let emailList = (await EmailFun.getAllEmail(call.uid, false)).filter(email => (
        email.prizeData?.prize.length > 0 && !email.prizeData?.isGet
    ));

    if (emailList.length < 1) return call.error(globalThis.lng.email_1);

    let prizeList = emailList.map(email => email.prizeData.prize).reduce((a, b) => a.concat(b));
    let sendPrize = await PlayerFun.sendPrize(call, prizeList);

    call.succ({
        prize: sendPrize,
        change: EmailFun.receiveEmail(call.uid, emailList.map(email => email._id))
    });
}