<?php
// Vendor-specific reCAPTCHA
$vendorRecap = customer_recaptcha();

// Global system reCAPTCHA
$globalRecapEnabled = isset($settings['is_recaptcha']) && $settings['is_recaptcha'] == 1;
$globalRecap = $globalRecapEnabled ? $this->security_m->recaptcha_settings() : null;

// Load script only once
$loaded = false;

// 1️⃣ Vendor reCAPTCHA has highest priority
if (isset($vendorRecap->is_google_recaptcha) && $vendorRecap->is_google_recaptcha == 1) {

    if (!empty($vendorRecap->google_recaptcha_site_key)) {
        echo '<script src="https://www.google.com/recaptcha/api.js?render=' . $vendorRecap->google_recaptcha_site_key . '"></script>';
        $loaded = true;
    }

    // 2️⃣ If vendor doesn't have reCAPTCHA, load global fallback
} elseif ($globalRecapEnabled && $globalRecap) {

    if ($globalRecap->version == 'v3') {
        echo '<script src="https://www.google.com/recaptcha/api.js?render=' . $globalRecap->site_key . '"></script>';
    } else {
        echo '<script src="https://www.google.com/recaptcha/api.js"></script>';
    }

    $loaded = true;
}
