<?php

declare(strict_types=1);

namespace Tournament\Scoring;

final class TieBreakerChain
{
    /**
     * @param array<int, TieBreaker> $tieBreakers
     */
    public function __construct(
        private readonly array $tieBreakers
    ) {
    }

    /**
     * Resolve a tie using the configured
     * tie-breaker chain.
     *
     * @param array<int, array{
     *     participant_id: int,
     *     score: float,
     *     placement?: int|null,
     *     kills?: int|null
     * }> $results
     */
    public function resolve(
        array $results
    ): ?int {
        foreach ($this->tieBreakers as $tieBreaker) {
            $winnerId = $tieBreaker->resolve(
                $results
            );

            if ($winnerId !== null) {
                return $winnerId;
            }
        }

        /*
         * No tie-breaker could determine
         * a unique winner.
         */
        return null;
    }
}