import { ILeagueStandingsPresenter, LeagueStandingsResultDTO, LeagueStandingsViewModel } from '@core/racing/application/presenters/ILeagueStandingsPresenter';

export class LeagueStandingsPresenter implements ILeagueStandingsPresenter {
  private result: LeagueStandingsViewModel | null = null;

  reset() {
    this.result = null;
  }

  present(dto: LeagueStandingsResultDTO) {
    const driverMap = new Map(dto.drivers.map(d => [d.id, { id: d.id, name: d.name }]));
    const standings = dto.standings
      .sort((a, b) => a.position - b.position)
      .map(standing => ({
        driverId: standing.driverId,
        driver: driverMap.get(standing.driverId)!,
        points: standing.points,
        rank: standing.position,
      }));
    this.result = { standings };
  }

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