'use strict';
import { shuffleHeroes } from '../heroesHelper';
export default function selectTournamentState(duelController) {
  let duelCtrl = duelController;
  let start = () => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    gameData.game['waitingFor/blue'] = true;
    gameData.game['waitingFor/red'] = true;
    gameData.game.currentPlayer = 'both';
    duelCtrl.storeData(gameData);
  };

  let update = (
    /** @type {import('type/comm').TH_MessageTournamentDeckStep} */ payload = null
  ) => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    if (payload) {
      let player = payload.color + 'player';
      let randomHeroes = shuffleHeroes(payload.twelveHeroes);
      // Init player twelve heroes
      gameData[player].twelveHeroes = randomHeroes;
      gameData.game['waitingFor/' + payload.color] = false;
      duelCtrl.storeData(gameData);
    }

    if (
      gameData.game['waitingFor/blue'] === false &&
      gameData.game['waitingFor/red'] === false
    ) {
      duelCtrl.endCurrentState();
    }
  };

  let end = () => {
    console.log('Go to next game state : 2_CHANGE_UP_TO_3_CARDS');

    duelCtrl.setCurrentState('2_CHANGE_UP_TO_3_CARDS');
    duelCtrl.startCurrentState();
  };
  return {
    start,
    update,
    end
  };
}