import { ApiCall } from "tsrpc";
import { ReqSelect, ResSelect } from "../../../shared/protocols/event/christmas/PtlSelect";
import {Christmasfun} from "./fun";
import {HongDianChange} from "../../hongdian/fun";

export default async function (call: ApiCall<ReqSelect, ResSelect>) {
    let initCon = await Christmasfun.getCon(call)
    let index = call.req.index;
    let pid = call.req.pid;
    let lbid = call.req.lbid;
    let _con = initCon[call.req.hdid].data.libao[lbid];
    if (!_con) {
        // 礼包id 不存在
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    if (index >= _con.dlz.length){
        // 礼包id 不存在
        return call.error('', { code: -2, message: globalThis.lng.yangchengmubiao_2 })
    }
    if (index >= _con.dlz.length){
        // 礼包id 不存在
        return call.error('', { code: -3, message: globalThis.lng.yangchengmubiao_2 })
    }
    if (!_con.dlz[index][pid]){
        // 礼包id 不存在
        return call.error('', { code: -4, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _mydata = await Christmasfun.getMyData(call, call.req.hdid)
    let lb_select = _mydata.select[lbid] || {}
    lb_select[index.toString()] = pid
    _mydata.select[lbid] = lb_select
    let _setData = {}
    _setData["select"] = _mydata.select
    await Christmasfun.setMyData(call.uid, call.req.hdid, { $set: _setData })
    let changedata = { mydata: _mydata}
    // 推送红点
    HongDianChange.sendChangeKey(call.uid, ['huodonghd']);
    call.succ(changedata);
}