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

export default async function (call: ApiCall<ReqBuy, ResBuy>) {

    let change: Partial<ResOpen> = {};
    let data = await DxltFun.getData(call);
    let shopItem = data.shop[call.req.id];

    if (!shopItem) return call.error(globalThis.lng.dixialeitai_1);
    if (shopItem.useBuyNum >= shopItem.buyNum) return call.error(globalThis.lng.dixialeitai_1);

    shopItem.useBuyNum++;
    change.shop = data.shop;

    await PlayerFun.checkNeedIsMeet(call, shopItem.need);
    await PlayerFun.sendPrize(call, shopItem.prize);
    await PlayerFun.cutNeed(call, shopItem.need);
    await DxltFun.changeData(call, change);

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

    call.succ({
        change: change,
        prize: shopItem.prize
    });
}