<?php

namespace GriffinForms\Admin\Secure;

use GriffinForms\Includes\Integrations\Categories\Captcha as CaptchaRegistry;

class AntiSpam extends \GriffinForms\Admin\Secure\Settings
{
    protected function secureEnableHoneypot($value)
    {
        return $this->secureToggle($value);
    }

    protected function secureRateLimitStorage($value)
    {
        $allowed_options = ['auto', 'transient', 'db'];
        $secured_value = sanitize_text_field($value);
        if (!in_array($secured_value, $allowed_options, true)) {
            $secured_value = 'transient';
            $this->errors['rate_limiter_storage'] = $this->lang->getText('rate_limit_storage_invalid');
        }
        return $secured_value;
    }

    protected function secureActiveCaptchaProvider($value)
    {
        $registry = CaptchaRegistry::getInstance();
        $providers = $registry->getProviders();
        $selection = sanitize_key($value);

        if ($selection === 'none') {
            return 'none';
        }

        if (empty($selection) || !isset($providers[$selection])) {
            $selection = $registry->getActiveProviderId();
        }

        if (empty($selection) && !empty($providers)) {
            $selection = array_key_first($providers);
        }

        return $selection ?: '';
    }
}
