jQuery(document).ready(function(){   
    const elements = [
        'wcmlim_distance_calculator_by_coordinates',
        'wcmlim_distance_calculator_by_cloudfare',
        'wcmlim_enable_autodetect_location_by_maxmind',
        'wcmlim_enable_autodetect_location',
        'wcmlim_enable_autodetect_location_with_ipinfo'
    ];

    // Check each element before adding event listeners
    elements.forEach(function(elementId) {
        const element = document.getElementById(elementId);
        if (element) {
            element.addEventListener('change', function() {
                wcmlimUpdateFields(elementId);
            });
        }
    });

    function wcmlimUpdateFields(checkedId){
        let checkboxIds = [
            'wcmlim_distance_calculator_by_coordinates',
            'wcmlim_distance_calculator_by_cloudfare',
            'wcmlim_enable_autodetect_location_by_maxmind',
            'wcmlim_enable_autodetect_location',
            'wcmlim_enable_autodetect_location_with_ipinfo'
        ];

        // Uncheck other checkboxes except the one that was changed
        jQuery.each(checkboxIds, function(index, id) {
            if (id !== checkedId) {
                jQuery('#' + id).prop('checked', false);
            }
        });
    }
});    
