<?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\Export;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

final readonly class SortExportElement
{
    public function __construct(
        private TranslatorInterface $translator,
    ) {}

    private function trans(string|TranslatableInterface $message): string
    {
        if ($message instanceof TranslatableInterface) {
            return $message->trans($this->translator, $this->translator->getLocale());
        }

        return $this->translator->trans($message);
    }

    /**
     * @param array<int|string, FilterInterface> $elements
     */
    public function sortFilters(array &$elements): void
    {
        uasort($elements, fn (FilterInterface $a, FilterInterface $b) => $this->trans($a->getTitle()) <=> $this->trans($b->getTitle()));
    }

    /**
     * @param array<int|string, AggregatorInterface> $elements
     */
    public function sortAggregators(array &$elements): void
    {
        uasort($elements, fn (AggregatorInterface $a, AggregatorInterface $b) => $this->trans($a->getTitle()) <=> $this->trans($b->getTitle()));
    }
}
