import { ApiCall } from "tsrpc";
import { DxltFun } from '../../public/dxlt';
import { PlayerFun } from '../../public/player';
import { ResOpen } from '../../shared/protocols/dixialeitai/PtlOpen';
import { ReqRecMiBao, ResRecMiBao } from "../../shared/protocols/dixialeitai/PtlRecMiBao";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqRecMiBao, ResRecMiBao>) {

    let change: Partial<ResOpen> = {};
    let data = await DxltFun.getData(call);

    if (!G.gc.dxlt_mb[call.req.id]) return call.error(globalThis.lng.dixialeitai_5);
    if (data.mibao < G.gc.dxlt_com.getPrizeByMiBaoNum) return call.error(globalThis.lng.dixialeitai_8);

    change.mibao = (data.mibao - G.gc.dxlt_com.getPrizeByMiBaoNum);

    await PlayerFun.sendPrize(call, G.gc.dxlt_mb[call.req.id].mibao);
    await DxltFun.changeData(call, change);

    HongDianChange.sendChangeKey(call.uid, ['dxlthd']);

    call.succ({
        change: change,
        prize: G.gc.dxlt_mb[call.req.id].mibao
    });
}