<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\MainBundle\Service\Regroupement;

use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\Regroupment;

class CenterRegroupementResolver
{
    /**
     * Resolves and returns a unique list of centers by merging those from the provided
     * groups and the additional centers while eliminating duplicates.
     *
     * @param list<Regroupment> $groups
     * @param list<Center>      $centers
     *
     * @return list<Center>
     */
    public function resolveCenters(array $groups, array $centers = []): array
    {
        $centersByHash = [];

        foreach ($groups as $group) {
            foreach ($group->getCenters() as $center) {
                $centersByHash[spl_object_hash($center)] = $center;
            }
        }

        foreach ($centers as $center) {
            $centersByHash[spl_object_hash($center)] = $center;
        }

        return array_values($centersByHash);
    }
}
