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

export default async function (call: ApiCall<ReqRecPrize, ResRecPrize>) {

    let change: Partial<ResOpen> = {};
    let data = await DxltFun.getData(call);
    let val = call.req.type == 'killPrizeRec' ? (data.killBoss || 0) : data.maxLayer;
    let conf = call.req.type == 'killPrizeRec' ? G.gc.dxlt_com.killPrize : G.gc.dxlt_com.passPrize;

    if (!conf[call.req.index]) return call.error(globalThis.lng.dixialeitai_5);
    if (data[call.req.type].includes(call.req.index)) return call.error(globalThis.lng.dixialeitai_9);
    if (val < conf[call.req.index].need) return call.error(globalThis.lng.dixialeitai_10);

    data[call.req.type].push(call.req.index);

    change[call.req.type] = data[call.req.type];

    await PlayerFun.sendPrize(call, conf[call.req.index].prize);
    await DxltFun.changeData(call, change);

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

    call.succ({
        change: change,
        prize: conf[call.req.index].prize
    });
}