<?php

namespace Openguru\OpenCartFramework\Validator;

use Openguru\OpenCartFramework\Container\ServiceProvider;
use Openguru\OpenCartFramework\Validator\ValidationRules\Email;
use Openguru\OpenCartFramework\Validator\ValidationRules\Required;

class ValidatorServiceProvider extends ServiceProvider
{
    protected function rules(): array
    {
        return [
            'required' => fn () => $this->container->get(Required::class),
            'email' => fn () => $this->container->get(Email::class),
        ];
    }

    public function register(): void
    {
        $this->container->bind(ValidatorInterface::class, function () {
            $langCode = $this->container->getConfigValue('', 'ru');
            $translationsFile = __DIR__ . "/translations/$langCode.php";

            if (! file_exists($translationsFile)) {
                $translationsFile = __DIR__ . "/lang/en.php";
            }

            return new Validator($this->rules(), require $translationsFile);
        });
    }
}
