<?php if(!empty($captcha['enabled']) && !empty($captcha['site_key'])): ?>
    <script>
        window.gpsCaptchaConfig = <?php echo json_encode([
            'provider' => $captcha['provider'], 'siteKey' => $captcha['site_key'], ]) ?>;

        window.GpsCaptcha = window.GpsCaptcha || (function () {
            const config = window.gpsCaptchaConfig || {};

            function upsertHiddenInput(container, token) {
                const form = container.closest('form');
                if (! form) {
                    return;
                }

                let input = form.querySelector('input[name="captcha_token"]');
                if (! input) {
                    input = document.createElement('input');
                    input.type = 'hidden';
                    input.name = 'captcha_token';
                    form.appendChild(input);
                }

                input.value = token || '';
            }

            function setToken(container, token) {
                container.dataset.captchaToken = token || '';
                upsertHiddenInput(container, token || '');
            }

            function resolveContainer(target) {
                if (! target) {
                    return null;
                }

                if (typeof target === 'string') {
                    return document.getElementById(target);
                }

                return target;
            }

            function render(target) {
                const container = resolveContainer(target);
                if (! container || container.dataset.captchaRendered === '1') {
                    return container;
                }

                if (config.provider === 'turnstile' && window.turnstile) {
                    const widgetId = window.turnstile.render(container, {
                        sitekey: config.siteKey,
                        callback: function (token) { setToken(container, token); },
                        'expired-callback': function () { setToken(container, ''); },
                        'error-callback': function () { setToken(container, ''); },
                    });
                    container.dataset.widgetId = String(widgetId);
                    container.dataset.captchaRendered = '1';
                    return container;
                }

                if (config.provider === 'google' && window.grecaptcha) {
                    const widgetId = window.grecaptcha.render(container, {
                        sitekey: config.siteKey,
                        callback: function (token) { setToken(container, token); },
                        'expired-callback': function () { setToken(container, ''); },
                        'error-callback': function () { setToken(container, ''); },
                    });
                    container.dataset.widgetId = String(widgetId);
                    container.dataset.captchaRendered = '1';
                    return container;
                }

                return null;
            }

            function reset(target) {
                const container = resolveContainer(target);
                if (! container || ! container.dataset.widgetId) {
                    return;
                }

                setToken(container, '');

                if (config.provider === 'turnstile' && window.turnstile) {
                    window.turnstile.reset(container.dataset.widgetId);
                }

                if (config.provider === 'google' && window.grecaptcha) {
                    window.grecaptcha.reset(Number(container.dataset.widgetId));
                }
            }

            function getToken(target) {
                const container = resolveContainer(target);
                return container?.dataset.captchaToken || '';
            }

            function boot() {
                document.querySelectorAll('.gps-captcha-widget[data-captcha-render="auto"]').forEach(render);
            }

            return {
                boot,
                render,
                reset,
                getToken,
            };
        })();

        window.gpsCaptchaOnload = function () {
            window.GpsCaptcha?.boot();
        };

        document.addEventListener('DOMContentLoaded', function () {
            if (window.gpsCaptchaConfig?.provider === 'turnstile') {
                let attempts = 0;
                const interval = setInterval(function () {
                    attempts += 1;
                    if (window.turnstile) {
                        clearInterval(interval);
                        window.GpsCaptcha?.boot();
                    } else if (attempts >= 40) {
                        clearInterval(interval);
                    }
                }, 250);
            }
        });
    </script>

    <?php if(($captcha['provider'] ?? 'off') === 'turnstile'): ?>
        <script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" async defer></script>
    <?php elseif(($captcha['provider'] ?? 'off') === 'google'): ?>
        <script src="https://www.google.com/recaptcha/api.js?onload=gpsCaptchaOnload&render=explicit" async defer></script>
    <?php endif; ?>
<?php endif; ?>
<?php /**PATH C:\xampp\htdocs\gps-migration\resources\views\partials\captcha-assets.blade.php ENDPATH**/ ?>