<?php

namespace App\Services;

use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\JsonFileLoader;

class TranslationService
{
    private string $langDir;
    private array $supportedLocales;
    private string $defaultLocale;

    public function __construct(string $langDir, array $supportedLocales, string $defaultLocale)
    {
        $this->langDir = $langDir;
        $this->supportedLocales = $supportedLocales;
        $this->defaultLocale = $defaultLocale;
    }

    public function getTranslator(string $locale): Translator
    {
        $translator = new Translator($locale);
        $translator->addLoader('json', new JsonFileLoader());

        foreach ($this->supportedLocales as $supportedLocale) {
            $filePath = $this->langDir . '/' . $supportedLocale . '.json';
            if (file_exists($filePath)) {
                $translator->addResource('json', $filePath, $supportedLocale);
            }
        }

        return $translator;
    }
}
