import { ApiCall } from "tsrpc";
import { HeroFun } from '../../public/hero';
import { PlayerFun } from '../../public/player';
import { ReqPotency, ResPotency } from "../../shared/protocols/hero/PtlPotency";

export default async function (call: ApiCall<ReqPotency, ResPotency>) {
    if (!G.gc.hero_qianneng[call.req.type]) return call.error(globalThis.lng.hero_14);

    let hero = await HeroFun.getHero(call, call.req._id)
    let qiannneg = hero.qiannneg || Object.fromEntries(Object.keys(G.gc.hero_qianneng).map(k => [k, 0]));
    let nextLv = qiannneg[call.req.type] + 1;

    if (nextLv - Math.min(...Object.values(qiannneg)) > 1) return call.error(globalThis.lng.hero_15);

    let need = G.gc.hero_qianneng[call.req.type][nextLv].need;
    await PlayerFun.checkNeedIsMeet(call, need);
    await PlayerFun.cutNeed(call, need);

    qiannneg[call.req.type]++;
    await HeroFun.changeHeroAttr(call, hero, {
        qiannneg: qiannneg
    });

    call.succ({});
}