<?php

declare(strict_types=1);

namespace Apexl\Io\Validation\Factories;

use Apexl\Io\Validation\Interfaces\NeedsFieldNameInterface;
use Apexl\Io\Validation\ValidationRules\ValidationRule;
use DI\Container;

final readonly class ValidationRuleFactory
{
    public function __construct(
        private Container $container,
    ) {}

    /**
     * @param class-string<ValidationRule> $class
     */
    public function make(string $class, array|string|int $params, string $fieldName): ValidationRule
    {
        \Apexl\Io\assertInstanceOf($class, ValidationRule::class);

        if (!is_array($params)) {
            $params = ['config' => $params];
        }

        $rule = $this->container->make($class, $params);

        if ($rule instanceof NeedsFieldNameInterface) {
            $rule->setFieldName($fieldName);
        }

        return $rule;
    }

}