import { IGetLeagueProtestsPresenter, GetLeagueProtestsResultDTO, GetLeagueProtestsViewModel } from '@core/racing/application/presenters/IGetLeagueProtestsPresenter';

export class GetLeagueProtestsPresenter implements IGetLeagueProtestsPresenter {
  private result: GetLeagueProtestsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: GetLeagueProtestsResultDTO) {
    const racesById = {};
    dto.races.forEach(race => {
      racesById[race.id] = race;
    });
    const driversById = {};
    dto.drivers.forEach(driver => {
      driversById[driver.id] = driver;
    });
    this.result = {
      protests: dto.protests,
      racesById,
      driversById,
    };
  }

  getViewModel(): GetLeagueProtestsViewModel {
    if (!this.result) throw new Error('Presenter not presented');
    return this.result;
  }
}