<?php

namespace GriffinForms\Includes\Integrations\FormControls;

/**
 * Form Control Factory
 *
 * Factory class for creating form control instances based on field type.
 * Provides a convenient way to instantiate the correct form control class.
 *
 * @since 1.0.0
 */
class Factory
{
    /**
     * Create a form control instance
     *
     * @param string $field_type The type of form control (text, select, checkbox)
     * @param string $field_key The unique identifier for this field
     * @param mixed $field_value The current value of the field
     * @param string $integration_id The integration ID
     * @param array $field_config Optional field configuration array
     * @return Format The form control instance
     *
     * @throws \InvalidArgumentException If the field type is not supported
     */
    public static function create(
        string $field_type,
        string $field_key,
        $field_value = '',
        string $integration_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, $integration_id, $field_config);
    }

    /**
     * Get the full class name for a field type
     *
     * @param string $field_type The field type (text, select, checkbox)
     * @return string The full class name with namespace
     */
    private static function getClassName(string $field_type): string
    {
        // Map field types to class names
        $type_map = [
            'text' => 'Text',
            'email' => 'Text',
            'password' => 'Text',
            'textarea' => 'Textarea',
            'number' => 'Number',
            'select' => 'Select',
            'dropdown' => 'Select',
            'checkbox' => 'Checkbox',
            'hidden' => 'Hidden',
        ];

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

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