<?php

namespace GriffinForms\Admin\App\FormControls;

class Factory
{
    public static function create(
        string $field_type,
        string $field_key,
        $field_value = '',
        string $context_id = '',
        array $field_config = []
    ): Format {
        $class_name = self::getClassName($field_type);

        if (!class_exists($class_name)) {
            throw new \InvalidArgumentException(
                sprintf('Unsupported form control type: %s', $field_type)
            );
        }

        return new $class_name($field_key, $field_value, $context_id, $field_config);
    }

    private static function getClassName(string $field_type): string
    {
        $type_map = [
            'text'     => 'Text',
            'email'    => 'Text',
            'password' => 'Text',
            'textarea' => 'Textarea',
            'number'   => 'Number',
            'select'   => 'Select',
            'dropdown' => 'Select',
            'checkbox' => 'Checkbox',
            'radio'    => 'Radio',
            'hidden'   => 'Hidden',
        ];

        $class_suffix = $type_map[$field_type] ?? ucfirst($field_type);

        return __NAMESPACE__ . '\\' . $class_suffix;
    }
}
