<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Provider;

use A2lix\TranslationFormBundle\Locale\LocaleProviderInterface;
use CpCreation\VitiCore\Locale\Repository\LocaleRepository;

class LocaleProvider implements LocaleProviderInterface
{
    public const string DEFAULT = 'fr';

    private array $locales = [self::DEFAULT];

    public function __construct(
        private LocaleRepository $localeRepository,
    ) {
        $locales = $this->localeRepository->findBy(['disabled' => 0], ['position' => 'ASC']);

        foreach ($locales as $locale) {
            $code = $locale->getLocale();

            if (\is_string($code) && !\in_array($code, $this->locales, true)) {
                $this->locales[] = $code;
            }
        }
    }

    public function getLocales(): array
    {
        return $this->locales;
    }

    public function getDefaultLocale(): string
    {
        return self::DEFAULT;
    }

    public function getRequiredLocales(): array
    {
        return [$this->getDefaultLocale()];
    }
}
