import { InitResp } from '../../types/api';
import { createCasinoRoundsBetData } from '../../utils/freeRounds';
import { createBetData, loadLastSetUserBet } from '../../utils/utils';

export class BetService
{
    constructor(private raw: InitResp, private outcomesView: { firstBet: number }) {}

    getMainBets()
    {
        const { available_bets, default_bet } = this.raw.options;
        const defaultIndex = Math.max(available_bets.indexOf(default_bet), 0);

        return createBetData(available_bets, defaultIndex, defaultIndex);
    }

    getLastUserBetIndex()
    {
        const { default_bet, available_bets } = this.raw.options;
        const bet = this.outcomesView.firstBet;
        const betIdx = available_bets.indexOf(bet);
        const defaultIdx = available_bets.indexOf(default_bet);
        const lastSetBet = loadLastSetUserBet().bet;
        const lastSetBetIdx = available_bets.indexOf(lastSetBet);

        if (lastSetBetIdx >= 0) return lastSetBetIdx;

        return betIdx >= 0 ? betIdx : Math.max(defaultIdx, 0);
    }

    getCasinoRoundsBets()
    {
        return createCasinoRoundsBetData(this.raw);
    }

    getBonusBets(gameData: any)
    {
        return gameData.casinoRounds ? this.getCasinoRoundsBets() : createBetData([this.outcomesView.firstBet]);
    }
}
