@php
    $captcha = app(\App\Services\CaptchaService::class);
    $show = $captcha->shouldShowCaptcha($form ?? '');
@endphp

@if($show)
    {!! $captcha->renderWidget($form ?? '') !!}

    @error($captcha->getResponseFieldName())
        <div class="invalid-feedback d-block">{{ $message }}</div>
    @enderror

    @once
        @push('captcha-scripts')
            @if($captcha->getType() === 'recaptcha_v3')
                <script src="{{ $captcha->getScriptUrl() }}"></script>
            @else
                <script src="{{ $captcha->getScriptUrl() }}" async defer></script>
            @endif

            @if($captcha->getType() === 'invisible_recaptcha')
                <script>
                    // PRC-438 — `document.querySelector('form')` sayfanın ilk
                    // form'unu seçiyordu; store layout'taki header'da locale
                    // switcher form'u DOM sırasında önce geliyor → captcha
                    // çözüldüğünde locale form submit oluyor, asıl login/register
                    // form'u hiç POST etmiyordu. Widget'ı kapsayan form'u
                    // bulup onu submit ediyoruz.
                    function onCaptchaSubmit(token) {
                        var widget = document.querySelector('.g-recaptcha, [data-sitekey]');
                        var form = widget ? widget.closest('form') : null;
                        if (form) {
                            form.submit();
                        } else {
                            console.warn('[captcha] target form for submit not found');
                        }
                    }
                </script>
            @endif

            @if($captcha->getType() === 'recaptcha_v3')
                {!! $captcha->getInlineScript() !!}
            @endif

            
            <script>
                window.captchaFieldName = '{{ $captcha->getResponseFieldName() }}';
                window.__captchaType = '{{ $captcha->getType() }}';
                window.__captchaSiteKey = '{{ $captcha->getSiteKey() }}';
                window.captchaToken = function () {
                    return new Promise(function (resolve) {
                        var type = window.__captchaType;
                        if (type === 'recaptcha_v3') {
                            if (window.grecaptcha && window.grecaptcha.execute) {
                                window.grecaptcha.ready(function () {
                                    window.grecaptcha.execute(window.__captchaSiteKey, { action: 'domain_search' })
                                        .then(function (t) { resolve(t || ''); })
                                        .catch(function () { resolve(''); });
                                });
                            } else { resolve(''); }
                            return;
                        }
                        // v2 / invisible / turnstile / hcaptcha → widget'ın doldurduğu response alanını oku
                        var el = document.querySelector('[name="' + window.captchaFieldName + '"]')
                              || document.getElementById(window.captchaFieldName);
                        resolve(el ? (el.value || '') : '');
                    });
                };
            </script>
        @endpush
    @endonce
@endif
